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

Correctly indent output of code actions #30

Closed
wants to merge 8 commits into from
Closed
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
14 changes: 11 additions & 3 deletions packages/typescript/src/configs/getFormatCodeSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export async function getFormatCodeSettings(

config = config ?? {};

return {
convertTabsToSpaces: options?.insertSpaces,
const defaultFormatOptions = ctx.typescript.module.getDefaultFormatCodeSettings();
zardoy marked this conversation as resolved.
Show resolved Hide resolved

return Object.assign({}, defaultFormatOptions, filterUndefined({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note:

Suggested change
return Object.assign({}, defaultFormatOptions, filterUndefined({
return Object.assign({}, defaultFormatOptions, JSON.parse(JSON.stringify({

convertTabsToSpaces: options?.insertSpaces ?? false,
tabSize: options?.tabSize,
indentSize: options?.tabSize,
indentStyle: 2 /** ts.IndentStyle.Smart */,
Expand All @@ -37,5 +39,11 @@ export async function getFormatCodeSettings(
placeOpenBraceOnNewLineForFunctions: config.placeOpenBraceOnNewLineForFunctions ?? false,
placeOpenBraceOnNewLineForControlBlocks: config.placeOpenBraceOnNewLineForControlBlocks ?? false,
semicolons: config.semicolons ?? 'ignore',
};
}));
}

function filterUndefined<T extends Record<string, any>>(obj: T) {
return Object.fromEntries(
Object.entries(obj).filter(([k, v]) => v !== undefined)
) as T;
}
37 changes: 36 additions & 1 deletion packages/typescript/src/services/codeActionResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,42 @@ export function resolveRefactorCodeAction(
if (!editInfo) {
return;
}
codeAction.edit = fileTextChangesToWorkspaceEdit(editInfo.edits, ctx);
const fullText = document.getText();
const inferredWhitespaces = document.getText().match(/^[ \t]+/m)?.[0] ?? '\t';
const patchedEdits = editInfo.edits.map(edit => {
if (edit.fileName !== data.fileName) return edit;
return {
...edit,
textChanges: edit.textChanges.map((change) => {
let { newText, span } = change;
const changePos = document.positionAt(span.start);
const startLineOffset = document.offsetAt({
line: changePos.line,
character: 0,
});
if (!/^\s/.test(fullText.slice(startLineOffset)) && newText.split('\n')[0].startsWith('\t')) {
newText = newText.split('\n').map(line => line.replace(/^\t/, '')).join('\n');
}
// patch renameLocation for extract symbol actions (first line only)
if (
editInfo.renameLocation &&
ctx.typescript.module.textSpanContainsPosition({
start: change.span.start,
length: newText.length,
}, editInfo.renameLocation)
) {
const leadingWhitespaceLenDiff = inferredWhitespaces.length - 1;
editInfo.renameLocation += (newText.split('\n')[0].match(/\t/g)?.length ?? 0) * leadingWhitespaceLenDiff;
zardoy marked this conversation as resolved.
Show resolved Hide resolved
}
return {
newText: newText.replaceAll('\t', inferredWhitespaces),
span
};
})
};
});

codeAction.edit = fileTextChangesToWorkspaceEdit(patchedEdits, ctx);
if (editInfo.renameLocation !== undefined && editInfo.renameFilename !== undefined) {
codeAction.command = ctx.commands.createRenameCommand(
document.uri,
Expand Down