Skip to content

Commit b552655

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 40f090a commit b552655

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Diff for: pythonx/jedi_vim.py

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

466459

467460
@_check_jedi_availability(show_error=False)
@@ -481,6 +474,7 @@ def show_call_signatures(signatures=()):
481474
return cmdline_call_signatures(signatures)
482475

483476
seen_sigs = []
477+
set_lines = []
484478
for i, signature in enumerate(signatures):
485479
line, column = signature.bracket_start
486480
# signatures are listed above each other
@@ -541,7 +535,17 @@ def show_call_signatures(signatures=()):
541535
tup = '%s, %s' % (len(add), replace)
542536
repl = prefix + (regex % (tup, text)) + add + line[end_column:]
543537

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

546550

547551
@catch_and_print_exceptions

0 commit comments

Comments
 (0)