<s> import sublime <EOL> import sublime_plugin <EOL> from vex . ex_command_parser import parse_command <EOL> from vex . ex_command_parser import EX_COMMANDS <EOL> from vex import ex_error <EOL> COMPLETIONS = sorted ( [ x [ <NUM_LIT:0> ] for x in EX_COMMANDS . keys ( ) ] ) <EOL> EX_HISTORY_MAX_LENGTH = <NUM_LIT:20> <EOL> EX_HISTORY = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> def update_command_line_history ( item , slot_name ) : <EOL> if len ( EX_HISTORY [ slot_name ] ) >= EX_HISTORY_MAX_LENGTH : <EOL> EX_HISTORY [ slot_name ] = EX_HISTORY [ slot_name ] [ <NUM_LIT:1> : ] <EOL> if item in EX_HISTORY [ slot_name ] : <EOL> EX_HISTORY [ slot_name ] . pop ( EX_HISTORY [ slot_name ] . index ( item ) ) <EOL> EX_HISTORY [ slot_name ] . append ( item ) <EOL> class ViColonInput ( sublime_plugin . WindowCommand ) : <EOL> def is_enabled ( self ) : <EOL> return len ( self . window . views ( ) ) > <NUM_LIT:0> <EOL> def __init__ ( self , window ) : <EOL> sublime_plugin . WindowCommand . __init__ ( self , window ) <EOL> def run ( self , initial_text = '<STR_LIT::>' , cmd_line = '<STR_LIT>' ) : <EOL> if cmd_line : <EOL> self . non_interactive = True <EOL> self . on_done ( cmd_line ) <EOL> return <EOL> v = self . window . show_input_panel ( '<STR_LIT>' , initial_text , <EOL> self . on_done , None , None ) <EOL> v . set_syntax_file ( '<STR_LIT>' ) <EOL> v . settings ( ) . set ( '<STR_LIT>' , False ) <EOL> v . settings ( ) . set ( '<STR_LIT>' , [ ] ) <EOL> def on_done ( self , cmd_line ) : <EOL> if not getattr ( self , '<STR_LIT>' , None ) : <EOL> update_command_line_history ( cmd_line , '<STR_LIT>' ) <EOL> else : <EOL> self . non_interactive = False <EOL> ex_cmd = parse_command ( cmd_line ) <EOL> print ex_cmd <EOL> if ex_cmd and ex_cmd . parse_errors : <EOL> ex_error . display_error ( ex_cmd . parse_errors [ <NUM_LIT:0> ] ) <EOL> return <EOL> if ex_cmd and ex_cmd . name : <EOL> if ex_cmd . can_have_range : <EOL> ex_cmd . args [ "<STR_LIT>" ] = ex_cmd . line_range <EOL> if ex_cmd . forced : <EOL> ex_cmd . args [ '<STR_LIT>' ] = ex_cmd . forced <EOL> self . window . run_command ( ex_cmd . command , ex_cmd . args ) <EOL> else : <EOL> ex_error . display_error ( ex_error . ERR_UNKNOWN_COMMAND , cmd_line ) <EOL> class ViColonRepeatLast ( sublime_plugin . WindowCommand ) : <EOL> def is_enabled ( self ) : <EOL> return ( len ( self . window . views ( ) ) > <NUM_LIT:0> ) and ( len ( EX_HISTORY [ '<STR_LIT>' ] ) > <NUM_LIT:0> ) <EOL> def run ( self ) : <EOL> self . window . run_command ( '<STR_LIT>' , { '<STR_LIT>' : EX_HISTORY [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] } ) <EOL> class ExCompletionsProvider ( sublime_plugin . EventListener ) : <EOL> CACHED_COMPLETIONS = [ ] <EOL> CACHED_COMPLETION_PREFIXES = [ ] <EOL> def on_query_completions ( self , view , prefix , locations ) : <EOL> if view . score_selector ( <NUM_LIT:0> , '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> if len ( prefix ) + <NUM_LIT:1> != view . size ( ) : <EOL> return [ ] <EOL> if prefix and prefix in self . CACHED_COMPLETION_PREFIXES : <EOL> return self . CACHED_COMPLETIONS <EOL> compls = [ x for x in COMPLETIONS if x . startswith ( prefix ) ] <EOL> self . CACHED_COMPLETION_PREFIXES = [ prefix ] + compls <EOL> self . CACHED_COMPLETIONS = zip ( [ prefix ] + compls , compls + [ prefix ] ) <EOL> return self . CACHED_COMPLETIONS <EOL> class CycleCmdlineHistory ( sublime_plugin . TextCommand ) : <EOL> HISTORY_INDEX = None <EOL> def run ( self , edit , backwards = False ) : <EOL> if CycleCmdlineHistory . HISTORY_INDEX is None : <EOL> CycleCmdlineHistory . HISTORY_INDEX = - <NUM_LIT:1> if backwards else <NUM_LIT:0> <EOL> else : <EOL> CycleCmdlineHistory . HISTORY_INDEX += - <NUM_LIT:1> if backwards else <NUM_LIT:1> <EOL> if CycleCmdlineHistory . HISTORY_INDEX == len ( EX_HISTORY [ '<STR_LIT>' ] ) or CycleCmdlineHistory . HISTORY_INDEX < - len ( EX_HISTORY [ '<STR_LIT>' ] ) : <EOL> CycleCmdlineHistory . HISTORY_INDEX = - <NUM_LIT:1> if backwards else <NUM_LIT:0> <EOL> self . view . erase ( edit , sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> self . view . insert ( edit , <NUM_LIT:0> , EX_HISTORY [ '<STR_LIT>' ] [ CycleCmdlineHistory . HISTORY_INDEX ] ) <EOL> class HistoryIndexRestorer ( sublime_plugin . EventListener ) : <EOL> def on_deactivated ( self , view ) : <EOL> if view . score_selector ( <NUM_LIT:0> , '<STR_LIT>' ) > <NUM_LIT:0> : <EOL> CycleCmdlineHistory . HISTORY_INDEX = None </s>
