Skip to content

Commit

Permalink
move up/down in the debugger console now autofills previously entered…
Browse files Browse the repository at this point in the history
… commands
  • Loading branch information
daveleroy committed Oct 16, 2022
1 parent de885ac commit 9125cd7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
62 changes: 55 additions & 7 deletions modules/debugger_console_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -244,21 +247,41 @@ 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

text = self.view.substr(sublime.Region(input.b, self.view.size()))
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
Expand All @@ -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 '',
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion modules/debugger_output_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand Down

0 comments on commit 9125cd7

Please sign in to comment.