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?

2 Answers 2

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.

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

8 Comments

In the same link I mentioned under 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!
i make a updete in main post. you can see it @ALFA
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.
So basically this thing is not possible to be done with snippet?
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.
|
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.

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.