ryuta46 / vscode-multi-command

Visual Studio Code Plugin named multi-command
MIT License
229 stars 14 forks source link

Variables not working in args #14

Closed ralbear closed 5 years ago

ralbear commented 5 years ago

Im trying to create a simple command to insert a console.log with a new line and getting a selected text. The problem i have is the ${selectedText} variable is not working in the script i have in settings.json

settings.json

{
    "key": "ctrl+shift+l",
    "command": "multiCommand.createConsoleLog",
    "when": "editorLangId == javascript && editorHasSelection",
  }

keybinfings.json

"multiCommand.commands": [
        {
            "command": "multiCommand.createConsoleLog",
            "sequence": [
                "editor.action.insertLineAfter",
                {   
                    "command": "editor.action.insertSnippet",
                    "args": {
                        "snippet": "console.log('Console log $1 :', ${selectedText});"
                    }
                }

            ]
        },
    ]

if i have selected a variable, for example database and make ctrl+shift+l i get this:

console.log('Console log  :', selectedText);

Any idea about how to solve this? Thanks in advance!

ryuta46 commented 5 years ago

Please try this.

"multiCommand.commands": [
    {
        "command": "multiCommand.createConsoleLog",
        "sequence": [
            "editor.action.clipboardCopyAction",
            "editor.action.insertLineAfter",
            {   
                "command": "editor.action.insertSnippet",
                "args": {
                    "snippet": "console.log('Console log $1 :', $CLIPBOARD);"
                }
            }
        ]
    }        
]

You can see the specification of code snippets and the variables in https://code.visualstudio.com/docs/editor/userdefinedsnippets

The variable you want to use is $TM_SELECTED_TEXT. But the selection is cleared when 'editor.action.insertLineAfter' is executed.

So I propose to copy the selected text to the clipboard by using "editor.action.clipboardCopyAction" first. Then execute "editor.action.insertLineAfter" and "editor.action.insertSnippet" with the clipboard text referred by $CLIPBOARD.

ralbear commented 5 years ago

@ryuta46 Thanks, that works perfect, i don't have in mind the selection is empty when i change the line.