Skip to content

Commit 39aa311

Browse files
committed
Improve clear_call_signatures: restore orig lines
This uses vim.current.buffer directly, with `:undojoin` before it. Fixes davidhalter#651. Note: it uses `silent!` with `undojoin` to work around E790 ("undojoin not allowed after undo"), although `undo` was not used (AFAIK), but only the cursor being moved.
1 parent 4c085a7 commit 39aa311

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

pythonx/jedi_vim.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -447,23 +447,16 @@ def clear_call_signatures():
447447
vim_command('echo ""')
448448
return
449449
cursor = vim.current.window.cursor
450-
e = vim_eval('g:jedi#call_signature_escape')
451-
# We need two turns here to search and replace certain lines:
452-
# 1. Search for a line with a call signature and save the appended
453-
# characters
454-
# 2. Actually replace the line and redo the status quo.
455-
py_regex = r'%sjedi=([0-9]+), (.*?)%s.*?%sjedi%s'.replace(
456-
'%s', re.escape(e))
457-
for i, line in enumerate(vim.current.buffer):
458-
match = re.search(py_regex, line)
459-
if match is not None:
460-
# Some signs were added to minimize syntax changes due to call
461-
# signatures. We have to remove them again. The number of them is
462-
# specified in `match.group(1)`.
463-
after = line[match.end() + int(match.group(1)):]
464-
line = line[:match.start()] + match.group(2) + after
465-
vim.current.buffer[i] = line
466-
vim.current.window.cursor = cursor
450+
451+
if not int(vim_eval("exists('b:_jedi_callsig_orig')")):
452+
return
453+
for linenr, line in vim_eval('b:_jedi_callsig_orig').items():
454+
# Check that the line would be reset, helps with keeping a single
455+
# undochain.
456+
if line != vim.current.buffer[int(linenr)-1]:
457+
vim_command('silent! undojoin')
458+
vim.current.buffer[int(linenr)-1] = line
459+
vim_command('unlet b:_jedi_callsig_orig')
467460

468461

469462
@_check_jedi_availability(show_error=False)
@@ -483,6 +476,7 @@ def show_call_signatures(signatures=()):
483476
return cmdline_call_signatures(signatures)
484477

485478
seen_sigs = []
479+
set_lines = []
486480
for i, signature in enumerate(signatures):
487481
line, column = signature.bracket_start
488482
# signatures are listed above each other
@@ -543,7 +537,17 @@ def show_call_signatures(signatures=()):
543537
tup = '%s, %s' % (len(add), replace)
544538
repl = prefix + (regex % (tup, text)) + add + line[end_column:]
545539

546-
vim_eval('setline(%s, %s)' % (line_to_replace, repr(PythonToVimStr(repl))))
540+
set_lines.append((line_to_replace, repl))
541+
542+
if not set_lines:
543+
return
544+
545+
orig_lines = {}
546+
for linenr, line in set_lines:
547+
orig_lines[linenr] = vim.current.buffer[linenr-1]
548+
vim_command('silent! undojoin')
549+
vim.current.buffer[int(linenr)-1] = line
550+
vim_command("let b:_jedi_callsig_orig = {!r}".format(orig_lines))
547551

548552

549553
@catch_and_print_exceptions

0 commit comments

Comments
 (0)