History

console.lua: update completions after moving the cursor

...and after navigating the history. I thought of making the new function accept the new cursor position and set it, but it would not be clear that you're only supposed to call it when handle_edit() is not already called.

Guido Cella committed 1 year ago

Commit e549bf01 parent 5730c631 file · +20 −23Browse files

Unified · Split

player/lua/console.lua+20−23
@@ -650,6 +650,17 @@ local function history_add(text) · 649 unmodified lines
650650 history[#history + 1] = text
651651 end
652652
653+local function handle_cursor_move()
654+ -- Don't show completions after a command is entered because they move its
655+ -- output up, and allow clearing completions by emptying the line.
656+ if line == '' then
657+ suggestion_buffer = {}
658+ update()
659+ else
660+ complete()
661+ end
662+end
663+
653664 local function handle_edit()
654665 if selectable_items then
655666 matches = {}
@@ -664,14 +675,7 @@ local function handle_edit() · 8 unmodified lines
664675 return
665676 end
666677
667− -- Don't show completions after a command is entered because they move its
668− -- output up, and allow clearing completions by emptying the line.
669− if line == '' then
670− suggestion_buffer = {}
671− update()
672− else
673− complete()
674− end
678+ handle_cursor_move()
675679
676680 if input_caller then
677681 mp.commandv('script-message-to', input_caller, 'input-event', 'edited',
@@ -714,15 +718,13 @@ end · 36 unmodified lines
714718 -- Move the cursor to the next character (Right)
715719 local function next_char()
716720 cursor = next_utf8(line, cursor)
717− suggestion_buffer = {}
718− update()
721+ handle_cursor_move()
719722 end
720723
721724 -- Move the cursor to the previous character (Left)
722725 local function prev_char()
723726 cursor = prev_utf8(line, cursor)
724− suggestion_buffer = {}
725− update()
727+ handle_cursor_move()
726728 end
727729
728730 -- Clear the current line (Ctrl+C)
@@ -801,7 +803,7 @@ local function handle_enter() · 72 unmodified lines
801803 line = #matches > 0 and matches[selected_match].text or ''
802804 cursor = #line + 1
803805 log_buffers[id] = {}
804− update()
806+ handle_edit()
805807 unbind_mouse()
806808 return
807809 end
@@ -918,8 +920,7 @@ local function go_history(new_pos) · 110 unmodified lines
918920 end
919921 cursor = line:len() + 1
920922 insert_mode = false
921− suggestion_buffer = {}
922− update()
923+ handle_edit()
923924 end
924925
925926 -- Go to the specified relative position in the command history (Up, Down)
@@ -1035,30 +1036,26 @@ local function prev_word() · 109 unmodified lines
10351036 -- string in order to do a "backwards" find. This wouldn't be as annoying
10361037 -- to do if Lua didn't insist on 1-based indexing.
10371038 cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 1
1038− suggestion_buffer = {}
1039− update()
1039+ handle_cursor_move()
10401040 end
10411041
10421042 -- Move to the end of the current word, or if already at the end, the end of
10431043 -- the next word. (Ctrl+Right)
10441044 local function next_word()
10451045 cursor = select(2, line:find('%s*[^%s]*', cursor)) + 1
1046− suggestion_buffer = {}
1047− update()
1046+ handle_cursor_move()
10481047 end
10491048
10501049 -- Move the cursor to the beginning of the line (HOME)
10511050 local function go_home()
10521051 cursor = 1
1053− suggestion_buffer = {}
1054− update()
1052+ handle_cursor_move()
10551053 end
10561054
10571055 -- Move the cursor to the end of the line (END)
10581056 local function go_end()
10591057 cursor = line:len() + 1
1060− suggestion_buffer = {}
1061− update()
1058+ handle_cursor_move()
10621059 end
10631060
10641061 -- Delete from the cursor to the beginning of the word (Ctrl+Backspace)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t