indentLine icon indicating copy to clipboard operation
indentLine copied to clipboard

Highlight empty lines in neovim

Open notano opened this issue 6 years ago • 7 comments

Highlight empty lines are maybe a greatly desired option. In neovim, you can highlight empty lines by using nvim_buf_set_virtual_text, can't you?

notano avatar Aug 04 '19 13:08 notano

Is this something that's going to get implemented? It seems like something that should exist

kioplato avatar Nov 01 '19 16:11 kioplato

I was thinking the same and wanted to experiment stuff but it seems not possible currently: https://github.com/neovim/neovim/blob/master/runtime/doc/api.txt#L2016

                 By default (and currently the only option) the text will be
                 placed after the buffer text.
                 ^^^^^^^^^^^^

ohcibi avatar Apr 02 '20 18:04 ohcibi

i didn't even see this issue, but i was trying to do the same thing as well. I came up with this

let g:pretty_indent_namespace = nvim_create_namespace('pretty_indent')

function! PrettyIndent()
    let l:view=winsaveview()
    call cursor(1, 1)
    call nvim_buf_clear_namespace(0, g:pretty_indent_namespace, 1, -1)
    while 1
        let l:match = search('^$', 'W')
        if l:match ==# 0
            break
        endif
        let l:indent = cindent(l:match)
        if l:indent > 0
            call nvim_buf_set_virtual_text(
            \   0,
            \   g:pretty_indent_namespace,
            \   l:match - 1,
            \   [[repeat(repeat(' ', &shiftwidth - 1) . '│', l:indent / &shiftwidth), 'IndentGuide']],
            \   {}
            \)
        endif
    endwhile
    call winrestview(l:view)
endfunction

augroup PrettyIndent
    autocmd!
    autocmd TextChanged * call PrettyIndent()
    autocmd BufEnter * call PrettyIndent()
    autocmd InsertLeave * call PrettyIndent()
augroup END

There are probably edgecases It doesn't work for wrapped lines, thats a limitation of virtual text It only works if cindent is working properly.

DeepinScreenshot_select-area_20200409102637

lukas-reineke avatar Apr 09 '20 08:04 lukas-reineke

I've been playing with this a bit more

I can get solid results with the indentexpr, but its too slow to run synchronously.

I have a working prototype to run it async in an embedded neovim instance, but its a bit hacky. It just opens the file again and sends results back. Maybe its possible to use nvim_buf_attach, but i'm not sure how.

Also its a bit ridiculous to run a new neovim instance just to fill in blank line indentation, but :man_shrugging:

Are you interested to integrate this here? If not i'll make it a standalone plugin

lukas-reineke avatar Apr 10 '20 05:04 lukas-reineke

I made it a new standalone plugin for now

https://github.com/lukas-reineke/indent-blankline.nvim

lukas-reineke avatar Apr 13 '20 07:04 lukas-reineke

I just enter space and save the file and it works for empty lines too :p

image

siduck avatar Dec 14 '20 13:12 siduck

I made it a new standalone plugin for now

https://github.com/lukas-reineke/indent-blankline.nvim

Is this going to be implemented in indentline? For now I'll use yours

ndavidpsi avatar Dec 23 '20 14:12 ndavidpsi