diff --git a/libs/language-server/src/lib/lsp/jayvee-code-action-provider.ts b/libs/language-server/src/lib/lsp/jayvee-code-action-provider.ts index e398820d7..6f4e136dd 100644 --- a/libs/language-server/src/lib/lsp/jayvee-code-action-provider.ts +++ b/libs/language-server/src/lib/lsp/jayvee-code-action-provider.ts @@ -6,6 +6,7 @@ import { strict as assert } from 'assert'; import { + type AstNodeDescription, type AstReflection, DocumentValidator, type IndexManager, @@ -103,50 +104,57 @@ export class JayveeCodeActionProvider implements CodeActionProvider { .allElements(refType) .filter((e) => e.name === linkingData.refText); - const actions: CodeAction[] = []; - for (const importCandidate of importCandidates) { - const isInCurrentFile = UriUtils.equals( - importCandidate.documentUri, - document.uri, - ); - if (isInCurrentFile) { - continue; - } + return [ + ...(importCandidates + .map((c) => this.getActionForImportCandidate(c, diagnostic, document)) + .filter((a) => a !== undefined) as unknown as CodeAction[]), + ]; + } - const importPath = this.getRelativeImportPath( - document.uri, - importCandidate.documentUri, - ); + protected getActionForImportCandidate( + importCandidate: AstNodeDescription, + diagnostic: Diagnostic, + document: LangiumDocument, + ): CodeAction | undefined { + const isInCurrentFile = UriUtils.equals( + importCandidate.documentUri, + document.uri, + ); + if (isInCurrentFile) { + return; + } - const importPosition = this.getImportLinePosition( - document.parseResult.value as JayveeModel, - ); - if (importPosition === undefined) { - continue; - } + const importPath = this.getRelativeImportPath( + document.uri, + importCandidate.documentUri, + ); - actions.push({ - title: `Use from '${importPath}'`, - kind: CodeActionKind.QuickFix, - diagnostics: [diagnostic], - isPreferred: false, - edit: { - changes: { - [document.textDocument.uri]: [ - { - range: { - start: importPosition, - end: importPosition, - }, - newText: `use * from "${importPath}";\n`, - }, - ], - }, - }, - }); + const importPosition = this.getImportLinePosition( + document.parseResult.value as JayveeModel, + ); + if (importPosition === undefined) { + return; } - return actions; + return { + title: `Use from '${importPath}'`, + kind: CodeActionKind.QuickFix, + diagnostics: [diagnostic], + isPreferred: false, + edit: { + changes: { + [document.textDocument.uri]: [ + { + range: { + start: importPosition, + end: importPosition, + }, + newText: `use * from "${importPath}";\n`, + }, + ], + }, + }, + }; } protected getImportLinePosition(