VS Code: how to make a python snippet that after string or expression hitting tab will transform it

妖精的绣舞 提交于 2021-01-27 17:35:09

问题


is it possible to make a python snippet that transforms code like i explain in my example?

"Hello world".print - hit tab transforms it into

print("Hello world")

it will be nice if automatically understand is it string or expression so if i make a variable for example "a" and i write ".print" at the end and hit tab it will not add " " or ' ' so in that way it will not convert it in something else.

a = 10    
a.print - hitting tab

transforms it into:

print(a)

not into:

print("a")

Progress ( if it can be say as that way )

so

TM_CURRENT_LINE

is not working correctly ( may be ).

So "${TM_CURRENT_LINE/[0-9a-z.]//g}" it sopose to remove every number, lowercase character and "." from the line. This little piece of regex code (snippet) works but not really.

When i write

mytext.py - hit tab/enter

it remove everything that is between the prefix point

Code snippet that is used in the picture.

"Print to console - test": {
            "scope": "python",
            "prefix": ".print",
            "body": [
                "${TM_CURRENT_LINE/[0-9a-z.]//g}"
                ],
            "description": "Log output to console"
        }

Do im wrong or this spouse to delete everything in that line?


回答1:


I don't think this is possible to match exactly what you need. What about something like:

"Print": {
    "prefix": ".print",
    "body": [
        "print(${TM_CURRENT_LINE/(.*)\\..+$/$1/})$0"
    ],
    "description": "Print"
}

If I write a.print and hit ENTER this will be the output:

aprint(a)

If I write "a".print this will be the output:

"a"print("a")

You should then remove the first part. This is based on what I know, doing some searches didn't result in a better solution so far.

This will have some problems if you use it on a line which consist of others statements because it'll take TM_CURRENT_LINE. See Variables.




回答2:


NEWER ANSWER:

Try the HyperSnips extension. It lets you use a regex as the snippet prefix so that you can capture exactly what you want preceding the .print.

See https://stackoverflow.com/a/62562886/836330 for more info on setting up the extension. then in your python.hsnips file create this snippet:

snippet `(("[^"]+")|(\b\w+))\.print` "expand to print()" A
print(``rv = m[2] ? m[2] : m[3]``)
endsnippet

The regex (("[^"]+")|(\b\w+))\.print matches both"Hello World".printand a.print with "" in capture group 2 and \b\w+ in capture group 3.

Then those capture groups m[2] and m[3] are inserted into the print() output depending on which capture group has content.

With the A flag, the input is automatically converted once the .print typing is completed.



OLDER ANSWER:

Here is a macro that I think does what you want. Using the multi-command macro extension, put this into your settings.json:

 "multiCommand.commands": [
    
    {
      "command": "multiCommand.printVariable",
      "interval": 150,

      "sequence": [
        "cursorHomeSelect",
        "editor.action.clipboardCutAction",
        {
          "command": "editor.action.insertSnippet",
          "args": {
            "snippet": "print($CLIPBOARD)"
          }
        }
      ]
    }
  ]

and a keybinding into keybindings.json to trigger the above macro:

{
    "key": "alt+p",
    "command": "multiCommand.printVariable",
    "when": "editorFocus"
  },

Now see the demo:

Put the cursor at the end of the variable (only thing on line) and trigger with your keybinding.



来源:https://stackoverflow.com/questions/54985908/vs-code-how-to-make-a-python-snippet-that-after-string-or-expression-hitting-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!