geddski / macros

macros support for VS Code
MIT License
166 stars 36 forks source link

Overview example of inserting a snippet #19

Open ArturoDent opened 7 years ago

ArturoDent commented 7 years ago

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"
}
LeThiHyVong commented 6 years ago

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

ArturoDent commented 6 years ago

{ "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.

LeThiHyVong commented 6 years ago

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

lastobelus commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

    {
        "command": "editor.action.insertSnippet",
        "args": {
            "name": "colo",
            "langId": "javascript",
        }
    },
lastobelus commented 5 years ago

...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.

ottopic commented 5 years ago

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);