2

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

enter image description here

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?

kokogar
  • 51
  • 6

2 Answers2

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.

ALFA
  • 1,726
  • 1
  • 10
  • 19
  • In the same link I mentioned under [variables](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables) there are a full official documentation. I tried to remove the part before `print` but couldn't find a way, looking forward to hear your results, it could be interesting! – ALFA Mar 04 '19 at 15:42
  • i make a updete in main post. you can see it @ALFA – kokogar Mar 04 '19 at 18:42
  • You’re not applying the regex directly to the CURRENT LINE. Here’s how it works: the snippet substitute the prefix with the body, so regex is applied to what you wrote in the body field. So when you type .print the snippet will replace it with the whole line removing any characters, so the same as just removing.print. – ALFA Mar 04 '19 at 23:15
  • So basically this thing is not possible to be done with snippet? – kokogar Mar 05 '19 at 10:21
  • As far as I know my code is the closest thing to the request. The fact is that it is not possible to put variables inside the prefix code, so you cannot handle them. – ALFA Mar 05 '19 at 10:26
  • https://github.com/Microsoft/vscode/issues/69797 So basically. It is not possible with snippets. And if i want this thing i need to make my own extension. – kokogar Mar 05 '19 at 22:11
  • This could be done easily with a macro - similar to https://stackoverflow.com/questions/53599250/how-can-i-insert-a-snippet-on-a-new-line-with-vscode?noredirect=1&lq=1. I will try to get to one tomorrow. – Mark Mar 06 '19 at 00:19
  • I will look it those days to see what can i do :) – kokogar Mar 07 '19 at 22:23
0

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.

hyperSnips demo



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:

demo gif of macro print var

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

Mark
  • 143,421
  • 24
  • 428
  • 436