So I wrote a small ulti-snips snippet with python interpolation. usually when you hit undo, after expanding the snippet, it goes back to trigger word. But in this case I have to hit the undo twice. I suspect it's the search function I am using. I would really appreciate some help on this. I would like to either use a better function than search or somehow use search (or whatever is causing this problem) to not pollute the undo history. Here is the snippet:
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
You can work with text completely in python. python is more powerful than vim script. here is my example:
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.