Skip to content

Commit

Permalink
More reliable codefix application (#3041)
Browse files Browse the repository at this point in the history
Current approach was getting in all kind of conflict because it was
calling getCodeActions too much and we were clearing the cached id
  • Loading branch information
timotheeguerin authored Mar 22, 2024
1 parent 4e6f137 commit 72bed4d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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

0 comments on commit 72bed4d

Please sign in to comment.