You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From what I've seen it doesn't look like CodeAction supports snippets and defaults to plain text with no option to change it.
CompletionItem's allow snippets through the insertTextFormat property.
If there is already a way could you please let me know.
Thanks.
Reacted by Danny Tuppeny, Mickael Istria, Carmenpfeil, Soheil Alizadeh, Heejae Chang, Bryson Thill, Pieter van Loon, Michael Messer, Ahmed Ashour, Suraj and 3 more
I need the ability to position the cursor after applying a code action as well. Using snippets for this seems like a nifty idea! To give a concrete example, I have an add derive assist for Rust, which helps with adding #[derive(Clone, Copy, PartialEq)] boilerplate.
For
structFoo{field:()|
}
It should result in
#[derive(|)]structFoo{field:()}
It also should work when the derive already exists, and in this case it only should move the cursor.
Heejae Chang (@heejaechang) defining this in the spec is the easy part. We need a client implementation as well and at least VS Code has no support for this right now.
Alex Kladov (@matklad) thank you, I will take a look! Dirk Bäumer (@dbaeumer) even though it is not supported by any client yet, would be nice if it is at least in spec, so some client can support it.
Heejae Chang (@heejaechang) defining this in the spec is the easy part. We need a client implementation as well and at least VS Code has no support for this right now.
Dirk Bäumer (@dbaeumer) can you make sure SnippetTextEdit is allowed all places where LSP returns TextEdit not just workspace edits? things like "FormatOnType" which returns TextEdit[].
Johannes Rieken (@jrieken) how about other places where TextEdit is used in vscode api such as provideOnTypeFormattingEdits, provideDocumentRangeFormattingEdits and more? shouldn't all they support SnippetTextEdit?
Heejae Chang (@heejaechang) not yet decided. Your case is valid but generally full blown snippets while users type could be asking for trouble. E.g snippets can force you into an interactive mode, e.g have you to select a choice element, have multiple placeholders, etc pp. Having this happen while I type along (or on save) doesn't seem like a good idea.
Johannes Rieken (@jrieken) thanks for the suggestion. We were holding Megan's solution as a last resort because it requires a lot of work in the extension (whereas the language server already has this information). So, the hope was to be able to just change selection as part of the edits returned from the provideDocumentRangeFormattingEdits.
because it requires a lot of work in the extension (whereas the language server already has this information)
Understood, I would still implement the logic in the language server but with a custom route/message that the extension invokes when certain things happen.
Another use case for them is refactoring code actions. Say, I have a code action that converts a foreach loop into a for loop. During refactoring, I introduce a new index variable that I'd need to allow the user to rename in snippet edit mode. Yes, I do it now by sending it back to the client and doing it from there. Possible but not really elegant, snippets should be supported everywhere.
Activity
rcjsuen commentedon Nov 1, 2018
Nikolas Komonen (@NikolasKomonen) Can you provide an example of the behaviour you are seeing? I'm not sure I understand your question/issue.
angelozerr commentedon Nov 1, 2018
Nikolas Komonen (@NikolasKomonen) post this question because in lsp4xml we have tried to use snippet in CodeAction for xml, see this issue eclipse-lemminx/lemminx#185
NikolasKomonen commentedon Nov 1, 2018
Remy Suen (@rcjsuen)
So I am trying to apply a
TextEditthough code actions. Eg I have this document:and on the starting 'project' tag I have a code action to insert an attribute with the expected result of:
In this case the TextEdit snippet I have the server sending has the text as "attName="$0"" and the
CodeAction is instead resulting in:
This is the payload of a similar CodeAction response:
CompletionItem's Responses have theinsertTextFormatproperty that allows for snippets to be considered,in this case 2 means 'Snippet':
rcjsuen commentedon Nov 2, 2018
Nikolas Komonen (@NikolasKomonen) Yeah, I don't think this is expected to work. I was able to reproduce your problem in the Monaco Playground.
rcjsuen commentedon Nov 2, 2018
Oh, wow. I totally thought this was an issue in the Microsoft/monaco-editor repository! :(
In any case, I agree with your assessment that there is no way for snippets to be used in a
CodeAction.angelozerr commentedon Nov 2, 2018
Thanks Remy Suen (@rcjsuen) for your feedback, it should be cool if LSP could specify this feature
snippet support for CodeAction.NikolasKomonen commentedon Nov 2, 2018
Remy Suen (@rcjsuen) Thanks for the feedback
matklad commentedon Jan 17, 2019
I need the ability to position the cursor after applying a code action as well. Using snippets for this seems like a nifty idea! To give a concrete example, I have an
add deriveassist for Rust, which helps with adding#[derive(Clone, Copy, PartialEq)]boilerplate.For
It should result in
It also should work when the derive already exists, and in this case it only should move the cursor.
=>
heejaechang commentedon Jan 22, 2021
Dirk Bäumer (@dbaeumer) any plan to support this? are you open to this feature request such as accepting contribution?
dbaeumer commentedon Jan 25, 2021
Heejae Chang (@heejaechang) defining this in the spec is the easy part. We need a client implementation as well and at least VS Code has no support for this right now.
matklad commentedon Jan 25, 2021
FWIW, in rust-analyzer we implement this already via a protocol extension and a hacky vscode impl:
Some other clients implement this extension as well, for example, lsp-mode
heejaechang commentedon Jan 25, 2021
Alex Kladov (@matklad) thank you, I will take a look! Dirk Bäumer (@dbaeumer) even though it is not supported by any client yet, would be nice if it is at least in spec, so some client can support it.
2 remaining items
KamasamaK commentedon Oct 20, 2022
Dirk Bäumer (@dbaeumer) said
VS Code now supports this since v1.72. See microsoft/vscode#145374
dbaeumer commentedon Oct 21, 2022
Something to work on for 3.18.
heejaechang commentedon Nov 10, 2022
Dirk Bäumer (@dbaeumer) can you make sure SnippetTextEdit is allowed all places where LSP returns TextEdit not just workspace edits? things like "FormatOnType" which returns TextEdit[].
thank you!
rchiodo commentedon Nov 10, 2022
If this was supported for all TextEdits, it would make it possible for us to implement this feature for pylance:
microsoft/pylance-release#2615
Without it, the cursor comes out weird.
heejaechang commentedon Nov 10, 2022
Johannes Rieken (@jrieken) how about other places where TextEdit is used in vscode api such as
provideOnTypeFormattingEdits,provideDocumentRangeFormattingEditsand more? shouldn't all they support SnippetTextEdit?jrieken commentedon Dec 2, 2022
Heejae Chang (@heejaechang) not yet decided. Your case is valid but generally full blown snippets while users type could be asking for trouble. E.g snippets can force you into an interactive mode, e.g have you to select a choice element, have multiple placeholders, etc pp. Having this happen while I type along (or on save) doesn't seem like a good idea.
Can't you do something like Megan Rogge (@meganrogge) has done with https://marketplace.visualstudio.com/items?itemName=meganrogge.template-string-converter. AFAIK this isn't a format-on-type provider but just a typing listener
rchiodo commentedon Dec 2, 2022
Johannes Rieken (@jrieken) thanks for the suggestion. We were holding Megan's solution as a last resort because it requires a lot of work in the extension (whereas the language server already has this information). So, the hope was to be able to just change selection as part of the edits returned from the
provideDocumentRangeFormattingEdits.jrieken commentedon Dec 5, 2022
Understood, I would still implement the logic in the language server but with a custom route/message that the extension invokes when certain things happen.
deakjahn commentedon Jan 12, 2023
Another use case for them is refactoring code actions. Say, I have a code action that converts a
foreachloop into aforloop. During refactoring, I introduce a new index variable that I'd need to allow the user to rename in snippet edit mode. Yes, I do it now by sending it back to the client and doing it from there. Possible but not really elegant, snippets should be supported everywhere.puremourning commentedon Jan 12, 2023
Doesn’t need to be a snippet. Could be an argument (like rename). Snippets are not a panacea of ux
feat: snippet extension (#3054)
dbaeumer commentedon Jul 8, 2024
This got implemented for 3.18 be having snippet support in WorkspaceEdits