Sublime Forum

How to autofill last typed text on "show_overlay"

#1

I tried it myself but I could not find a way to make it work.

What I want is: when I press Ctrl+P and typed something, when I press Ctrl+P for the second time I see the same text I typed before selected in order for me to either type another text or have the search results from the last time.

I Googled it for a while but I could not find something like this made before maybe I’m just looking in the wrong direction.

Here is an example of what am I trying to achieve.

1 Like

#2

https://packagecontrol.io/packages/RememberCommandPaletteInput does something similar, for the Command Palette. Maybe the author can adapt it to work with Goto Anything as well, if you ask them to.

0 Likes

#3

I made some progress from the plugin you sent me:

But I still have a problem whenever I hit enter or escape the EventListener therefore context is not triggered. That means the text is not saved. Any clues?
Key bindings:
{ “keys”: [“ctrl+shift+o”], “command”: “show_goto_overlay_with_selection”, “context”:
[
{ “key”: “show_goto_overlay_with_selection_context”, “operator”: “equal”, “operand”: “open” },
] },
{ “keys”: [“escape”], “command”: “fixed_hide_overlay_which_is_correctly_logged_goto”, “context”:
[

        { "key": "auto_complete_visible" },
        { "key": "show_goto_overlay_with_selection_context", "operator": "equal", "operand": "close" },
    ]
},
{ "keys": ["enter"], "command": "fixed_hide_overlay_which_is_correctly_logged_goto", "context":
    [
        { "key": "auto_complete_visible" },
        { "key": "show_goto_overlay_with_selection_context", "operator": "equal", "operand": "close" },
    ]
},

Code:

import sublime_plugin

widget_text = ""

goto_palette_states  = ("open", "close")
is_goto_palette_open = False

class ShowGotoOverlayWithSelectionCommand(sublime_plugin.WindowCommand):
    def run(self):
        global is_goto_palette_open
        global is_goto_just_closed
        print("open")
        
        if is_goto_palette_open:
            #print( "FixedCommandPaletteLastInputHistoryCommand, Command palette is already open, closing it..." )
            self.window.run_command( "select_all" )
            self.window.run_command( "fixed_command_palette_last_input_history_helper_goto" )
            self.window.run_command( "fixed_hide_overlay_which_is_correctly_logged_goto" )

        else:
            #print( "\nFixedCommandPaletteLastInputHistoryCommand, Opening fixed command palette..." )
            self.window.run_command( "show_overlay", {"overlay": "goto", "show_files":True } )
            self.window.run_command( "select_all" )

            self.window.run_command( "fixed_command_palette_last_input_history_helper_goto" )
            self.window.run_command( "select_all" )

            is_goto_palette_open = True
            #print(">>>" + widget_text + "<<<");

class FixedCommandPaletteLastInputHistoryHelperGotoCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        selections = self.view.sel()

        global widget_text
        current_widget_text = self.view.substr( selections[0] )

        #print("cwt >>>" + current_widget_text + "<<<");

        if len( current_widget_text ):
            widget_text = current_widget_text
            self.view.erase( edit, selections[0] )

        #print( "FixedCommandPaletteLastInputHistoryCommand, widget_text:         %r" % ( str( widget_text ) ) )
        #print( "FixedCommandPaletteLastInputHistoryCommand, current_widget_text: %r" % ( str( current_widget_text ) ) )

        self.view.run_command( "append", {"characters": widget_text} )


class ShowGotoOverlayWithSelectionEventListener(sublime_plugin.EventListener):

    def on_activated( self, view ):
        """
            Allow to open the command palette correctly after running a command by pressing enter.
            How to detect when the user closed the `goto_definition` box?
            https://forum.sublimetext.com/t/how-to-detect-when-the-user-closed-the-goto-definition-box/25800
        """
        # print( "FixedCommandPaletteLastInputHistoryEventListener, on_activated, Setting is_command_palette_open to False..." )

        global is_goto_palette_open
        is_goto_palette_open = False

    def on_query_context(self, view, key, operator, operand, match_all):

        print( "FixedCommandPaletteLastInputHistoryEventListener, operand: %5s, is_command_palette_open: %s" % ( operand, str( is_goto_palette_open ) ) )
        if key == "show_goto_overlay_with_selection_context":

            if operand in goto_palette_states:

                return operand == "open" \
                        or operand == "close" and is_goto_palette_open

            else:
                return not is_goto_palette_open

        return None

    def on_window_command(self, window, command_name, args):
        self.set_goto_palette_state(window, command_name, args)

    def on_text_command(self, window, command_name, args):
        self.set_goto_palette_state(window, command_name, args)

    def set_goto_palette_state(self, window, command_name, args):
        # print( "command_name: " + command_name )

        if command_name == "fixed_hide_overlay_which_is_correctly_logged_goto":
            #print( "FixedCommandPaletteLastInputHistoryEventListener, set_command_palette_state, Setting is_command_palette_open to False..." )

            global is_goto_palette_open
            is_goto_palette_open = False


class FixedHideOverlayWhichIsCorrectlyLoggedGotoCommand(sublime_plugin.WindowCommand):

    def run(self):
        print("closing panel")
        #print( "sssFixedCommandPaletteLastInputHistoryEventListener, set_command_palette_state, Setting is_command_palette_open to False..." )
        self.window.run_command( "hide_overlay" )
0 Likes