vim 不污染更改列表的脚本搜索功能(撤消)

vim script search function without polluting change list (undo)

所以我写了一个带有 python 插值的小 ulti-snips 片段。 通常当你点击撤消时,在展开片段后,它会回到触发词。但在这种情况下,我必须点击两次撤消。我怀疑这是我正在使用的搜索功能。我真的很感激这方面的帮助。我想要么使用比搜索更好的功能,要么以某种方式使用搜索(或导致此问题的任何原因)不污染撤消历史记录。 这是片段:

snippet super "Adds a super function for the current function" b
`!p
import vim
# get the class name
line_number = int(vim.eval('search("class .*(", "bn")'))
line = vim.current.buffer[line_number - 1]
class_name = re.findall(r'class\s+(.*?)\s*\(', line)[0]
# get the function signature
line_number = int(vim.eval('search("def.*self.*", "bn")'))
line = vim.current.buffer[line_number - 1]
func = re.findall(r'def\s+(.*):', line)[0]
matches = re.findall(r'(.*)\(self,?\s*(.*)\)', func)
snip.rv = 'super(%s, self).%s(%s)' % (class_name, matches[0][0], matches[0][1])
`
endsnippet

您可以在 python 中完全使用文本。 python 比 vim 脚本更强大。这是我的例子:

buf = vim.current.buffer
line_number = vim.current.window.cursor[0] - 1 # cursor line start from 1. so minus it
previous_lines = "\n".join(buf[0:line_number])

try:
    class_name = re.findall(r'class\s+(.*?)\s*\(', previous_lines)[-1]
    func_name, func_other_param = re.findall(r'def\s+(.*)\(self,?\s*(.*)?\):', previous_lines)[-1]
    snip.rv = 'super(%s, self).%s(%s)' % (class_name, func_name, func_other_param)
except IndexError as e:
    snip.rv = 'super'    # regex match fail.