Thanks to visit codestin.com
Credit goes to github.com

Skip to content

post_jump and pre_expand do not interact well #1579

@firtrees

Description

@firtrees

The problem

As for me, one of the main uses of pre_expand is to have a flexible number and layout of tabstops, while one of the main uses of post_jump is to perform actions while landing on a specific tabstop.
However, it looks like the numbered tabstops that pre_expand creates cannot be seen by post_jump functions, making it hard for the two to interact.

Indeed, they do not properly interact with the standard expanded snippet either, which might make for easier debugging.

Example 1

global !p

# creates some test placeholders
def test(trigger_length):
# deletes the trigger
	cursorcol = snip.cursor[1]
	snip.buffer[snip.line] = snip.buffer[snip.line][:cursorcol-trigger_length] + snip.buffer[snip.line][cursorcol:]

	mystr = r'{$1} | {$2} | {$3} | $0'
	snip.expand_anon(mystr)
	return 0

endglobal

# creates some other placeholders
pre_expand "test(3)"
snippet try "try"
[$1] | [$2] | [$3] | $0
endsnippet

Expected behaviour

{[$1] | [$2] | [$3] | $0$1} | {$2} | {$3} | $0, in particular if I start typing text should appear in two places.

Actual behaviour

(something equivalent to) {[$1] | [$2] | [$3] | $4} | {$5} | {$6} | $0.

Example 2

global !p

# creates some test placeholders
def test(trigger_length):
# deletes the trigger
	cursorcol = snip.cursor[1]
	snip.buffer[snip.line] = snip.buffer[snip.line][:cursorcol-trigger_length] + snip.buffer[snip.line][cursorcol:]

	mystr = r'{$1} | {$2} | {$3} | $0'
	snip.expand_anon(mystr)
	return 0

def del_useless_stop(delendum):
	cursorcol = snip.cursor[1]
	oldstring = snip.buffer[snip.line]
	newstring = oldstring.replace(delendum, "")
	snip.buffer[snip.line] = newstring
	cursorcol -= len(oldstring)
	cursorcol += len(newstring)
	snip.cursor.set(snip.line, cursorcol)

endglobal

post_jump "if snip.tabstop == 0: del_useless_stop('{}')"
pre_expand "test(3)"
snippet try "try"
endsnippet

Expected behaviour

{$1} | {$2} | {$3} | $0, depending on whether or not something was written in the tabstops 1­--3, the braces should be deleted when jumping to $0.
Indeed, this is the behaviour that one gets with the snippet

post_jump "if snip.tabstop == 0: del_useless_stop('{}')"
pre_expand "test(3)"
snippet try2 "try2"
{$1} | {$2} | {$3} | $0
endsnippet

Actual behaviour

Nothing happens when jumping to $0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions