Skip to content

Text Snippets not available for CodeAction #592

Closed

Description

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.

Activity

  1. rcjsuen commented on Nov 1, 2018

    @rcjsuen
    Contributor

    Nikolas Komonen (@NikolasKomonen) Can you provide an example of the behaviour you are seeing? I'm not sure I understand your question/issue.

  2. angelozerr commented on Nov 1, 2018

    @angelozerr
    Contributor

    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

  3. NikolasKomonen commented on Nov 1, 2018

    @NikolasKomonen
    Author

    Remy Suen (@rcjsuen)

    So I am trying to apply a TextEdit though code actions. Eg I have this document:

    <project>
    
    </project>
    

    and on the starting 'project' tag I have a code action to insert an attribute with the expected result of:

    <project attName="">
    
    </project>
    

    In this case the TextEdit snippet I have the server sending has the text as "attName="$0"" and the
    CodeAction is instead resulting in:

    <project attName="$0">
    
    </project>
    

    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\""_
                            }
                        ]
                    }
                ]
            }
        }
    ]

    CompletionItem 's Responses have the insertTextFormat property that allows for snippets to be considered,
    in this case 2 means 'Snippet':

    [ ]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"
                }
            }
        ]
    }
  4. rcjsuen commented on Nov 2, 2018

    @rcjsuen
    Contributor

    Nikolas Komonen (@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
        }
    });
  5. rcjsuen commented on Nov 2, 2018

    @rcjsuen
    Contributor

    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.

  6. angelozerr commented on Nov 2, 2018

    @angelozerr
    Contributor

    Thanks Remy Suen (@rcjsuen) for your feedback, it should be cool if LSP could specify this feature snippet support for CodeAction.

  7. NikolasKomonen commented on Nov 2, 2018

    @NikolasKomonen
    Author

    Remy Suen (@rcjsuen) Thanks for the feedback

  8. added
    feature-request
    on Dec 18, 2018
  9. matklad commented on Jan 17, 2019

    @matklad
    Contributor

    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

    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: ()
    }
  10. heejaechang commented on Jan 22, 2021

    @heejaechang

    Dirk Bäumer (@dbaeumer) any plan to support this? are you open to this feature request such as accepting contribution?

  11. dbaeumer commented on Jan 25, 2021

    @dbaeumer
    Member

    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.

  12. matklad commented on Jan 25, 2021

    @matklad
    Contributor

    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

  13. heejaechang commented on Jan 25, 2021

    @heejaechang

    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.

  14. 2 remaining items

  15. KamasamaK commented on Oct 20, 2022

    @KamasamaK
    Contributor

    Dirk Bäumer (@dbaeumer) said

    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.

    VS Code now supports this since v1.72. See microsoft/vscode#145374

  16. dbaeumer commented on Oct 21, 2022

    @dbaeumer
    Member

    Something to work on for 3.18.

  17. heejaechang commented on Nov 10, 2022

    @heejaechang

    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!

  18. rchiodo commented on Nov 10, 2022

    @rchiodo

    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.

  19. heejaechang commented on Nov 10, 2022

    @heejaechang

    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?

  20. jrieken commented on Dec 2, 2022

    @jrieken

    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.

    ... this feature for pylance: microsoft/pylance-release#2615

    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

  21. rchiodo commented on Dec 2, 2022

    @rchiodo

    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.

  22. jrieken commented on Dec 5, 2022

    @jrieken

    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.

  23. deakjahn commented on Jan 12, 2023

    @deakjahn

    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.

  24. puremourning commented on Jan 12, 2023

    @puremourning

    Doesn’t need to be a snippet. Could be an argument (like rename). Snippets are not a panacea of ux

  25. dbaeumer commented on Jul 8, 2024

    @dbaeumer
    Member

    This got implemented for 3.18 be having snippet support in WorkspaceEdits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    code actionsfeature-request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions