From 82ddcd725e3baffea1ce11df2679d74d390b2fad Mon Sep 17 00:00:00 2001 From: Georg Schwarz Date: Tue, 21 May 2024 16:29:42 +0200 Subject: [PATCH] Extract getActionForImportCandidate into own method --- .../lib/lsp/jayvee-code-action-provider.ts | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) 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(