From af6734ce4328a538644e7e99cb06f75dbad6580e Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 16 Mar 2023 22:28:35 +0300 Subject: [PATCH 1/3] Normalize code actions indentation --- packages/vscode/src/index.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/vscode/src/index.ts b/packages/vscode/src/index.ts index 834adf4e..6f8011cc 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) { + action.edit = normalizeCodeActionEdit(document, action.edit) ?? action.edit; + } return action; }); return actions; @@ -84,3 +87,15 @@ export function parseServerCommand(command: vscode.Command) { } return command; } + +export const normalizeCodeActionEdit = (document: vscode.TextDocument, edit: vscode.WorkspaceEdit) => { + 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(); + for (const [uri, edits] of edit.entries()) { + newEdit.set(uri, edits.map(edit => new vscode.TextEdit(edit.range, edit.newText.replaceAll('\t', ' '.repeat(tabSize as number))))); + } + return newEdit; +}; From 9d9c1283506649a26cb09f277f4cabbb9692f01d Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 16 Mar 2023 23:26:14 +0300 Subject: [PATCH 2/3] patch renameLocation for all known problem cases --- packages/vscode/src/index.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/vscode/src/index.ts b/packages/vscode/src/index.ts index 6f8011cc..b8c23a06 100644 --- a/packages/vscode/src/index.ts +++ b/packages/vscode/src/index.ts @@ -30,7 +30,7 @@ export const middleware: lsp.Middleware = { action.command = parseServerCommand(action.command); } if (action.edit) { - action.edit = normalizeCodeActionEdit(document, action.edit) ?? action.edit; + normalizeCodeActionEdit(document, action); } return action; }); @@ -88,14 +88,24 @@ export function parseServerCommand(command: vscode.Command) { return command; } -export const normalizeCodeActionEdit = (document: vscode.TextDocument, edit: vscode.WorkspaceEdit) => { +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(); - for (const [uri, edits] of edit.entries()) { - newEdit.set(uri, edits.map(edit => new vscode.TextEdit(edit.range, edit.newText.replaceAll('\t', ' '.repeat(tabSize as number))))); + 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 => { + 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,); + } + } + return new vscode.TextEdit(edit.range, edit.newText.replaceAll('\t', ' '.repeat(tabSize as number))); + })); } - return newEdit; + action.edit = newEdit; }; From efda54d518d699637aa5a5b6ce8a89aac1247868 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 16 Mar 2023 23:27:23 +0300 Subject: [PATCH 3/3] format --- packages/vscode/src/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/vscode/src/index.ts b/packages/vscode/src/index.ts index b8c23a06..b754ca80 100644 --- a/packages/vscode/src/index.ts +++ b/packages/vscode/src/index.ts @@ -94,16 +94,20 @@ export const normalizeCodeActionEdit = (document: vscode.TextDocument, action: v 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))); })); }