Skip to content

Commit

Permalink
Extract getActionForImportCandidate into own method
Browse files Browse the repository at this point in the history
  • Loading branch information
georg-schwarz committed May 21, 2024
1 parent a718fd9 commit 82ddcd7
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions libs/language-server/src/lib/lsp/jayvee-code-action-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { strict as assert } from 'assert';

import {
type AstNodeDescription,
type AstReflection,
DocumentValidator,
type IndexManager,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 82ddcd7

Please sign in to comment.