Revert abbrevs expansion on the second consecutive space keypress

This commit is contained in:
David Strobach 2020-07-28 14:10:42 +02:00
parent ebca1e464f
commit 7cf5a91673

View file

@ -16,8 +16,12 @@ builtins.__xonsh__.abbrevs = dict()
proxy = DynamicAccessProxy("abbrevs", "__xonsh__.abbrevs") proxy = DynamicAccessProxy("abbrevs", "__xonsh__.abbrevs")
setattr(builtins, "abbrevs", proxy) setattr(builtins, "abbrevs", proxy)
last_expanded = None
def expand_abbrev(buffer): def expand_abbrev(buffer):
global last_expanded
last_expanded = None
abbrevs = getattr(builtins, "abbrevs", None) abbrevs = getattr(builtins, "abbrevs", None)
if abbrevs is None: if abbrevs is None:
return return
@ -30,6 +34,26 @@ def expand_abbrev(buffer):
return return
buffer.delete_before_cursor(count=len(word)) buffer.delete_before_cursor(count=len(word))
buffer.insert_text(abbrevs[word]) buffer.insert_text(abbrevs[word])
last_expanded = word
def revert_abbrev(buffer):
global last_expanded
if last_expanded is None:
return False
abbrevs = getattr(builtins, "abbrevs", None)
if abbrevs is None:
return False
if last_expanded not in abbrevs.keys():
return False
document = buffer.document
expansion = abbrevs[last_expanded] + " "
if not document.text_before_cursor.endswith(expansion):
return False
buffer.delete_before_cursor(count=len(expansion))
buffer.insert_text(last_expanded)
last_expanded = None
return True
@events.on_ptk_create @events.on_ptk_create
@ -44,6 +68,7 @@ def custom_keybindings(bindings, **kw):
@handler(" ", filter=IsMultiline() & insert_mode) @handler(" ", filter=IsMultiline() & insert_mode)
def handle_space(event): def handle_space(event):
buffer = event.app.current_buffer buffer = event.app.current_buffer
if not revert_abbrev(buffer):
expand_abbrev(buffer) expand_abbrev(buffer)
buffer.insert_text(" ") buffer.insert_text(" ")