diff options
author | mitchell <70453897+667e-11@users.noreply.github.com> | 2020-07-25 10:24:09 -0400 |
---|---|---|
committer | mitchell <70453897+667e-11@users.noreply.github.com> | 2020-07-25 10:24:09 -0400 |
commit | 8fd378c09636e3c9a2275dcd1b9195a21a38e5de (patch) | |
tree | a54508d4df6723a11b7c212709f0cc032765976c | |
parent | c8074b47b7826d7018f4984fa1de1d0f6d19e83d (diff) |
Moved word highlighting back into editing module and disable by default.
Also fixed `HIGHLIGHT_SELECTED` behavior with non-word selections.
-rw-r--r-- | core/ui.lua | 53 | ||||
-rw-r--r-- | init.lua | 5 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 53 | ||||
-rw-r--r-- | test/test.lua | 26 | ||||
-rw-r--r-- | themes/dark.lua | 4 | ||||
-rw-r--r-- | themes/light.lua | 7 | ||||
-rw-r--r-- | themes/term.lua | 2 |
7 files changed, 75 insertions, 75 deletions
diff --git a/core/ui.lua b/core/ui.lua index 7aa15070..a9c4618e 100644 --- a/core/ui.lua +++ b/core/ui.lua @@ -33,26 +33,9 @@ local ui = ui -- with a group of [`ui.print()`]() and [`ui._print()`]() function calls. -- The default value is `false`, and focuses buffers when messages are printed -- to them. --- @field highlight_words (number) --- The word highlight mode. --- --- * `ui.HIGHLIGHT_CURRENT` --- Automatically highlight all instances of the current word. --- * `ui.HIGHLIGHT_SELECTED` --- Automatically highlight all instances of the selected word. --- * `ui.HIGHLIGHT_NONE` --- Do not automatically highlight words. --- --- The default value is `ui.HIGHLIGHT_SELECTED` except in the terminal --- version, where it is `ui.HIGHLIGHT_NONE`. --- @field INDIC_HIGHLIGHT (number) --- The word highlight indicator number. module('ui')]] ui.silent_print = false -ui.HIGHLIGHT_NONE, ui.HIGHLIGHT_CURRENT, ui.HIGHLIGHT_SELECTED = 1, 2, 3 -ui.highlight_words = not CURSES and ui.HIGHLIGHT_SELECTED or ui.HIGHLIGHT_NONE -ui.INDIC_HIGHLIGHT = _SCINTILLA.next_indic_number() -- Helper function for printing messages to buffers. -- @see ui._print @@ -300,42 +283,6 @@ function ui.goto_file(filename, split, preferred_view, sloppy) io.open_file(filename) end --- Clears highlighted word indicators. -local function clear_highlighted_words() - buffer.indicator_current = ui.INDIC_HIGHLIGHT - buffer:indicator_clear_range(1, buffer.length) -end -events_connect(events.KEYPRESS, function(code) - if keys.KEYSYMS[code] == 'esc' then clear_highlighted_words() end -end, 1) - --- Highlight all instances of the current or selected word. -events_connect(events.UPDATE_UI, function(updated) - if not updated or updated & buffer.UPDATE_SELECTION == 0 then return end - local word - if ui.highlight_words == ui.HIGHLIGHT_CURRENT then - local s = buffer:word_start_position(buffer.current_pos, true) - local e = buffer:word_end_position(buffer.current_pos, true) - if s == e then clear_highlighted_words() return end - word = buffer:text_range(s, e) - elseif ui.highlight_words == ui.HIGHLIGHT_SELECTED then - local s, e = buffer.selection_start, buffer.selection_end - if not buffer:is_range_word(s, e) then return end - word = buffer:get_sel_text() - if not word:find('^%S+$') then return end - else - return - end - clear_highlighted_words() - buffer.search_flags = buffer.FIND_MATCHCASE | buffer.FIND_WHOLEWORD - buffer:target_whole_document() - while buffer:search_in_target(word) ~= -1 do - buffer:indicator_fill_range( - buffer.target_start, buffer.target_end - buffer.target_start) - buffer:set_target_range(buffer.target_end, buffer.length + 1) - end -end) - -- Ensure title, statusbar, etc. are updated for new views. events_connect(events.VIEW_NEW, function() events.emit(events.UPDATE_UI, 3) end) @@ -265,8 +265,9 @@ if not CURSES then view.indic_under[ui.find.INDIC_FIND] = true end local INDIC_BRACEMATCH = textadept.editing.INDIC_BRACEMATCH view.indic_style[INDIC_BRACEMATCH] = view.INDIC_BOX view:brace_highlight_indicator(not CURSES, INDIC_BRACEMATCH) -view.indic_style[ui.INDIC_HIGHLIGHT] = view.INDIC_ROUNDBOX -if not CURSES then view.indic_under[ui.INDIC_HIGHLIGHT] = true end +local INDIC_HIGHLIGHT = textadept.editing.INDIC_HIGHLIGHT +view.indic_style[INDIC_HIGHLIGHT] = view.INDIC_ROUNDBOX +if not CURSES then view.indic_under[INDIC_HIGHLIGHT] = true end local INDIC_PLACEHOLDER = textadept.snippets.INDIC_PLACEHOLDER view.indic_style[INDIC_PLACEHOLDER] = not CURSES and view.INDIC_DOTBOX or view.INDIC_STRAIGHTBOX diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 941d96e6..c10f77c0 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -15,14 +15,30 @@ local M = {} -- Autocomplete the current word using words from all open buffers. -- If `true`, performance may be slow when many buffers are open. -- The default value is `false`. +-- @field highlight_words (number) +-- The word highlight mode. +-- +-- * `textadept.editing.HIGHLIGHT_CURRENT` +-- Automatically highlight all instances of the current word. +-- * `textadept.editing.HIGHLIGHT_SELECTED` +-- Automatically highlight all instances of the selected word. +-- * `textadept.editing.HIGHLIGHT_NONE` +-- Do not automatically highlight words. +-- +-- The default value is `textadept.editing.HIGHLIGHT_NONE`. -- @field INDIC_BRACEMATCH (number) -- The matching brace highlight indicator number. +-- @field INDIC_HIGHLIGHT (number) +-- The word highlight indicator number. module('textadept.editing')]] M.auto_indent = true M.strip_trailing_spaces = false M.autocomplete_all_words = false +M.HIGHLIGHT_NONE, M.HIGHLIGHT_CURRENT, M.HIGHLIGHT_SELECTED = 1, 2, 3 +M.highlight_words = M.HIGHLIGHT_NONE M.INDIC_BRACEMATCH = _SCINTILLA.next_indic_number() +M.INDIC_HIGHLIGHT = _SCINTILLA.next_indic_number() --- -- Map of image names to registered image numbers. @@ -163,6 +179,43 @@ events.connect(events.UPDATE_UI, function(updated) view:brace_bad_light(-1) end) +-- Clears highlighted word indicators. +local function clear_highlighted_words() + buffer.indicator_current = M.INDIC_HIGHLIGHT + buffer:indicator_clear_range(1, buffer.length) +end +events.connect(events.KEYPRESS, function(code) + if keys.KEYSYMS[code] == 'esc' then clear_highlighted_words() end +end, 1) + +-- Highlight all instances of the current or selected word. +events.connect(events.UPDATE_UI, function(updated) + if not updated or updated & buffer.UPDATE_SELECTION == 0 then return end + local word + if M.highlight_words == M.HIGHLIGHT_CURRENT then + clear_highlighted_words() + local s = buffer:word_start_position(buffer.current_pos, true) + local e = buffer:word_end_position(buffer.current_pos, true) + if s == e then return end + word = buffer:text_range(s, e) + elseif M.highlight_words == M.HIGHLIGHT_SELECTED then + local s, e = buffer.selection_start, buffer.selection_end + if s ~= e then clear_highlighted_words() end + if not buffer:is_range_word(s, e) then return end + word = buffer:get_sel_text() + if word:find(string.format('[^%s]', buffer.word_chars)) then return end + else + return + end + buffer.search_flags = buffer.FIND_MATCHCASE | buffer.FIND_WHOLEWORD + buffer:target_whole_document() + while buffer:search_in_target(word) ~= -1 do + buffer:indicator_fill_range( + buffer.target_start, buffer.target_end - buffer.target_start) + buffer:set_target_range(buffer.target_end, buffer.length + 1) + end +end) + -- Moves over typeover characters when typed, taking multiple selections into -- account. events.connect(events.KEYPRESS, function(code) diff --git a/test/test.lua b/test/test.lua index 84e1c7e9..00ed70be 100644 --- a/test/test.lua +++ b/test/test.lua @@ -1806,9 +1806,9 @@ function test_editing_convert_indentation() buffer:close(true) end -function test_ui_highlight_word() +function test_editing_highlight_word() local function verify(indics) - local bit = 1 << ui.INDIC_HIGHLIGHT - 1 + local bit = 1 << textadept.editing.INDIC_HIGHLIGHT - 1 for _, pos in ipairs(indics) do local mask = buffer:indicator_all_on_for(pos) assert(mask & bit > 0, 'no indicator on line %d', buffer:line_from_position(pos)) @@ -1819,8 +1819,8 @@ function test_ui_highlight_word() if CURSES then events.emit(events.UPDATE_UI, buffer.UPDATE_SELECTION) end end - local highlight = ui.highlight_words - ui.highlight_words = ui.HIGHLIGHT_SELECTED + local highlight = textadept.editing.highlight_words + textadept.editing.highlight_words = textadept.editing.HIGHLIGHT_SELECTED buffer.new() buffer:append_text(table.concat({ 'foo', @@ -1842,26 +1842,26 @@ function test_ui_highlight_word() update() verify_foo() events.emit(events.KEYPRESS, not CURSES and 0xFF1B or 7) -- esc - local pos = buffer:indicator_end(ui.INDIC_HIGHLIGHT, 1) + local pos = buffer:indicator_end(textadept.editing.INDIC_HIGHLIGHT, 1) assert_equal(pos, 1) -- highlights cleared -- Verify turning off word highlighting. - ui.highlight_words = ui.HIGHLIGHT_NONE + textadept.editing.highlight_words = textadept.editing.HIGHLIGHT_NONE textadept.editing.select_word() update() - pos = buffer:indicator_end(ui.INDIC_HIGHLIGHT, 2) + pos = buffer:indicator_end(textadept.editing.INDIC_HIGHLIGHT, 2) assert_equal(pos, 1) -- no highlights - ui.highlight_words = ui.HIGHLIGHT_SELECTED -- reset + textadept.editing.highlight_words = textadept.editing.HIGHLIGHT_SELECTED -- reset -- Verify partial word selections do not highlight words. buffer:set_sel(1, 3) - pos = buffer:indicator_end(ui.INDIC_HIGHLIGHT, 2) + pos = buffer:indicator_end(textadept.editing.INDIC_HIGHLIGHT, 2) assert_equal(pos, 1) -- no highlights -- Verify multi-word selections do not highlight words. buffer:set_sel(buffer:position_from_line(LINE(3)), buffer.line_end_position[LINE(3)]) assert(buffer:is_range_word(buffer.selection_start, buffer.selection_end)) - pos = buffer:indicator_end(ui.INDIC_HIGHLIGHT, 2) + pos = buffer:indicator_end(textadept.editing.INDIC_HIGHLIGHT, 2) assert_equal(pos, 1) -- no highlights -- Verify current word highlighting. - ui.highlight_words = ui.HIGHLIGHT_CURRENT + textadept.editing.highlight_words = textadept.editing.HIGHLIGHT_CURRENT buffer:goto_pos(1) update() verify_foo() @@ -1876,10 +1876,10 @@ function test_ui_highlight_word() verify_foo() buffer:char_left() update() - pos = buffer:indicator_end(ui.INDIC_HIGHLIGHT, 2) + pos = buffer:indicator_end(textadept.editing.INDIC_HIGHLIGHT, 2) assert_equal(pos, 1) -- no highlights buffer:close(true) - ui.highlight_words = highlight -- reset + textadept.editing.highlight_words = highlight -- reset end function test_editing_filter_through() diff --git a/themes/dark.lua b/themes/dark.lua index 4d5d2087..d147a4d7 100644 --- a/themes/dark.lua +++ b/themes/dark.lua @@ -118,11 +118,11 @@ for i = buffer.MARKNUM_FOLDEREND, buffer.MARKNUM_FOLDEROPEN do -- fold margin end -- Indicators. -view.indic_fore[ui.INDIC_HIGHLIGHT] = colors.grey -view.indic_alpha[ui.INDIC_HIGHLIGHT] = 128 view.indic_fore[ui.find.INDIC_FIND] = colors.dark_yellow view.indic_alpha[ui.find.INDIC_FIND] = 255 view.indic_fore[textadept.editing.INDIC_BRACEMATCH] = colors.light_grey +view.indic_fore[textadept.editing.INDIC_HIGHLIGHT] = colors.orange +view.indic_alpha[textadept.editing.INDIC_HIGHLIGHT] = 128 view.indic_fore[textadept.snippets.INDIC_PLACEHOLDER] = colors.grey -- Call tips. diff --git a/themes/light.lua b/themes/light.lua index 70afaf9b..89be42c0 100644 --- a/themes/light.lua +++ b/themes/light.lua @@ -118,13 +118,12 @@ for i = buffer.MARKNUM_FOLDEREND, buffer.MARKNUM_FOLDEROPEN do -- fold margin end -- Indicators. -view.indic_fore[ui.INDIC_HIGHLIGHT] = colors.light_grey -view.indic_alpha[ui.INDIC_HIGHLIGHT] = 128 view.indic_fore[ui.find.INDIC_FIND] = colors.yellow view.indic_alpha[ui.find.INDIC_FIND] = 255 view.indic_fore[textadept.editing.INDIC_BRACEMATCH] = colors.grey -local INDIC_PLACEHOLDER = textadept.snippets.INDIC_PLACEHOLDER -view.indic_fore[INDIC_PLACEHOLDER] = colors.grey_black +view.indic_fore[textadept.editing.INDIC_HIGHLIGHT] = colors.orange +view.indic_alpha[textadept.editing.INDIC_HIGHLIGHT] = 128 +view.indic_fore[textadept.snippets.INDIC_PLACEHOLDER] = colors.grey_black -- Call tips. view.call_tip_fore_hlt = colors.light_blue diff --git a/themes/term.lua b/themes/term.lua index d9c67c78..ec2fd53a 100644 --- a/themes/term.lua +++ b/themes/term.lua @@ -75,8 +75,8 @@ view.marker_back[textadept.run.MARK_WARNING] = colors.yellow view.marker_back[textadept.run.MARK_ERROR] = colors.red -- Indicators. -view.indic_fore[ui.INDIC_HIGHLIGHT] = colors.yellow view.indic_fore[ui.find.INDIC_FIND] = colors.yellow +view.indic_fore[textadept.editing.INDIC_HIGHLIGHT] = colors.yellow view.indic_fore[textadept.snippets.INDIC_PLACEHOLDER] = colors.magenta -- Call tips. |