diff --git a/packages/vscode/src/index.ts b/packages/vscode/src/index.ts index 834adf4e..b754ca80 100644 --- a/packages/vscode/src/index.ts +++ b/packages/vscode/src/index.ts @@ -29,6 +29,9 @@ export const middleware: lsp.Middleware = { if (action.command) { action.command = parseServerCommand(action.command); } + if (action.edit) { + normalizeCodeActionEdit(document, action); + } return action; }); return actions; @@ -84,3 +87,29 @@ export function parseServerCommand(command: vscode.Command) { } return command; } + +export const normalizeCodeActionEdit = (document: vscode.TextDocument, action: vscode.CodeAction) => { + if (!action.edit) return; + const editor = vscode.window.visibleTextEditors.find(editor => editor.document.fileName === document.fileName); + if (!editor) return; + const { options: { insertSpaces, tabSize } } = editor; + if (!insertSpaces) return; + + const newEdit = new vscode.WorkspaceEdit(); + const renamePos = action.command?.command === 'editor.action.rename' && action.command.arguments![0][1] as vscode.Position; + + for (const [uri, edits] of action.edit.entries()) { + newEdit.set(uri, edits.map(edit => { + // #region patch renameLocation for extract symbol actions + if (edit.newText.startsWith('\t') && renamePos) { + const endPos = document.positionAt(document.offsetAt(edit.range.start) + edit.newText.length); + if (new vscode.Range(edit.range.start, endPos).contains(renamePos)) { + action.command!.arguments![0][1] = renamePos.translate(0,); + } + } + // #endregion + return new vscode.TextEdit(edit.range, edit.newText.replaceAll('\t', ' '.repeat(tabSize as number))); + })); + } + action.edit = newEdit; +};