fix: Lineno alignment, hl_mode, config options

- Config
  - Renamed auto_goto -> auto_jump (because goto implies change of
    cursor focus)
  - Renamed down/up_and_goto -> down/up_and_jump

Existing config that use the old keys *WILL STILL WORK*. But users are
recommended to update to avoid inconsistency. I promise the number of
config changes like this will decrease inverse-exponentially after the
plugin refactor :)

- Docs
  - Improved wording

- Lineno
  - Fixed alignment (no way I was taking max line num of the *Outline*
    buf this whole time!)
  - Fixed appearance of lineno column hl blending if hide_cursor is one
    (please see the comment added)
This commit is contained in:
hedy
2023-11-13 18:07:52 +08:00
parent fd22263c32
commit 1446bdd135
4 changed files with 110 additions and 68 deletions

View File

@@ -27,7 +27,7 @@ M.defaults = {
wrap = false,
focus_on_open = true,
auto_close = false,
auto_goto = false,
auto_jump = false,
show_numbers = false,
show_relative_numbers = false,
show_cursorline = true,
@@ -67,8 +67,8 @@ M.defaults = {
fold_all = 'W',
unfold_all = 'E',
fold_reset = 'R',
down_and_goto = '<C-j>',
up_and_goto = '<C-k>',
down_and_jump = '<C-j>',
up_and_jump = '<C-k>',
},
providers = {
lsp = {
@@ -195,21 +195,41 @@ function M.show_help()
print(vim.inspect(M.o.keymaps))
end
---Check for inconsistent or mutually exclusive opts. Does not alter the opts
function M.check_config()
if M.o.outline_window.hide_cursor and not M.o.outline_window.show_cursorline then
vim.notify("[outline.config]: hide_cursor enabled WITHOUT cursorline enabled!", vim.log.levels.ERROR)
end
end
---Resolve shortcuts and deprecated option conversions
function M.resolve_config()
local sc = M.o.outline_window.split_command
if not sc then
return
if sc then
-- This should not be needed, nor is it failsafe. But in case user only provides
-- the, eg, "topleft", we append the ' vs'.
if not sc:find(' vs', 1, true) then
M.o.outline_window.split_command = sc..' vs'
end
end
-- This should not be needed, nor is it failsafe. But in case user only provides
-- the, eg, "topleft", we append the ' vs'.
if not sc:find(' vs', 1, true) then
M.o.outline_window.split_command = sc..' vs'
local dg = M.o.keymaps.down_and_goto
local ug = M.o.keymaps.up_and_goto
if dg then
M.o.keymaps.down_and_jump = dg
M.o.keymaps.down_and_goto = nil
end
if ug then
M.o.keymaps.up_and_jump = ug
M.o.keymaps.up_and_goto = nil
end
-- if dg or ug then
-- vim.notify("[outline.config]: keymaps down/up_and_goto are renamed to down/up_and_jump. Your keymaps for the current session is converted successfully.", vim.log.levels.WARN)
-- end
if M.o.outline_window.auto_goto then
M.o.outline_window.auto_jump = M.o.outline_window.auto_goto
M.o.outline_window.auto_goto = nil
end
end

View File

@@ -58,7 +58,7 @@ local function wipe_state()
end
local function _update_lines()
M.state.flattened_outline_items = writer.make_outline(M.view.bufnr, M.state.outline_items)
M.state.flattened_outline_items = writer.make_outline(M.view.bufnr, M.state.outline_items, M.state.code_win)
end
---@param items outline.SymbolNode[]
@@ -127,7 +127,7 @@ function M._goto_and_close()
end
---@param direction "up"|"down"
function M._move_and_goto(direction)
function M._move_and_jump(direction)
local move = direction == 'down' and 1 or -1
local cur = vim.api.nvim_win_get_cursor(0)
cur[1] = cur[1] + move
@@ -171,7 +171,7 @@ local function setup_buffer_autocmd()
callback = require('outline.preview').close,
})
end
if cfg.o.outline_window.auto_goto then
if cfg.o.outline_window.auto_jump then
vim.api.nvim_create_autocmd('CursorMoved', {
buffer = 0,
callback = function()
@@ -327,12 +327,12 @@ local function setup_keymaps(bufnr)
-- Navigate to corresponding outline location for current code location
map(cfg.o.keymaps.goto_and_close, M._goto_and_close)
-- Move down/up in outline and peek that location in code
map(cfg.o.keymaps.down_and_goto, function()
M._move_and_goto('down')
map(cfg.o.keymaps.down_and_jump, function()
M._move_and_jump('down')
end)
-- Move down/up in outline and peek that location in code
map(cfg.o.keymaps.up_and_goto, function()
M._move_and_goto('up')
map(cfg.o.keymaps.up_and_jump, function()
M._move_and_jump('up')
end)
-- hover symbol
map(
@@ -412,7 +412,7 @@ local function handler(response, opts)
local items = parser.parse(response)
M.state.outline_items = items
M.state.flattened_outline_items = writer.make_outline(M.view.bufnr, items)
M.state.flattened_outline_items = writer.make_outline(M.view.bufnr, items, M.state.code_win)
M._highlight_current_item(M.state.code_win)

View File

@@ -101,10 +101,12 @@ end
---@param bufnr integer Nothing is done if is_buffer_outline(bufnr) is not true
---@param items outline.SymbolNode[] Tree of symbols after being parsed by parser.parse_result
---@return outline.FlatSymbolNode[]? flattened_items No return value if bufnr is invalid
function M.make_outline(bufnr, items)
---@param codewin integer code window
function M.make_outline(bufnr, items, codewin)
if not M.is_buffer_outline(bufnr) then
return
end
local codebuf = vim.api.nvim_win_get_buf(codewin)
clear_virt_text(bufnr)
@@ -121,7 +123,7 @@ function M.make_outline(bufnr, items)
-- Find the prefix for each line needed for the lineno space
local lineno_offset = 0
local lineno_prefix = ""
local lineno_max_width = #tostring(vim.api.nvim_buf_line_count(bufnr) - 1)
local lineno_max_width = #tostring(vim.api.nvim_buf_line_count(codebuf) - 1)
if cfg.o.outline_items.show_symbol_lineno then
-- Use max width-1 plus 1 space padding.
-- -1 because if max_width is a power of ten, don't shift the entire lineno
@@ -250,13 +252,19 @@ function M.make_outline(bufnr, items)
if cfg.o.outline_items.show_symbol_lineno then
-- Line numbers are left padded, right aligned, positioned at the leftmost
-- column
-- TODO: Fix lineno not appearing if text in line is truncated on the right
-- due to narrow window, after nvim fixes virt_text_hide.
for index, value in ipairs(linenos) do
vim.api.nvim_buf_set_extmark(bufnr, ns, index - 1, -1, {
virt_text = { { value, 'OutlineLineno' } },
virt_text_pos = 'overlay',
virt_text_win_col = 0,
virt_text_hide = true,
hl_mode = 'replace',
-- When hide_cursor + cursorline enabled, we want the lineno to also
-- take on the cursorline background so wherever the cursor is, it
-- appears blended. We want 'replace' even for `hide_cursor=false
-- cursorline=true` because vim's native line numbers do not get
-- highlighted by cursorline.
hl_mode = (cfg.o.outline_window.hide_cursor and 'combine') or 'replace',
})
end
end