我想核心代码是
snip.rv='```mermaidn graph $1'+out+'```'
我想在 snip.rv 中使用 tabstop $1,如果可能的话,在 var out 中使用。
完整代码如下所示:
snippet '(?<!\)([0-9])([0-9])gtb' "graphy" r
$1`!p
x=match.group(1)
y=match.group(2)
row1=""
for i in range(int(x)):
row1+=" -->"
row1+="n"
out=int(y)*row1
snip.rv='```mermaidn graph $1'+out+'```'
`$0
endsnippet
你在代码段中使用了 Python 插值 (`!p ...`
(,UltiSnips 不会在嵌入式 Python 代码本身中执行任何占位符替换。(这样做会有问题,因为与转义 Python 术语和字符串相关的许多原因。
相反,UltiSnips会将占位符导出到您可以在Python代码中访问的t
变量中。
请参阅有关 Python 插值块中可用变量的文档,其中包括:
t - The values of the placeholders, t[1] is the text of ${1}, etc.
因此,您实际上可以在该行代码中使用t[1]
作为Python变量,如下所示:
snip.rv='```mermaidn graph '+t[1]+out+'```'
感谢您的帮助,我终于知道我可以使用
snip.expand_anon(anon_snippet_table)
获得我想要的东西的方式
def create_gtable(snip):
# retrieving single line from current string and treat it like tabstops count
placeholders_string = snip.buffer[snip.line].strip()
rows_amount = int(placeholders_string[0])
columns_amount = int(placeholders_string[1])
# erase current line
snip.buffer[snip.line] = ''
# create anonymous snippet with expected content and number of tabstops
anon_snippet_title = "```mermaidn graph "+'$1' + "n"
anon_snippet_end="```"
anon_snippet_body = ""
for row in range(1,rows_amount+1):
anon_snippet_body += ' -> '.join(['$' + str(row*columns_amount+col+1) for col in range(1,columns_amount+1)]) + "n"
anon_snippet_table = anon_snippet_title+ anon_snippet_body+anon_snippet_end
# expand anonymous snippet
snip.expand_anon(anon_snippet_table)