From c0ff05a97a0c512724780b1911cf5b9f7195d6d6 Mon Sep 17 00:00:00 2001 From: Misode Date: Mon, 30 Dec 2024 02:50:48 +0100 Subject: [PATCH] Add quickfix to add the default namespace --- packages/core/src/parser/resourceLocation.ts | 22 +++++++++++++++++-- .../src/processor/codeActions/CodeAction.ts | 6 ++--- .../core/src/processor/codeActions/builtin.ts | 3 +-- .../linter/builtin/undeclaredSymbol.ts | 13 +---------- packages/core/src/source/LanguageError.ts | 8 ++++++- packages/language-server/src/util/toLS.ts | 13 +++++++++++ packages/locales/src/locales/en.json | 2 +- 7 files changed, 45 insertions(+), 22 deletions(-) diff --git a/packages/core/src/parser/resourceLocation.ts b/packages/core/src/parser/resourceLocation.ts index c31798103..6f6fced62 100644 --- a/packages/core/src/parser/resourceLocation.ts +++ b/packages/core/src/parser/resourceLocation.ts @@ -3,7 +3,7 @@ import { ResourceLocation } from '../common/index.js' import type { ResourceLocationNode, ResourceLocationOptions } from '../node/index.js' import type { ParserContext } from '../service/index.js' import type { Source } from '../source/index.js' -import { Range } from '../source/index.js' +import { ErrorSeverity, Range } from '../source/index.js' import type { InfallibleParser } from './Parser.js' const Terminators = new Set([ @@ -124,7 +124,25 @@ export function resourceLocation( } if (!ans.namespace && options.requireCanonical) { - ctx.err.report(localize('parser.resource-location.namespace-expected'), ans) + ctx.err.report( + localize('parser.resource-location.namespace-expected'), + ans, + ErrorSeverity.Error, + { + codeAction: { + title: localize('code-action.add-default-namespace'), + isPreferred: true, + changes: [ + { + type: 'edit', + range: Range.create(start), + text: ResourceLocation.DefaultNamespace + + ResourceLocation.NamespacePathSep, + }, + ], + }, + }, + ) } } diff --git a/packages/core/src/processor/codeActions/CodeAction.ts b/packages/core/src/processor/codeActions/CodeAction.ts index 7a8d107ad..6a51dfddf 100644 --- a/packages/core/src/processor/codeActions/CodeAction.ts +++ b/packages/core/src/processor/codeActions/CodeAction.ts @@ -1,10 +1,8 @@ import type { AstNode } from '../../node/index.js' import type { CodeActionProviderContext } from '../../service/index.js' -import type { LanguageError } from '../../source/index.js' +import type { LanguageError, LanguageErrorAction } from '../../source/index.js' -export interface CodeAction { - title: string - isPreferred?: boolean +export interface CodeAction extends LanguageErrorAction { errors?: LanguageError[] } diff --git a/packages/core/src/processor/codeActions/builtin.ts b/packages/core/src/processor/codeActions/builtin.ts index fbd343e9c..f805de4a2 100644 --- a/packages/core/src/processor/codeActions/builtin.ts +++ b/packages/core/src/processor/codeActions/builtin.ts @@ -27,8 +27,7 @@ export const file: CodeActionProvider> = (node, ctx) => { continue } ans.push({ - title: action.title, - isPreferred: action.isPreferred, + ...action, errors: [error], }) } diff --git a/packages/core/src/processor/linter/builtin/undeclaredSymbol.ts b/packages/core/src/processor/linter/builtin/undeclaredSymbol.ts index c057dd90a..c83c2139e 100644 --- a/packages/core/src/processor/linter/builtin/undeclaredSymbol.ts +++ b/packages/core/src/processor/linter/builtin/undeclaredSymbol.ts @@ -21,17 +21,6 @@ export const undeclaredSymbol: Linter = (node, ctx) => { }) } if (Config.Action.isReport(action)) { - const info = FileCategories.includes(node.symbol.category as FileCategory) - ? { - codeAction: { - title: localize( - 'code-action.create-undeclared-file', - node.symbol.category, - localeQuote(node.symbol.identifier), - ), - }, - } satisfies LanguageErrorInfo - : undefined const severityOverride = action.report === 'inherit' ? undefined : LinterSeverity.toErrorSeverity(action.report) @@ -42,7 +31,7 @@ export const undeclaredSymbol: Linter = (node, ctx) => { localeQuote(node.symbol.identifier), ), node, - info, + undefined, severityOverride, ) } diff --git a/packages/core/src/source/LanguageError.ts b/packages/core/src/source/LanguageError.ts index b26f5d435..5b5178b31 100644 --- a/packages/core/src/source/LanguageError.ts +++ b/packages/core/src/source/LanguageError.ts @@ -69,5 +69,11 @@ export interface LanguageErrorInfo { export interface LanguageErrorAction { title: string isPreferred?: boolean - // TODO + changes?: CodeActionChange[] +} + +export type CodeActionChange = { + type: 'edit' + range: Range + text: string } diff --git a/packages/language-server/src/util/toLS.ts b/packages/language-server/src/util/toLS.ts index 98e60b427..41efbcc07 100644 --- a/packages/language-server/src/util/toLS.ts +++ b/packages/language-server/src/util/toLS.ts @@ -182,6 +182,19 @@ export function codeAction(codeAction: core.CodeAction, doc: TextDocument): ls.C kind: ls.CodeActionKind.QuickFix, isPreferred: codeAction.isPreferred, diagnostics: codeAction.errors?.map(e => diagnostic(core.LanguageError.withPosRange(e, doc))), + edit: codeAction.changes + ? { + documentChanges: codeAction.changes.map(change => { + switch (change.type) { + case 'edit': + return { + textDocument: { uri: doc.uri, version: doc.version }, + edits: [{ range: range(change.range, doc), newText: change.text }], + } satisfies ls.TextDocumentEdit + } + }), + } + : undefined, } } diff --git a/packages/locales/src/locales/en.json b/packages/locales/src/locales/en.json index e48eb3900..e40ba5e77 100644 --- a/packages/locales/src/locales/en.json +++ b/packages/locales/src/locales/en.json @@ -7,7 +7,7 @@ "code-action.fix-file": "Fix all auto-fixable problems in this file", "code-action.fix-workspace": "Fix all auto-fixable problems in the workspace", "code-action.id-attribute-datafix": "Update this attribute name to 1.16", - "code-action.id-complete-default-namespace": "Complete default namespace", + "code-action.add-default-namespace": "Add default namespace", "code-action.create-undeclared-file": "Create %0% %1% in the same pack", "code-action.id-omit-default-namespace": "Omit default namespace", "code-action.id-zombified-piglin-datafix": "Change this ID to Zombified Piglin's",