-
Notifications
You must be signed in to change notification settings - Fork 811
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
Text Snippets not available for CodeAction #592
Comments
@NikolasKomonen Can you provide an example of the behaviour you are seeing? I'm not sure I understand your question/issue. |
@NikolasKomonen post this question because in lsp4xml we have tried to use snippet in CodeAction for xml, see this issue eclipse-lemminx/lemminx#185 |
So I am trying to apply a
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
This is the payload of a similar CodeAction response: [] Received response 'textDocument/codeAction - (69)' in 2ms.
Result: [
{
"title": "Insert required attributes",
"kind": "quickfix",
"diagnostics": [
{
"range": {
"start": {
"line": 1,
"character": 1
},
"end": {
"line": 1,
"character": 10
}
},
"severity": 1,
"code": "cvc-complex-type.4",
"source": "xml",
"message": "cvc-complex-type.4: Attribute 'variant' must appear on element 'resources'."
}
],
"edit": {
"documentChanges": [
{
"textDocument": {
"version": 8,
"uri": "file:///Users/fbricon/Downloads/sample-resources%203/tesst/resources.xml"
},
"edits": [
{
"range": {
"start": {
"line": 1,
"character": 10
},
"end": {
"line": 1,
"character": 10
}
},
_"newText": " variant=\"$1\""_
}
]
}
]
}
}
]
[ ]Received response 'textDocument/completion - (32)' in 2ms.
Result: {
"isIncomplete": false,
"items": [
{
"label": "<?xml ... ?>",
"kind": 10,
"filterText": "xml version=\"1.0\" encoding=\"UTF-8\"?>",
_"insertTextFormat": 2,_
"textEdit": {
"range": {
"start": {
"line": 0,
"character": 2
},
"end": {
"line": 0,
"character": 2
}
},
"newText": "xml version=\"1.0\" encoding=\"UTF-8\"?>$0"
}
}
]
} |
@NikolasKomonen Yeah, I don't think this is expected to work. I was able to reproduce your problem in the Monaco Playground. let uri = monaco.Uri.parse("uri://test.json");
let model = monaco.editor.createModel("xxx\nxxx\nxxx", "json", uri);
monaco.languages.registerCodeActionProvider('json', {
provideCodeActions: function() {
return [
{
title: "abc",
edit: {
edits: [
{
resource: uri,
edits: [
{
range: {
startColumn: 1,
startLineNumber: 1,
endColumn: 1,
endLineNumber: 1
},
text: "def($1)"
}
]
}
]
}
}
]
}
});
monaco.editor.create(document.getElementById("container"), {
model: model,
language: "json",
lightbulb: {
enabled: true
}
}); |
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 |
Thanks @rcjsuen for your feedback, it should be cool if LSP could specify this feature |
@rcjsuen Thanks for the feedback |
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 For struct Foo {
field: ()|
} It should result in #[derive(|)]
struct Foo {
field: ()
} It also should work when the derive already exists, and in this case it only should move the cursor. #[derive(Clone)]
struct Foo {
field: ()|
} => #[derive(Clone|)]
struct Foo {
field: ()
} |
@dbaeumer any plan to support this? are you open to this feature request such as accepting contribution? |
@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. |
@heejaechang we decided that we only add something to the spec if we have an implementation in a client. Otherwise things are hard to spec and to get feedback on. |
I'm confused. Is lsp-mode not a client? |
@dbaeumer said
VS Code now supports this since v1.72. See microsoft/vscode#145374 |
Something to work on for 3.18. |
@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! |
If this was supported for all TextEdits, it would make it possible for us to implement this feature for pylance: Without it, the cursor comes out weird. |
@jrieken how about other places where TextEdit is used in vscode api such as |
@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 @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 |
@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 |
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 |
Doesn’t need to be a snippet. Could be an argument (like rename). Snippets are not a panacea of ux |
# Summary This makes a small addition to our take on the LSP protocol in the form of supporting snippet text edits. It has been discussed [here](microsoft/language-server-protocol#592) on the LSP issue tracker for a while, but seems unlikely to be added anytime soon. This feature was requested by @PatrickMassot for the purposes of supporting Lean code templates in code actions and widgets. --------- Co-authored-by: David Thrane Christiansen <[email protected]>
This got implemented for 3.18 be having snippet support in WorkspaceEdits |
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 theinsertTextFormat
property.If there is already a way could you please let me know.
Thanks.
The text was updated successfully, but these errors were encountered: