diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d88b6a..8266049b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ Introduction of a new module. ## mini.jump2d - FEATURE: Add `view.n_steps_ahead` option which controls how many steps ahead to show. Appearance is controlled by new `MiniJump2dSpotAhead` highlight group. +- FEATURE: Add `view.dim` option which controls whether to dim lines with at least one jump spot. Appearance is controlled by new `MiniJump2dDim` highlight group. ## mini.pairs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a570cc18..9cb3faf7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -103,6 +103,7 @@ Here is a list of all highlight groups defined inside 'mini.nvim' modules. See d - `MiniJump` - 'mini.jump2d': + - `MiniJump2dDim` - `MiniJump2dSpot` - `MiniJump2dSpotAhead` diff --git a/doc/mini-jump2d.txt b/doc/mini-jump2d.txt index 7fe6bd8e..169f0480 100644 --- a/doc/mini-jump2d.txt +++ b/doc/mini-jump2d.txt @@ -15,7 +15,7 @@ Features: - Customizable (see |MiniJump2d.config|): - Way of computing possible jump spots with opinionated default. - Characters used to label jump spots during iterative filtering. - - Visual effects: how many steps ahead to show. + - Visual effects: how many steps ahead to show; dim lines with spots. - Action hooks to be executed at certain events during jump. - Allowed windows: current and/or not current. - Allowed lines: whether to process blank or folded lines, lines @@ -114,6 +114,10 @@ To stop module from showing non-error feedback, set `config.silent = true`. * `MiniJump2dSpotAhead` - highlighting of jump spot's future steps. By default similar to `MiniJump2dSpot` but with less contrast and visibility. +* `MiniJump2dDim` - highlighting of lines with at least one jump spot. + Make it non-bright in order for jump spot labels to be more visible. + By default linked to `Comment` highlight group. + To change any highlight group, modify it directly with |:highlight|. # Disabling~ @@ -153,6 +157,9 @@ Default values: -- Options for visual effects view = { + -- Whether to dim lines with at least one jump spot + dim = false, + -- How many steps ahead to show. Set to big number to show all steps. n_steps_ahead = 0, }, @@ -217,6 +224,9 @@ it is a good idea to use this with a spotter which doesn't result into many jump spots (like, for example, |MiniJump2d.builtin_opts.word_start|). Default is 0 to not show anything ahead as it reduces visual noise. +Option `view.dim` contols whether to dim lines with at least one jump spot. +Dimming is done by applying "MiniJump2dDim" highlight group to the whol line. + ## Allowed lines~ Option `allowed_lines` controls which lines will be used for computing @@ -295,6 +305,8 @@ Parameters~ Default: "MiniJump2dSpot". - - which highlight group to use for second step and later. Default: "MiniJump2dSpotAhead". + - - which highlight group to use dimming used lines. + Default: "MiniJump2dSpotDim". Usage~ - Start default jumping: diff --git a/lua/mini/jump2d.lua b/lua/mini/jump2d.lua index 07ee772b..f0d8b2da 100644 --- a/lua/mini/jump2d.lua +++ b/lua/mini/jump2d.lua @@ -15,7 +15,7 @@ --- - Customizable (see |MiniJump2d.config|): --- - Way of computing possible jump spots with opinionated default. --- - Characters used to label jump spots during iterative filtering. ---- - Visual effects: how many steps ahead to show. +--- - Visual effects: how many steps ahead to show; dim lines with spots. --- - Action hooks to be executed at certain events during jump. --- - Allowed windows: current and/or not current. --- - Allowed lines: whether to process blank or folded lines, lines @@ -114,6 +114,10 @@ --- * `MiniJump2dSpotAhead` - highlighting of jump spot's future steps. By default --- similar to `MiniJump2dSpot` but with less contrast and visibility. --- +--- * `MiniJump2dDim` - highlighting of lines with at least one jump spot. +--- Make it non-bright in order for jump spot labels to be more visible. +--- By default linked to `Comment` highlight group. +--- --- To change any highlight group, modify it directly with |:highlight|. --- --- # Disabling~ @@ -174,6 +178,8 @@ MiniJump2d.setup = function(config) vim.cmd(main_hl_cmd) vim.cmd(ahead_hl_cmd) + vim.cmd('hi default link MiniJump2dDim Comment') + local augroup_hl_cmd = string.format( [[augroup MiniJump2d au ColorScheme * %s @@ -219,6 +225,9 @@ end --- jump spots (like, for example, |MiniJump2d.builtin_opts.word_start|). --- Default is 0 to not show anything ahead as it reduces visual noise. --- +--- Option `view.dim` contols whether to dim lines with at least one jump spot. +--- Dimming is done by applying "MiniJump2dDim" highlight group to the whol line. +--- --- ## Allowed lines~ --- --- Option `allowed_lines` controls which lines will be used for computing @@ -250,6 +259,9 @@ MiniJump2d.config = { -- Options for visual effects view = { + -- Whether to dim lines with at least one jump spot + dim = false, + -- How many steps ahead to show. Set to big number to show all steps. n_steps_ahead = 0, }, @@ -339,6 +351,8 @@ MiniJump2d.config = { --- Default: "MiniJump2dSpot". --- - - which highlight group to use for second step and later. --- Default: "MiniJump2dSpotAhead". +--- - - which highlight group to use dimming used lines. +--- Default: "MiniJump2dSpotDim". --- ---@usage - Start default jumping: --- `MiniJump2d.start()` @@ -372,6 +386,7 @@ MiniJump2d.start = function(opts) opts.spotter = opts.spotter or MiniJump2d.default_spotter opts.hl_group = opts.hl_group or 'MiniJump2dSpot' opts.hl_group_ahead = opts.hl_group_ahead or 'MiniJump2dSpotAhead' + opts.hl_group_dim = opts.hl_group_dim or 'MiniJump2dDim' local spots = H.spots_compute(opts) local label_tbl = vim.split(opts.labels, '') @@ -590,6 +605,7 @@ H.default_config = MiniJump2d.config -- Namespaces to be used withing module H.ns_id = { + dim = vim.api.nvim_create_namespace('MiniJump2dDim'), spots = vim.api.nvim_create_namespace('MiniJump2dSpots'), input = vim.api.nvim_create_namespace('MiniJump2dInput'), } @@ -633,6 +649,7 @@ H.setup_config = function(config) }) vim.validate({ + ['view.dim'] = { config.view.dim, 'boolean' }, ['view.n_steps_ahead'] = { config.view.n_steps_ahead, 'number' }, ['allowed_lines.blank'] = { config.allowed_lines.blank, 'boolean' }, @@ -765,6 +782,10 @@ H.spots_show = function(spots, opts) return end + local set_extmark = vim.api.nvim_buf_set_extmark + + -- Add extmark with proper virtual text to jump spots + local dim_buf_lines = {} for _, extmark in ipairs(H.spots_to_extmarks(spots, opts)) do local extmark_opts = { hl_mode = 'combine', @@ -773,7 +794,24 @@ H.spots_show = function(spots, opts) virt_text = extmark.virt_text, virt_text_pos = 'overlay', } - pcall(vim.api.nvim_buf_set_extmark, extmark.buf_id, H.ns_id.spots, extmark.line, extmark.col, extmark_opts) + local buf_id, line = extmark.buf_id, extmark.line + pcall(set_extmark, buf_id, H.ns_id.spots, line, extmark.col, extmark_opts) + + -- Register lines to dim + local lines = dim_buf_lines[buf_id] or {} + lines[line] = true + dim_buf_lines[buf_id] = lines + end + + -- Possibly dim used lines + if opts.view.dim then + local extmark_opts = { end_col = 0, hl_eol = true, hl_group = opts.hl_group_dim, priority = 999 } + for buf_id, lines in pairs(dim_buf_lines) do + for _, l_num in ipairs(vim.tbl_keys(lines)) do + extmark_opts.end_line = l_num + 1 + pcall(set_extmark, buf_id, H.ns_id.dim, l_num, 0, extmark_opts) + end + end end -- Need to redraw in Operator-pending mode, because otherwise extmarks won't @@ -792,6 +830,7 @@ H.spots_unshow = function(spots) for _, buf_id in ipairs(vim.tbl_keys(buf_ids)) do pcall(vim.api.nvim_buf_clear_namespace, buf_id, H.ns_id.spots, 0, -1) + pcall(vim.api.nvim_buf_clear_namespace, buf_id, H.ns_id.dim, 0, -1) end end diff --git a/readmes/mini-jump2d.md b/readmes/mini-jump2d.md index 3ce6dc5c..31d85907 100644 --- a/readmes/mini-jump2d.md +++ b/readmes/mini-jump2d.md @@ -156,6 +156,9 @@ Here are code snippets for some common installation methods (use only one): -- Options for visual effects view = { + -- Whether to dim lines with at least one jump spot + dim = false, + -- How many steps ahead to show. Set to big number to show all steps. n_steps_ahead = 0, }, diff --git a/tests/screenshots/tests-test_jump2d.lua---start()---respects-`opts.hl_group_dim` b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`opts.hl_group_dim` new file mode 100644 index 00000000..6d0ff47f --- /dev/null +++ b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`opts.hl_group_dim` @@ -0,0 +1,13 @@ +-|---------|-- +1|jxxk +2| +3|dimx +4|< [+] 1,1 +5| + +-|---------|-- +1|011011111111 +2|222222222222 +3|333222222222 +4|444444444444 +5|555555555555 diff --git a/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim` b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim` new file mode 100644 index 00000000..07fe4631 --- /dev/null +++ b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim` @@ -0,0 +1,13 @@ +-|---------|-- +1|jxxj +2|k +3|xxxx +4|< [+] 1,1 +5| + +-|---------|-- +1|011011111111 +2|011111111111 +3|222222222222 +4|333333333333 +5|444444444444 diff --git a/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim`-002 b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim`-002 new file mode 100644 index 00000000..1af92e43 --- /dev/null +++ b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim`-002 @@ -0,0 +1,13 @@ +-|---------|-- +1|jxxk +2| +3|xxxx +4|< [+] 1,1 +5| + +-|---------|-- +1|011011111111 +2|222222222222 +3|222222222222 +4|333333333333 +5|444444444444 diff --git a/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim`-003 b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim`-003 new file mode 100644 index 00000000..1af92e43 --- /dev/null +++ b/tests/screenshots/tests-test_jump2d.lua---start()---respects-`view.dim`-003 @@ -0,0 +1,13 @@ +-|---------|-- +1|jxxk +2| +3|xxxx +4|< [+] 1,1 +5| + +-|---------|-- +1|011011111111 +2|222222222222 +3|222222222222 +4|333333333333 +5|444444444444 diff --git a/tests/screenshots/tests-test_jump2d.lua---stop()---clears-all-highlighting b/tests/screenshots/tests-test_jump2d.lua---stop()---clears-all-highlighting new file mode 100644 index 00000000..746f2762 --- /dev/null +++ b/tests/screenshots/tests-test_jump2d.lua---stop()---clears-all-highlighting @@ -0,0 +1,15 @@ +-|---------|-- +1|axxb +2|cxxd +3| +4|~ +5|< [+] 1,1 +6| + +-|---------|-- +1|011011111111 +2|011011111111 +3|222222222222 +4|333333333333 +5|444444444444 +6|555555555555 diff --git a/tests/screenshots/tests-test_jump2d.lua---stop()---clears-all-highlighting-002 b/tests/screenshots/tests-test_jump2d.lua---stop()---clears-all-highlighting-002 new file mode 100644 index 00000000..771288c1 --- /dev/null +++ b/tests/screenshots/tests-test_jump2d.lua---stop()---clears-all-highlighting-002 @@ -0,0 +1,15 @@ +-|---------|-- +1|xxxx +2|xxxx +3| +4|~ +5|< [+] 1,1 +6| + +-|---------|-- +1|000000000000 +2|000000000000 +3|000000000000 +4|111111111111 +5|222222222222 +6|333333333333 diff --git a/tests/screenshots/tests-test_jump2d.lua---stop()---works b/tests/screenshots/tests-test_jump2d.lua---stop()---works index 5ca6e22e..234d0351 100644 --- a/tests/screenshots/tests-test_jump2d.lua---stop()---works +++ b/tests/screenshots/tests-test_jump2d.lua---stop()---works @@ -1,51 +1,23 @@ ---|---------|---------|---------|---------|---------|---------|---------|---------| -01|axxb -02|cxxd -03|~ -04|~ -05|~ -06|~ -07|~ -08|~ -09|~ -10|~ -11|~ -12|~ -13|~ -14|~ -15|~ -16|~ -17|~ -18|~ -19|~ -20|~ -21|~ -22|~ -23|[No Name] [+] 1,1 All -24| +--|---------|-- +01|axxb +02|cxxd +03|~ +04|~ +05|~ +06|~ +07|~ +08|~ +09|< [+] 1,1 +10| ---|---------|---------|---------|---------|---------|---------|---------|---------| -01|01101111111111111111111111111111111111111111111111111111111111111111111111111111 -02|01101111111111111111111111111111111111111111111111111111111111111111111111111111 -03|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -04|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -05|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -06|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -07|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -08|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -09|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -10|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -11|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -12|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -13|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -14|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -15|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -16|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -17|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -18|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -19|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -20|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -21|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -22|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -23|33333333333333333333333333333333333333333333333333333333333333333333333333333333 -24|11111111111111111111111111111111111111111111111111111111111111111111111111111111 +--|---------|-- +01|011011111111 +02|011011111111 +03|222222222222 +04|222222222222 +05|222222222222 +06|222222222222 +07|222222222222 +08|222222222222 +09|333333333333 +10|444444444444 diff --git a/tests/screenshots/tests-test_jump2d.lua---stop()---works-002 b/tests/screenshots/tests-test_jump2d.lua---stop()---works-002 index 90b901ff..8aff0e18 100644 --- a/tests/screenshots/tests-test_jump2d.lua---stop()---works-002 +++ b/tests/screenshots/tests-test_jump2d.lua---stop()---works-002 @@ -1,51 +1,23 @@ ---|---------|---------|---------|---------|---------|---------|---------|---------| -01|xxxx -02|xxxx -03|~ -04|~ -05|~ -06|~ -07|~ -08|~ -09|~ -10|~ -11|~ -12|~ -13|~ -14|~ -15|~ -16|~ -17|~ -18|~ -19|~ -20|~ -21|~ -22|~ -23|[No Name] [+] 1,1 All -24| +--|---------|-- +01|xxxx +02|xxxx +03|~ +04|~ +05|~ +06|~ +07|~ +08|~ +09|< [+] 1,1 +10| ---|---------|---------|---------|---------|---------|---------|---------|---------| -01|00000000000000000000000000000000000000000000000000000000000000000000000000000000 -02|00000000000000000000000000000000000000000000000000000000000000000000000000000000 -03|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -04|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -05|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -06|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -07|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -08|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -09|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -10|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -11|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -12|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -13|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -14|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -15|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -16|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -17|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -18|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -19|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -20|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -21|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -22|11111111111111111111111111111111111111111111111111111111111111111111111111111111 -23|22222222222222222222222222222222222222222222222222222222222222222222222222222222 -24|00000000000000000000000000000000000000000000000000000000000000000000000000000000 +--|---------|-- +01|000000000000 +02|000000000000 +03|111111111111 +04|111111111111 +05|111111111111 +06|111111111111 +07|111111111111 +08|111111111111 +09|222222222222 +10|333333333333 diff --git a/tests/test_jump2d.lua b/tests/test_jump2d.lua index c54422be..a346ac39 100644 --- a/tests/test_jump2d.lua +++ b/tests/test_jump2d.lua @@ -122,7 +122,7 @@ T['setup()']['creates `config` field'] = function() expect_config('spotter', vim.NIL) expect_config('labels', 'abcdefghijklmnopqrstuvwxyz') - expect_config('view', { n_steps_ahead = 0 }) + expect_config('view', { dim = false, n_steps_ahead = 0 }) expect_config( 'allowed_lines', { blank = true, cursor_before = true, cursor_at = true, cursor_after = true, fold = true } @@ -150,6 +150,7 @@ T['setup()']['validates `config` argument'] = function() expect_config_error({ spotter = 'a' }, 'spotter', 'function') expect_config_error({ labels = 1 }, 'labels', 'string') expect_config_error({ view = 1 }, 'view', 'table') + expect_config_error({ view = { dim = 'a' } }, 'view.dim', 'boolean') expect_config_error({ view = { n_steps_ahead = 'a' } }, 'view.n_steps_ahead', 'number') expect_config_error({ allowed_lines = 'a' }, 'allowed_lines', 'table') expect_config_error({ allowed_lines = { blank = 1 } }, 'allowed_lines.blank', 'boolean') @@ -491,6 +492,25 @@ T['start()']['respects `labels`'] = function() child.expect_screenshot() end +T['start()']['respects `view.dim`'] = function() + -- In argument + start({ labels = 'jk', view = { dim = true } }) + + type_keys('j') + -- Whole lines with at least one jump spot should be highlighted differently + child.expect_screenshot() + + type_keys('j') + child.expect_screenshot() + child.lua('MiniJump2d.stop()') + + -- In global config + child.lua('MiniJump2d.config.view.dim = true') + start({ labels = 'jk' }) + type_keys('j', 'j') + child.expect_screenshot() +end + T['start()']['respects `view.n_steps_ahead`'] = function() -- In argument start({ labels = 'jk', view = { n_steps_ahead = 1 } }) @@ -750,6 +770,46 @@ T['start()']['respects `opts.hl_group_ahead`'] = function() validate_hl_group('MiniJump2dSpot', 'Search') end +T['start()']['uses `MiniJump2dDim` highlight group by default'] = function() + local validate = function(line_numbers) + local ns_id = child.api.nvim_get_namespaces()['MiniJump2dDim'] + local extmarks = child.api.nvim_buf_get_extmarks(0, ns_id, 0, -1, { details = true }) + + for i, e_mark in ipairs(extmarks) do + eq(e_mark[2], line_numbers[i] - 1) + end + end + + start({ labels = 'jk', view = { dim = true } }) + validate({ 1, 2, 3 }) + + type_keys('j') + validate({ 1, 2 }) + + type_keys('j') + validate({ 1 }) +end + +T['start()']['respects `opts.hl_group_dim`'] = function() + -- At the moment, `nvim_buf_get_extmarks(..., { details = true })` doesn't + -- return highlight group specified via `opts.line_hl_group`. So instead test + -- directly by comparing highlighting in screenshot (should be different). + local ns_id = child.api.nvim_create_namespace('test-dim') + --stylua: ignore + child.api.nvim_buf_set_extmark( + 0, ns_id, 2, 0, + { hl_mode = 'combine', virt_text_pos = 'overlay', virt_text = { { 'dim', 'MiniJump2dDim' } } } + ) + + start({ labels = 'jk', view = { dim = true }, hl_group_dim = 'Search' }) + type_keys('j', 'j') + + -- Highlighting for first and second whole lines and first three columns of + -- third line should be different: first is 'Search', second is 'Normal', + -- third is 'MiniJump2dDim'. + child.expect_screenshot() +end + T['start()']['respects `vim.{g,b}.minijump2d_disable`'] = new_set({ parametrize = { { 'g' }, { 'b' } }, }, { @@ -776,6 +836,8 @@ end T['stop()'] = new_set() T['stop()']['works'] = function() + child.set_size(10, 12) + set_lines({ 'xxxx', 'xxxx' }) child.lua('MiniJump2d.start()') child.expect_screenshot() @@ -784,6 +846,17 @@ T['stop()']['works'] = function() child.expect_screenshot() end +T['stop()']['clears all highlighting'] = function() + child.set_size(6, 12) + + set_lines({ 'xxxx', 'xxxx', '' }) + start({ view = { dim = true, n_steps_ahead = 1 }, allowed_lines = { blank = false } }) + child.expect_screenshot() + + child.lua('MiniJump2d.stop()') + child.expect_screenshot() +end + T['stop()']['works even if not jumping'] = function() -- Shouldn't move local init_cursor = get_cursor()