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
650 history[#history + 1] = text650 history[#history + 1] = text
651end651end
652 652
653local 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
662end
663
653local function handle_edit()664local function handle_edit()
654 if selectable_items then665 if selectable_items then
655 matches = {}666 matches = {}
@@ -664,14 +675,7 @@ local function handle_edit() · 8 unmodified lines
664 return675 return
665 end676 end
666 677
667 -- Don't show completions after a command is entered because they move its678 handle_cursor_move()
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
675 679
676 if input_caller then680 if input_caller then
677 mp.commandv('script-message-to', input_caller, 'input-event', 'edited',681 mp.commandv('script-message-to', input_caller, 'input-event', 'edited',
@@ -714,15 +718,13 @@ end · 36 unmodified lines
714-- Move the cursor to the next character (Right)718-- Move the cursor to the next character (Right)
715local function next_char()719local function next_char()
716 cursor = next_utf8(line, cursor)720 cursor = next_utf8(line, cursor)
717 suggestion_buffer = {}721 handle_cursor_move()
718 update()
719end722end
720 723
721-- Move the cursor to the previous character (Left)724-- Move the cursor to the previous character (Left)
722local function prev_char()725local function prev_char()
723 cursor = prev_utf8(line, cursor)726 cursor = prev_utf8(line, cursor)
724 suggestion_buffer = {}727 handle_cursor_move()
725 update()
726end728end
727 729
728-- Clear the current line (Ctrl+C)730-- Clear the current line (Ctrl+C)
@@ -801,7 +803,7 @@ local function handle_enter() · 72 unmodified lines
801 line = #matches > 0 and matches[selected_match].text or ''803 line = #matches > 0 and matches[selected_match].text or ''
802 cursor = #line + 1804 cursor = #line + 1
803 log_buffers[id] = {}805 log_buffers[id] = {}
804 update()806 handle_edit()
805 unbind_mouse()807 unbind_mouse()
806 return808 return
807 end809 end
@@ -918,8 +920,7 @@ local function go_history(new_pos) · 110 unmodified lines
918 end920 end
919 cursor = line:len() + 1921 cursor = line:len() + 1
920 insert_mode = false922 insert_mode = false
921 suggestion_buffer = {}923 handle_edit()
922 update()
923end924end
924 925
925-- Go to the specified relative position in the command history (Up, Down)926-- Go to the specified relative position in the command history (Up, Down)
@@ -1035,30 +1036,26 @@ local function prev_word() · 109 unmodified lines
1035 -- string in order to do a "backwards" find. This wouldn't be as annoying1036 -- string in order to do a "backwards" find. This wouldn't be as annoying
1036 -- to do if Lua didn't insist on 1-based indexing.1037 -- to do if Lua didn't insist on 1-based indexing.
1037 cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 11038 cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 1
1038 suggestion_buffer = {}1039 handle_cursor_move()
1039 update()
1040end1040end
1041 1041
1042-- Move to the end of the current word, or if already at the end, the end of1042-- Move to the end of the current word, or if already at the end, the end of
1043-- the next word. (Ctrl+Right)1043-- the next word. (Ctrl+Right)
1044local function next_word()1044local function next_word()
1045 cursor = select(2, line:find('%s*[^%s]*', cursor)) + 11045 cursor = select(2, line:find('%s*[^%s]*', cursor)) + 1
1046 suggestion_buffer = {}1046 handle_cursor_move()
1047 update()
1048end1047end
1049 1048
1050-- Move the cursor to the beginning of the line (HOME)1049-- Move the cursor to the beginning of the line (HOME)
1051local function go_home()1050local function go_home()
1052 cursor = 11051 cursor = 1
1053 suggestion_buffer = {}1052 handle_cursor_move()
1054 update()
1055end1053end
1056 1054
1057-- Move the cursor to the end of the line (END)1055-- Move the cursor to the end of the line (END)
1058local function go_end()1056local function go_end()
1059 cursor = line:len() + 11057 cursor = line:len() + 1
1060 suggestion_buffer = {}1058 handle_cursor_move()
1061 update()
1062end1059end
1063 1060
1064-- Delete from the cursor to the beginning of the word (Ctrl+Backspace)1061-- 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