question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

editor.insertSnippet() messes with indenting of the SnippetString

See original GitHub issue

This 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:open
  • Created 5 years ago
  • Reactions:4
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jriekencommented, Oct 7, 2019

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

1reaction
jriekencommented, Oct 7, 2019

planned to work around this

How about not working around this but on this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found