Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More reliable codefix application #3041

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Improve relability of application of codefixes in IDE, often it would not do anything
15 changes: 5 additions & 10 deletions packages/compiler/src/server/serverlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import { skipTrivia, skipWhiteSpace } from "../core/scanner.js";
import { createSourceFile, getSourceFileKindFromExt } from "../core/source-file.js";
import {
AugmentDecoratorStatementNode,
CodeFix,
CodeFixEdit,
CompilerHost,
DecoratorDeclarationStatementNode,
Expand Down Expand Up @@ -117,8 +116,6 @@ export function createServer(host: ServerHost): Server {
});
const currentDiagnosticIndex = new Map<number, Diagnostic>();
let diagnosticIdCounter = 0;
const currentCodeFixesIndex = new Map<number, CodeFix>();
let codeFixCounter = 0;
compileService.on("compileEnd", (result) => reportDiagnostics(result));

let workspaceFolders: ServerWorkspaceFolder[] = [];
Expand Down Expand Up @@ -797,28 +794,25 @@ export function createServer(host: ServerHost): Server {
}

async function getCodeActions(params: CodeActionParams): Promise<CodeAction[]> {
currentCodeFixesIndex.clear();
const actions = [];
for (const vsDiag of params.context.diagnostics) {
const tspDiag = currentDiagnosticIndex.get(vsDiag.data?.id);
if (tspDiag === undefined || tspDiag.codefixes === undefined) continue;

for (const fix of tspDiag.codefixes ?? []) {
const id = codeFixCounter++;
const codeAction: CodeAction = {
...CodeAction.create(
fix.label,
{
title: fix.label,
command: Commands.APPLY_CODE_FIX,
arguments: [params.textDocument.uri, id],
arguments: [params.textDocument.uri, vsDiag.data?.id, fix.id],
},
CodeActionKind.QuickFix
),
diagnostics: [vsDiag],
};
actions.push(codeAction);
currentCodeFixesIndex.set(id, fix);
}
}

Expand All @@ -827,9 +821,10 @@ export function createServer(host: ServerHost): Server {

async function executeCommand(params: ExecuteCommandParams) {
if (params.command === Commands.APPLY_CODE_FIX) {
const [documentUri, id] = params.arguments ?? [];
if (documentUri && id) {
const codeFix = currentCodeFixesIndex.get(id);
const [documentUri, diagId, fixId] = params.arguments ?? [];
if (documentUri && diagId && fixId) {
const diag = currentDiagnosticIndex.get(diagId);
const codeFix = diag?.codefixes?.find((x) => x.id === fixId);
if (codeFix) {
const edits = await resolveCodeFix(codeFix);
const vsEdits = convertCodeFixEdits(edits);
Expand Down
Loading