Sublime Forum

Strange cursor location when using begin{}-end{} snippet for list environment in LaTeX

#1

When I use the begin{}-end{} snippet to type a list environment (i.e. itemize, enumerate, list, description) in LaTeX, after I enter the environment name in brace brackets, the snippet will automatically fill one \item inside the environment. But when I press Tab, the cursor jumps to the position before the \item command rather than after it, where I suppose to continue typing (see the enclosed screencast).

I’ve checked the snippet code:

<snippet>
	<content><![CDATA[\\begin{${1:env}}
	${1/(enumerate|itemize|list)|(description)|.*/(?1:\\item )(?2:\\item)/}$0
\\end{${1:env}}]]></content>
	<tabTrigger>begin</tabTrigger>
	<scope>text.tex.latex</scope>
	<description>\begin{}…\end{}</description>
</snippet>

The $0 mark is indeed after the substituted \item command.

The ST3 version I used is build 3143. This issue occurs on Windows 7 (6.1.7601), macOS (High Sierra 10.13.2), and Ubuntu 17.10, even when there is no third-party package enabled. Does anyone know how to fix this?

0 Likes

#2

Is this a bug of Sublime Text, or am I doing something wrong?

I’ve filed a bug report at Sublime Text’s package repository, too. No one responds after two months.

0 Likes

#3

I’m guessing that ST calculates the region where $0 refers to before any substitutions take place, i.e. it’s a static position. So not necessarily a bug per se, but an implementation detail that prevents it from working the way you want it to.

possibly slightly related:

0 Likes

#4

Thanks for your explanation. That issue doesn’t seem very helpful.

After a few tries, I find that if there are any characters between the substitution and the $0 mark, then the tab navigation will work as what I expect, i.e., the cursor will jump to the location where $0 is no matter whether the substitution takes place or not.

For example, if I replace the original line

${1/(enumerate|itemize|list)|(description)|.*/(?1:\\item )(?2:\\item)/}$0

by

${1/(enumerate|itemize|list)|(description)|.*/(?1:\\item )(?2:\\item)/} $0

then

A workaround

However, this is an ugly workaround, because the additional character inserted before $0 mark will still be there.

Maybe such a problem is an intentional design, but I still think it is more like a bug because when the substitution part is put before the $0 mark in the snippet, it is supposed the substitution and the region where $0 refers to will follow the order

0 Likes