Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Implment snip.expand_anon #327

Closed
SeniorMars opened this issue Mar 16, 2023 · 5 comments
Closed

[Request] Implment snip.expand_anon #327

SeniorMars opened this issue Mar 16, 2023 · 5 comments

Comments

@SeniorMars
Copy link

SeniorMars commented Mar 16, 2023

Hello, could this be implemented: expand_anon from ultisnippets?

The reference is here: https://github.com/SirVer/ultisnips/blob/0ad238b1910d447476b2d98f593322c1cdb71285/pythonx/UltiSnips/text_objects/python_code.py#L54 and here: https://github.com/SirVer/ultisnips/blob/0ad238b1910d447476b2d98f593322c1cdb71285/pythonx/UltiSnips/snippet_manager.py#LL297

Implementing this would allow snippets like:

global !p
def create_matrix_placeholders(snip):
	# Create anonymous snippet body
	anon_snippet_body = ""

	# Get start and end line number of expanded snippet
	start = snip.snippet_start[0]
	end = snip.snippet_end[0]

	# Append current line into anonymous snippet
	for i in range(start, end + 1):
		anon_snippet_body += snip.buffer[i]
		anon_snippet_body += "" if i == end else "\n"

	# Delete expanded snippet line till second to last line
	for i in range(start, end):
		del snip.buffer[start]

	# Empty last expanded snippet line while preserving the line
	snip.buffer[start] = ''

	# Expand anonymous snippet
	snip.expand_anon(anon_snippet_body)

def create_matrix(cols, rows, sep, start, end):
	res = ""
	placeholder = 1
	for _ in range(0, int(rows)):
		res += start + f"${placeholder} "
		placeholder += 1
		for _ in range(0, int(cols) - 1):
			res += sep + f" ${placeholder} "
			placeholder += 1
		res += end
	return res[:-1]
endglobal

post_jump "create_matrix_placeholders(snip)"
snippet 'arr(\d+),(\d+)' "LaTeX array" br
\begin{array}{`!p
orient = ""
for _ in range(0, int(match.group(1))): orient += "l"
snip.rv = orient`}
`!p
snip.rv = create_matrix(match.group(1), match.group(2), "&", "\t", "\\\\\n")
`$0
\end{array}
endsnippet

to work for this

https://github.com/SirVer/ultisnips/blob/master/doc/examples/tabstop-generation/demo1.gif?raw=true

This would make my day in Latex, and I would appreciate it.

@fannheyward
Copy link
Member

#67
#205

@chemzqm
Copy link
Member

chemzqm commented Sep 13, 2023

Need communication between python and node process, should be possible, I think.

@SeniorMars
Copy link
Author

SeniorMars commented Sep 15, 2023

Thank you for this! But like in the demo, I have arr9x3 and I expect to be able to jump to the $1 through $27 and modify them -- is post jump implemented?

For instance, I expect:

post_jump "create_matrix_placeholders(snip)"
snippet 'arr(\d+),(\d+)' "LaTeX array" br
\begin{array}{`!p
orient = ""
for _ in range(0, int(match.group(1))): orient += "l"
snip.rv = orient`}
`!p
snip.rv = create_matrix(match.group(1), match.group(2), "&", "\t", "\\\\\n")
`$0
\end{array}
endsnippet

This to produce when doing arr3,3

\begin{array}{lll}
	$1 & $2 & $3 \\
	$4 & $5 & $6 \\
	$7 & $8 & $9 \\
\end{array}

And able to edit $1 and so on

@SeniorMars
Copy link
Author

Or is there a setting I'm missing?

@chemzqm
Copy link
Member

chemzqm commented Sep 17, 2023

No support for post_jump yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants