echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.45k stars 171 forks source link

mini.eolmark to show eol mark at cursor line? #990

Closed aidancz closed 1 week ago

aidancz commented 1 week ago

Contributing guidelines

Module(s)

mini.eolmark

Description

When using a block cursor, a visible eol mark can help distinguish between spaces and eol

But with:

set list
set listchars+=eol:$

The eol of each line is displayed, which makes the text messy

Displaying the eol mark only at cursor line would be nice

So I write this little script:

local eol_mark = vim.api.nvim_create_augroup('eol_mark', {clear = true})

local eol_mark_ns_id = vim.api.nvim_create_namespace('eol_mark')

function eol_mark_clear_and_set()
    vim.api.nvim_buf_clear_namespace(0, eol_mark_ns_id, 0, -1)
    local line = vim.api.nvim_win_get_cursor(0)[1] - 1
    -- local col = #vim.api.nvim_get_current_line()
    local opts = {
        virt_text = {{'○', 'Comment'}},
        virt_text_pos = 'overlay',
        }
    vim.api.nvim_buf_set_extmark(0, eol_mark_ns_id, line, -1, opts)
end

vim.api.nvim_create_autocmd(
    {'BufEnter', 'CursorMoved', 'CursorMovedI'}, {
    group = eol_mark,
    pattern = {'*'},
    -- callback = time
    callback = eol_mark_clear_and_set
    })

Will mini consider adding this module?

echasnovski commented 1 week ago

Thanks for the suggestion!

Although I do find it useful for some use cases, this doesn't look like a good fit for a separate module. I'll think about maybe adding it in some capacity to 'mini.misc', but currently I have doubts about this.

echasnovski commented 1 week ago

After sleeping on it, I don't think this is both non-trivial and useful UI to be added to 'mini.nvim' (either as a separate module or as part of 'mini.misc').

Closing as not planned.


Here is how I'd rewrite the code for a slightly better performance:

local eol_mark_ns_id = vim.api.nvim_create_namespace('eol_mark')
local extmark_opts = { virt_text = { { '○', 'Comment' } }, virt_text_pos = 'overlay' }
local eol_extmark_id
local show_eol_at_cursor_line = function(args)
  if vim.api.nvim_get_current_buf() ~= args.buf then return end
  extmark_opts.id = eol_extmark_id
  local line = vim.fn.line('.') - 1
  eol_extmark_id = vim.api.nvim_buf_set_extmark(args.buf, eol_mark_ns_id, line, -1, extmark_opts)
end

local eol_mark_augroup = vim.api.nvim_create_augroup('eol_mark', { clear = true })
vim.api.nvim_create_autocmd(
  { 'BufEnter', 'CursorMoved', 'CursorMovedI' },
  { group = eol_mark_augroup, callback = show_eol_at_cursor_line }
)

For further optimization, there is caching of current buffer-line pair and skipping doing something if current buffer-line pair is the same. This would account for a very frequent cursor movement within same line.