From 9125cd787967a5f9896c97d49617306f88602050 Mon Sep 17 00:00:00 2001 From: David <2889367+daveleroy@users.noreply.github.com> Date: Sun, 16 Oct 2022 10:21:18 -0700 Subject: [PATCH] move up/down in the debugger console now autofills previously entered commands --- modules/debugger_console_panel.py | 62 +++++++++++++++++++++++++++---- modules/debugger_output_panel.py | 2 +- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/modules/debugger_console_panel.py b/modules/debugger_console_panel.py index baa344c..3a5b8b5 100644 --- a/modules/debugger_console_panel.py +++ b/modules/debugger_console_panel.py @@ -36,6 +36,9 @@ def __init__(self, debugger: Debugger) -> None: self.indent = '' self.forced_indent = '' + self._history_offset = 0 + self._history = [] + settings = self.view.settings() settings.set('line_numbers', False) settings.set('gutter', False) @@ -244,10 +247,15 @@ def on_text_command(self, command_name: str, args: Any): if command_name == 'insert' and args['characters'] == '\n' and self.enter(): return ('noop') - def on_query_completions(self, prefix: str, locations: list[int]) -> Any: - if not self.debugger.is_active: return - if not self.debugger.active.capabilities.supportsCompletionsRequest: return + if not self.view.is_auto_complete_visible() and command_name == 'move' and args['by'] =='lines': + self.set_input_mode() + if args['forward']: + self.autofill(-1) + else: + self.autofill(1) + return ('noop') + def on_query_completions(self, prefix: str, locations: list[int]) -> Any: input = self.input() if not input: return @@ -255,10 +263,25 @@ def on_query_completions(self, prefix: str, locations: list[int]) -> Any: col = (locations[0] - input.b) completions = sublime.CompletionList() + items: list[sublime.CompletionItem] = [] + + for fill in self._history: + items.append(sublime.CompletionItem.command_completion( + trigger=fill, + annotation='', + kind=sublime.KIND_SNIPPET, + command='insert', + args= { + 'characters': fill + } + )) + @core.schedule async def fetch(): - items: list[sublime.CompletionItem] = [] try: + if not self.debugger.is_active or not self.debugger.active.capabilities.supportsCompletionsRequest: + raise core.CancelledError + for completion in await self.debugger.active.completions(text, col): if completion.type == 'method' : kind = sublime.KIND_FUNCTION elif completion.type == 'function': kind = sublime.KIND_FUNCTION @@ -274,7 +297,6 @@ async def fetch(): elif completion.type == 'snippet': kind = sublime.KIND_SNIPPET else: kind = sublime.KIND_VARIABLE - item = sublime.CompletionItem.command_completion( trigger=completion.text or completion.label, annotation=completion.detail or '', @@ -286,9 +308,13 @@ async def fetch(): ) items.append(item) + except core.Error as e: core.debug('Unable to fetch completions:', e) + except core.CancelledError: + ... + completions.set_completions(items, sublime.INHIBIT_EXPLICIT_COMPLETIONS|sublime.INHIBIT_REORDER|sublime.INHIBIT_WORD_COMPLETIONS) fetch() @@ -314,8 +340,8 @@ def clear_input_mode(self): if input := self.input(): self.view.erase_regions('input') self.edit(lambda edit: self.view.erase(edit, sublime.Region(input.a, self.view.size()))) + def set_input_mode(self): - if input := self.input(): sel = self.view.sel() end_of_input = input.b @@ -360,10 +386,32 @@ def enter(self): self.on_input(text) self.write(':' + text, 'comment', True) - + self._history_offset = 0 + self._history.append(text) return True + def autofill(self, offset: int): + self._history_offset += offset + self._history_offset = min(max(0, self._history_offset), len(self._history)) + self.set_input_mode() + input = self.input() + if not input: return False + + text_region = sublime.Region(input.b, self.view.size()) + if self._history_offset: + self.edit(lambda edit: ( + self.view.replace(edit, text_region, self._history[-self._history_offset]), + self.view.sel().clear(), + self.view.sel().add(self.view.size()) + )) + else: + self.edit(lambda edit: ( + self.view.erase(edit, text_region), + self.view.sel().clear(), + self.view.sel().add(self.view.size()) + )) + def log(self, type: str, value: Any): if type == 'transport': self.protocol.log(type, value) diff --git a/modules/debugger_output_panel.py b/modules/debugger_output_panel.py index 388b664..1326f84 100644 --- a/modules/debugger_output_panel.py +++ b/modules/debugger_output_panel.py @@ -300,7 +300,7 @@ def on_post_text_command(self, view: sublime.View, command_name: str, args: Any) return panel.on_post_text_command(command_name, args) def on_query_context(self, view: sublime.View, key: str, operator: int, operand: Any, match_all: bool) -> bool|None: - if key != 'debugger': + if not key.startswith('debugger.'): return None if panel := DebuggerOutputPanel.panels.get(view.id()):