randy3k / Terminus

Bring a real terminal to Sublime Text
https://packagecontrol.io/packages/Terminus
MIT License
1.37k stars 83 forks source link

autocomplete suggestion #407

Open ta946 opened 7 months ago

ta946 commented 7 months ago

I couldnt get autocomplete working with terminus. i assume its because of the insert method.

I've come up with a hacky workaround and figured id share incase it helps someone.

When the custom autocomplete command is run, it copies the word at the terminus cursor to an input panel and shows sublime's autocomplete list. Then you can select the autocomplete and modify as usual. Then pressing enter will delete the word from terminal and insert the finalized autocompletion

i added the following keybind { "keys": ["ctrl+space"], "command": "terminus_autocomplete", "context": [{"key": "terminus_view"}] }

and created teminus_autocomplete.py in Packages/User folder

import sublime
import sublime_plugin

class TerminusAutocompleteCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if not self.view.settings().get("terminus_view"):
            return
        self.view.run_command("terminus_show_cursor")
        if len(self.view.sel()) != 1:
            return
        self._do_delete_word = False
        word = ''
        region = self.view.word(self.view.sel()[0])
        if not region.empty():
            word = self.view.substr(region)
            if word in ('>', '$'):
                self._do_delete_word = True
            else:
                word = ''
        input_panel_view = self.view.window().show_input_panel('terminus autocomplete', word, self.on_done, self.on_change, self.on_cancel)
        input_panel_view.settings().set('syntax', 'Packages/User/shell.tmLanguage')
        if word:
            input_panel_view.run_command("auto_complete")

    def on_change(self, value):
        pass

    def on_cancel(self):
        pass

    def on_done(self, value):
        if not value:
            return
        if self._do_delete_word:
            self.view.run_command("terminus_delete_word")
        self.view.run_command("terminus_paste_text", args={"text": value})

i have my snippet scope as source.shell so i set the input_panel syntax to shell.tmLanguage to limit the autocompletes. but thats not a required step