0

I want to create a custom snippet for my own usage, I currently want to make a snippet for @emotion/core.

I always want /** @jsx jsx */ to be ontop of my jsx file. So when I import my module on line 9, then import {css, jsx} from '@emotion/core is on line 9 and /** @jsx jsx */ is on line 0. How do I achieve this?

Current Snippet:

"Import emotion":{
    "prefix":"ime",
    "description": "Import emotion",
    "body": 
    [
        "/** @jsx jsx */",
        "import {css, jsx} from '@emotion/core';"
    ]
},

1 Answer 1

1

You are going to have to break the snippet up into separate commands to move the cursor in a middle step. Which will require a macro extension like multi-command.

Put this into your settings.json:

"multiCommand.commands": [

  {
    "command": "multiCommand.insertImports",
    "sequence": [
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "import {css, jsx} from '@emotion/core';"
        }
      },
      // "editor.action.setSelectionAnchor",   // see note below
      "cursorTop",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "/** @jsx jsx */\n"
        }
      },
      // "editor.action.goToSelectionAnchor",
      // "editor.action.cancelSelectionAnchor",
      "cursorDown"  
    ]
  }
]

and some keybinding to trigger that macro:

{
  "key": "ctrl+shift+i",             // whatever keybinding you wish
  "command": "extension.multiCommand.execute",
  "args": {
    "command": "multiCommand.insertImports"
  },
   "when": "editorTextFocus"
},

Note on anchor commands

editor.action.setSelectionAnchor
editor.action.goToSelectionAnchor
editor.action.cancelSelectionAnchor

these commands are in the Insiders' Build and so presumably in the v1.46 release in early June 2020. They are in the macro only to facilitate returning the cursor to where you began. For some reason, the command workbench.action.navigateToLastEditLocation isn't working for me here - I thought it would be sufficient.

Without these new commands the cursor doesn't return to where you began - maybe that isn't a problem for you. In any case, they are coming soon. Once you have the version they are included in just un-comment those commands. Here is a demo using those commands:

insert import at top of page

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.