editor.insertSnippet() messes with indenting of the SnippetString
See original GitHub issueThis is a very similar issue to #44200/#57093 but that fix appears to be only being applied to completions, but applying snippets causes the same issues.
Here’s a repro:
vs.workspace.openTextDocument(vs.Uri.parse(`untitled:${os.tmpdir}/a.dart`)).then(async (document) => {
const originalCode = `main() {
// code indented with two spaces
}`;
const editor = await vs.window.showTextDocument(document);
await editor.edit((eb) => eb.insert(new vs.Position(0, 0), originalCode));
const startOffset = originalCode.indexOf("// code indented with two spaces");
const endOffset = originalCode.indexOf("\n}");
const snippetString = new vs.SnippetString("// new code indented with two spaces\n // newer code indented with two spaces");
await editor.insertSnippet(snippetString, new vs.Range(document.positionAt(startOffset), document.positionAt(endOffset)));
});
This creates a doc and inserts into it the code:
main() {
// code indented with two spaces
}
It then applies a snippet string that includes an additional line that is also indented by two spaces. However what you end up with is:
main() {
// new code indented with two spaces
// newer code indented with two spaces
}
The inserted line has been re-indented.
Whilst this behaviour may be useful if applying basic hard-coded strings, it’s incorrect for language servers that are correctly calculating edits to leave the file indented/formatted correctly (I guess for the same reasons that left to #57093).
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:14 (11 by maintainers)
Top Results From Across the Web
vscode surround text with snippet with correct indentation
This works for most part except that if I select the entire line of indented code, it inserts try at the beginning of...
Read more >Snippets in Visual Studio Code
Snippets in Visual Studio Code. Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements....
Read more >Community Code Snippets: Script Formatting Revisited (Part 1)
Axiom: Properly formatted and indented code is the first step toward maintainability. Steps to fix it up: 1. Click on the Format button...
Read more >vscode.SnippetString - Haxe externs for Visual Studio Code
A snippet string is a template which allows to insert text and to control the editor cursor when insertion happens.
Read more >visual-studio-code - How to link Codelens provider with reference ...
Found the answer, We have to use "editor.action.showReferences" command from VS- Code built-in commands. Reference from the github repo of vscode: ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well, the issue wouldn’t be open otherwise. This is your code pointer from which you can drill into things: https://github.com/microsoft/vscode/blob/2d713ac3a5385224a40ec6911195b14ac5a26139/src/vs/workbench/api/common/extHostTextEditor.ts#L626
How about not working around this but on this?