Skip to content

Commit

Permalink
Add quickfix to add the default namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 30, 2024
1 parent 3c6da15 commit c0ff05a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
22 changes: 20 additions & 2 deletions packages/core/src/parser/resourceLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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,
},
],
},
},
)
}
}

Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/processor/codeActions/CodeAction.ts
Original file line number Diff line number Diff line change
@@ -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[]
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/processor/codeActions/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export const file: CodeActionProvider<FileNode<AstNode>> = (node, ctx) => {
continue
}
ans.push({
title: action.title,
isPreferred: action.isPreferred,
...action,
errors: [error],
})
}
Expand Down
13 changes: 1 addition & 12 deletions packages/core/src/processor/linter/builtin/undeclaredSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ export const undeclaredSymbol: Linter<AstNode> = (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)
Expand All @@ -42,7 +31,7 @@ export const undeclaredSymbol: Linter<AstNode> = (node, ctx) => {
localeQuote(node.symbol.identifier),
),
node,
info,
undefined,
severityOverride,
)
}
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/source/LanguageError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions packages/language-server/src/util/toLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/locales/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c0ff05a

Please sign in to comment.