From af95c0283dd68400e07f6fc3caae722a476f53dd Mon Sep 17 00:00:00 2001 From: Talent Zeng Date: Mon, 19 Aug 2024 12:11:43 -0400 Subject: [PATCH] feat(utils): add support for 1.2 in code completions --- src/constants/schema-version.ts | 1 + src/tools/monaco/providers/code-actions.ts | 18 ++++++++++++++++++ src/tools/monaco/providers/completion.ts | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/constants/schema-version.ts b/src/constants/schema-version.ts index 2ef818f..ed9d751 100644 --- a/src/constants/schema-version.ts +++ b/src/constants/schema-version.ts @@ -1,4 +1,5 @@ export enum SchemaVersion { OneDotZero = "1.0", OneDotOne = "1.1", + OneDotTwo = "1.2", } diff --git a/src/tools/monaco/providers/code-actions.ts b/src/tools/monaco/providers/code-actions.ts index fa7397b..e8c7bd9 100644 --- a/src/tools/monaco/providers/code-actions.ts +++ b/src/tools/monaco/providers/code-actions.ts @@ -48,6 +48,14 @@ const errorFixesByErrorCodeAndSchema: Partial< text: `${SINGLE_INDENTATION}${SINGLE_INDENTATION}${Keyword.DEFINE} ${relation}: [typeName]\n${lineContent}`, }; }, + [SchemaVersion.OneDotTwo]: ({ model, marker, relation }) => { + const lineContent = model.getLineContent(marker.startLineNumber); + return { + startColumn: 0, + title: `Fix: add definition for \`${relation}\`.`, + text: `${SINGLE_INDENTATION}${SINGLE_INDENTATION}${Keyword.DEFINE} ${relation}: [typeName]\n${lineContent}`, + }; + }, }, [errors.ValidationError.SelfError]: { [SchemaVersion.OneDotZero]: ({ text }) => ({ @@ -58,6 +66,10 @@ const errorFixesByErrorCodeAndSchema: Partial< title: `Fix: replace \`${text}\` with type restrictions.`, text: "[typeName]", }), + [SchemaVersion.OneDotTwo]: ({ text }) => ({ + title: `Fix: replace \`${text}\` with type restrictions.`, + text: "[typeName]", + }), }, [errors.ValidationError.DuplicatedError]: { [SchemaVersion.OneDotZero]: ({ model, marker, markerRange, text }) => ({ @@ -72,6 +84,12 @@ const errorFixesByErrorCodeAndSchema: Partial< title: `Fix: remove duplicated \`${text}\`.`, text: "", }), + [SchemaVersion.OneDotTwo]: ({ model, marker, markerRange, text }) => ({ + startLineNumber: markerRange.startLineNumber - 1, + startColumn: model.getLineContent(marker.startLineNumber - 1).length + 1, + title: `Fix: remove duplicated \`${text}\`.`, + text: "", + }), }, }; diff --git a/src/tools/monaco/providers/completion.ts b/src/tools/monaco/providers/completion.ts index dbd8cae..961c956 100644 --- a/src/tools/monaco/providers/completion.ts +++ b/src/tools/monaco/providers/completion.ts @@ -37,7 +37,7 @@ function getSuggestions( label: `sample-${key}`, kind: monaco.languages.CompletionItemKind.Struct, insertText: transformer.transformJSONToDSL( - schemaVersion === SchemaVersion.OneDotOne + schemaVersion === SchemaVersion.OneDotOne || schemaVersion === SchemaVersion.OneDotTwo ? sampleModel : { schema_version: SchemaVersion.OneDotZero, @@ -228,6 +228,7 @@ export const provideCompletionItems = ) => (model: editor.ITextModel, position: Position): languages.ProviderResult => { switch (schemaVersion) { + case SchemaVersion.OneDotTwo: case SchemaVersion.OneDotOne: return provideCompletionItemsOneDotOne(monaco, completionExtraOptions)(model, position); case SchemaVersion.OneDotZero: