vim-lsc icon indicating copy to clipboard operation
vim-lsc copied to clipboard

Use the sign gutter for diagnostics

Open natebosch opened this issue 6 years ago • 1 comments

natebosch avatar Mar 13 '19 23:03 natebosch

Im using the following config to display both signs and right-aligned virtual text just in case anyone finds it useful:

" define signs
call sign_define("vim-lsc-error", {"text" : "x", "texthl" : "lscSignDiagnosticError"})
call sign_define("vim-lsc-warning", {"text" : "x", "texthl" : "lscSignDiagnosticWarning"})

" virtual text namespace
let s:namespace_id = nvim_create_namespace("vim-lsc")

" update diagnostic visualizations
function! s:updateDiagnosticVisuals() abort
    let buf_id = nvim_get_current_buf()
    let file_path = nvim_buf_get_name(buf_id)
    let diagnostics = lsc#diagnostics#forFile(file_path).ListItems()
    call s:setVirtualText(buf_id, diagnostics)
    call s:setSigns(buf_id, diagnostics)
endfunction

function! s:setVirtualText(buf_id, diagnostics) abort

    " clear previous virtual texts
    call nvim_buf_clear_namespace(a:buf_id, s:namespace_id, 0, -1)

    " add virtual texts
    for diagnostic in a:diagnostics
        let available_space = winwidth('%') - strwidth(getline(l:diagnostic['lnum'])) - 8
        let text = strwidth(l:diagnostic['text']) < l:available_space ?  repeat(" ", l:available_space - strwidth(l:diagnostic['text'])).l:diagnostic['text'] : l:diagnostic['text']
        let hl_group = l:diagnostic['type'] == 'E' ? 'lscVTDiagnosticError' : 'lscVTDiagnosticWarning'
        call nvim_buf_set_virtual_text(l:diagnostic['bufnr'], s:namespace_id, l:diagnostic['lnum']-1, [[l:text, l:hl_group]], {})
    endfor
endfunction

function! s:setSigns(buf_id, diagnostics) abort

    " clear previous virtual texts
    call sign_unplace('vim-lsc', {'buffer' : a:buf_id})

    " add signs
    for diagnostic in a:diagnostics
        if l:diagnostic['type'] == 'E'
            let sign = 'vim-lsc-error'
        elseif l:diagnostic['type'] == 'W'
            let sign = 'vim-lsc-warning'
        else 
            return
        endif
        call sign_place(1, 'vim-lsc', l:sign, l:diagnostic['bufnr'], {'lnum' : l:diagnostic['lnum']})
    endfor
endfunction

autocmd BufEnter * if has_key(g:lsc_server_commands, &filetype) | call s:updateDiagnosticVisuals() | endif
autocmd User LSCDiagnosticsChange call s:updateDiagnosticVisuals()

pwntester avatar Nov 13 '19 10:11 pwntester