macros icon indicating copy to clipboard operation
macros copied to clipboard

Overview example of inserting a snippet

Open ArturoDent opened this issue 7 years ago • 8 comments

I tried to get your example of inserting a snippet within a macro to work without success. From VSCode extensions marketplace : macros. Your example:

And I have a macro that will select the word at my cursor, copy it, drop to a new line, and run that snippet to log the variable:

editor.action.addSelectionToNextFindMatch

`"macros": {
    "logCurrentVariable": [
    "editor.action.addSelectionToNextFindMatch",
    "problems.action.copy",

      {"command": "type", "args": {"text": "con"}}
  ]
}`

At the very least that must be missing an "insertSnippet" command? And there is no copy or paste? And the weird mention of "editor.action.addSelectionToNextFindMatch" seems out of place.

Might I suggest:

 "logCurrentVariable": [
      "editor.action.smartSelect.grow",
      "editor.action.clipboardCopyAction",
      "editor.action.insertLineAfter",
      {
        "command": "type",
        "args": {
          "text": "log"
        }
     },
    "insertSnippet",
    "editor.action.clipboardPasteAction"
  ]

which uses the following snippet (in VSCode) 👍

"Print to console": {
    "prefix": "log",
     "body": [
         "console.log('${TM_FILENAME}, line ${TM_LINE_NUMBER}   :  $1 = ' + $1);"
    ],
   "description": "Log output to console"
}

ArturoDent avatar Oct 20 '17 04:10 ArturoDent

How do you getting user-defined snippet working with macros? I have tried your and author's suggestion but neither work. I've used your example snippet (just change the prefix to "logex") and write the macro

 "logCurrentVariable": [
      {
        "command": "type",
        "args": {
          "text": "logex"
        }
     },
    "insertSnippet"
  ]

After press the assigned keyboard for this macro, it just insert the prefix and the suggestion is displayed instead of inserting entire snippet. I'm using VS 1.18.1, all other extensions are disabled. untitled

LeThiHyVong avatar Dec 01 '17 01:12 LeThiHyVong

{ "key": "ctrl+shift+.", "command": "macros.logCurrentVariable" },

Just to show how my keybinding is set up. Otherwise what you have looks like it should work.

You might try playing with these settings:

// Insert snippets when their prefix matches. Works best when 'quickSuggestions' aren't enabled. "editor.tabCompletion": false,

// Controls if suggestions should automatically show up while typing

"editor.quickSuggestions": {
   "other": true,
   "comments": false,
   "strings": false
 },

I haven't changed those from the defaults - see if you have.

ArturoDent avatar Dec 06 '17 02:12 ArturoDent

Change to "editor.tabCompletion": true and it works perfectly

LeThiHyVong avatar Dec 06 '17 04:12 LeThiHyVong

In Code 1.29.1, this isn't working for me, regardless of the setting for editor.tabCompletion (which is now

insertSnippet doesn't do anything. editor.action.insertSnippet ignores the trigger text and brings up the list of snippets in the command palette. Typing a "\t" inserts a literal tab.

@ArturoDent @LeThiHyVong is this still working for you in code 1.29.1?

lastobelus avatar Dec 07 '18 23:12 lastobelus

editor.action.insertSnippet can take args in native keyboard bindings, so I tried that:

        {
            "command": "editor.action.insertSnippet",
            "args": {
                "snippet": "colo",
                "langId": "javascript",
            }
        },

but that also only typed the trigger text and did not expand the snippet :(

I also tried insertBestCompletion

Cannot get the macro to actually expand the snippet!

lastobelus avatar Dec 07 '18 23:12 lastobelus

Ah! had the args to editor.action.insertSnippet wrong. Finally, this works:

    {
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "colo",
            "langId": "javascript",
        }
    },

lastobelus avatar Dec 07 '18 23:12 lastobelus

...but then, sadly, when followed with editor.action.clipboardPasteAction it does them in reverse order, pasting the copied word before the snippet, instead of into the tab stop :(

I'll be at least a decade getting back my time investment on this "productivity" enhancement.

lastobelus avatar Dec 07 '18 23:12 lastobelus

It finally works fo me too, but I the problem that all $ in the snipped are removed, any idea to solve it?

"macros": { "printr": [ "editor.action.smartSelect.grow", "editor.action.clipboardCopyAction", "editor.action.insertLineAfter", { "command": "editor.action.insertSnippet", "args": { "name": "customprintr", "langId": "php", } }, ] }

snippet

$printMe = ${CLIPBOARD}; print_r($printMe);

result:

printMe = $myVar; print_r(printMe);

ottopic avatar Jan 11 '19 16:01 ottopic