Sublime Forum

Snippet: how to check if TM_CURRENT_LINE starts with space

#1

Hello,
I have the following snippet which I use to create a python function:

<snippet>
	<content><![CDATA[
def ${1:function}():
	${0:...}
]]></content>
	<scope>source.python - comment - string</scope>
	<tabTrigger>def</tabTrigger>
	<description>def function(): …</description>
</snippet>

Every time I type def and press tab, it generates:

def function():
    ...

I would like to make the snippet a bit more clever, and recognize (potential) class methods instead of basic functions, and produce this instead:

def function(self):
    ...

In order to achieve this, I think that I could check whether the current line (TM_CURRENT_LINE) starts with a space, and then do a regex replacement, but I’m not sure how to do this.
Any suggestion?

0 Likes

#2

I would try something like this:

<snippet>
	<content><![CDATA[
def ${1:function}(${TM_CURRENT_LINE/\s+/self/}):
	${0:...}
]]></content>
	<scope>source.python - comment - string</scope>
	<tabTrigger>def</tabTrigger>
	<description>def function(): …</description>
</snippet>

The function argument list is an expansion of the current line, with a sequence of 1 or more whitespace characters converted into the word self; it won’t trigger and will return itself if the line doesn’t have any whitespace in it.

2 Likes

#3

Hi Terence,
I was also playing with something along those lines. It looks like your solution is incorrect though. Without spaces it produces:

def function(def):
    ...

With spaces:

    def function(selfdef):
        ...

While google searching I ended up on this SO thread where you replied, which seems somewhat related to this (because it’s about checking wheter “X” is in string) but I wasn’t good enough to re-adapt it to my needs.

0 Likes

#4

It worked when I tested it locally, but in my test the line was blank except for the whitespace that indents it. Are you trying to expand the snippet with other text already on the line?

0 Likes

#5

I’m simply typing "def" or " def" and then pressing TAB. Other than that the file is empty.

0 Likes

#6

I don’t see that here; maybe try in safe mode to see if there’s some package that’s trying to take actions as a part of this?

com-video-to-gif

0 Likes

#7

Mmm… This is weird. The problem only occurs if I type “def” and then press TAB. On the other hand if I:

  • type "def"
  • wait for the auto-completion menu to show up (or press alt+/ to make it appear)
  • then press ENTER or TAB

…it doesn’t happen (everything works as expected). The same thing happens in safe mode.

0 Likes

#8

I don’t see that here, but the distinction in my test above and showing the AC panel is because there is already a pair of snippets that does this and I wanted to verify that I was picking the right one.

Since it’s just a core snippet the AC panel should not matter; what build are you running?

0 Likes

#9

Build 4147 on Ubuntu 22.04.

0 Likes

#10

When the AC panel shows up, how many snippets do you see with a tab trigger of just def? In my case there’s two and just pressing tab picks the one that ships with Sublime; perhaps you have another version of the snippet that’s hitting?

That one is a real shot in the dark if it’s also happening in Safe Mode, where there should be only the core snippet and the single one you put in though.

Possible bug report?

0 Likes

#11

When the AC panel shows up, how many snippets do you see with a tab trigger of just def ?

In safe mode I see your same panel entries. In normal mode I see this:
Screenshot%20from%202023-02-27%2023-44-48
It must be noted that in normal mode I have this setting enabled, which disables the default snippet with the same trigger keys:

	"ignored_snippets": ["Python/*"],

I realized that safe and non-safe modes are not exactly the same. In safe-mode the problem occurs less often, and that’s because the AC panel shows up much more quickly than in normal mode.

In both modes I can reliably reproduce the problem 100% of the times with the following steps:

  1. type "def", wait for AC panel to show up
  2. press TAB, auto complete occurs and it’s correct
  3. press CTRL+Z, we’re back at "def"
  4. press TAB again, problem occurs
0 Likes