Skip to content

Instantly share code, notes, and snippets.

@phelipetls
Last active December 9, 2022 18:21
Show Gist options
  • Select an option

  • Save phelipetls/0aeb9f4aca9af25d9f45ee56e0c5a340 to your computer and use it in GitHub Desktop.

Select an option

Save phelipetls/0aeb9f4aca9af25d9f45ee56e0c5a340 to your computer and use it in GitHub Desktop.

Revisions

  1. phelipetls revised this gist May 1, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions lsp.lua
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,8 @@ local parse_diagnostics = function(diagnostics)
    return items
    end

    -- redefine unwanted callbacks to be an empty function
    -- notice that I keep `vim.lsp.util.buf_diagnostics_underline()`
    vim.lsp.util.buf_diagnostics_signs = function() return end
    vim.lsp.util.buf_diagnostics_virtual_text = function() return end

  2. phelipetls revised this gist May 1, 2020. 2 changed files with 15 additions and 34 deletions.
    38 changes: 15 additions & 23 deletions lsp.lua
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,15 @@
    local severity_map = { "E", "W", "I", "H" }

    local parse_diagnostics = function(diagnostics)
    if not diagnostics then return end
    local items = {}
    for _, diagnostic in ipairs(diagnostics) do
    local fname = vim.uri_to_fname(diagnostic.uri)
    local fname = vim.fn.bufname()
    local position = diagnostic.range.start
    local severity = diagnostic.severity
    table.insert(items, {
    filename = fname,
    type = severity_map[severity],
    lnum = position.line + 1,
    col = position.character + 1,
    text = diagnostic.message:gsub("\r", ""):gsub("\n", " ")
    @@ -13,30 +18,17 @@ local parse_diagnostics = function(diagnostics)
    return items
    end

    local diagnostics_quickfix = function(_, _, result, _)
    if not result then return end

    local uri = result.uri
    local bufnr = vim.uri_to_bufnr(uri)
    vim.lsp.util.buf_diagnostics_signs = function() return end
    vim.lsp.util.buf_diagnostics_virtual_text = function() return end

    if not bufnr then
    err_message("LSP.publishDiagnostics: Couldn't find buffer for ", uri)
    return
    end
    update_diagnostics_loclist = function()
    bufnr = vim.fn.bufnr()
    diagnostics = vim.lsp.util.diagnostics_by_buf[bufnr]

    local diagnostics = result.diagnostics
    for _, v in ipairs(diagnostics) do
    v.uri = v.uri or result.uri
    end

    local items = parse_diagnostics(diagnostics)
    items = parse_diagnostics(diagnostics)
    vim.lsp.util.set_loclist(items)

    local ll = vim.fn.getloclist(bufnr)
    -- when errors are fixed, close loclist
    if ll and #ll == 0 then
    vim.api.nvim_command("lclose")
    end
    vim.api.nvim_command("doautocmd QuickFixCmdPost")
    end

    vim.api.nvim_command("autocmd! BufWritePost <buffer> Lwindow")
    end
    vim.api.nvim_command [[autocmd! User LspDiagnosticsChanged lua update_diagnostics_loclist()]]
    11 changes: 0 additions & 11 deletions lsp.vim
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    function! Lwindow()
    " open lwindow, if not empty
    try
    lwindow
    " ignore error if there isn't one
    catch /E776/
    return
    endtry
    endfunction

    command! Lwindow call Lwindow()
  3. phelipetls revised this gist Apr 11, 2020. 2 changed files with 18 additions and 24 deletions.
    31 changes: 7 additions & 24 deletions lsp.lua
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,4 @@
    local parse_diagnostics = function(diagnostics)
    -- parse diagnostics into a table to be passed
    -- to setqflist function of vim
    --
    -- check out :help setqflist
    local items = {}
    for _, diagnostic in ipairs(diagnostics) do
    local fname = vim.uri_to_fname(diagnostic.uri)
    @@ -34,26 +30,13 @@ local diagnostics_quickfix = function(_, _, result, _)
    end

    local items = parse_diagnostics(diagnostics)

    -- equivalent to vim.fn.setqflist({}, ' ', { title = "Language Server", items = items } )
    -- :help setqflist
    -- :help vim.fn
    vim.lsp.util.set_qflist(items)
    vim.lsp.util.set_loclist(items)

    -- when errors are fixed, close qflist
    local qf = vim.fn.getqflist({["size"] = 0})
    if qf["size"] == nil or qf["size"] == 0 then
    vim.api.nvim_command("cclose")
    local ll = vim.fn.getloclist(bufnr)
    -- when errors are fixed, close loclist
    if ll and #ll == 0 then
    vim.api.nvim_command("lclose")
    end

    -- open quick fix list on save
    vim.api.nvim_command("autocmd! BufWritePost <buffer> doautocmd QuickFixCmdPost")
    end

    -- set as callback to all servers
    vim.lsp.callbacks["textDocument/publishDiagnostics"] = diagnostics_quickfix

    -- set as callback to a specific lsp server
    nvim_lsp.tsserver.setup{
    callbacks = { ["textDocument/publishDiagnostics"] = diagnostics_quickfix };
    }
    vim.api.nvim_command("autocmd! BufWritePost <buffer> Lwindow")
    end
    11 changes: 11 additions & 0 deletions lsp.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    function! Lwindow()
    " open lwindow, if not empty
    try
    lwindow
    " ignore error if there isn't one
    catch /E776/
    return
    endtry
    endfunction

    command! Lwindow call Lwindow()
  4. phelipetls revised this gist Apr 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lsp.lua
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ end
    -- set as callback to all servers
    vim.lsp.callbacks["textDocument/publishDiagnostics"] = diagnostics_quickfix

    -- set callback to a specific lsp server
    -- set as callback to a specific lsp server
    nvim_lsp.tsserver.setup{
    callbacks = { ["textDocument/publishDiagnostics"] = diagnostics_quickfix };
    }
  5. phelipetls revised this gist Apr 9, 2020. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion lsp.lua
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    local parse_diagnostics = function(diagnostics)
    -- parse diagnostics into a table to be passed
    -- to setqflist function of vim
    --
    -- check out :help setqflist
    local items = {}
    for _, diagnostic in ipairs(diagnostics) do
    local fname = vim.uri_to_fname(diagnostic.uri)
    @@ -30,6 +34,10 @@ local diagnostics_quickfix = function(_, _, result, _)
    end

    local items = parse_diagnostics(diagnostics)

    -- equivalent to vim.fn.setqflist({}, ' ', { title = "Language Server", items = items } )
    -- :help setqflist
    -- :help vim.fn
    vim.lsp.util.set_qflist(items)

    -- when errors are fixed, close qflist
    @@ -40,4 +48,12 @@ local diagnostics_quickfix = function(_, _, result, _)

    -- open quick fix list on save
    vim.api.nvim_command("autocmd! BufWritePost <buffer> doautocmd QuickFixCmdPost")
    end
    end

    -- set as callback to all servers
    vim.lsp.callbacks["textDocument/publishDiagnostics"] = diagnostics_quickfix

    -- set callback to a specific lsp server
    nvim_lsp.tsserver.setup{
    callbacks = { ["textDocument/publishDiagnostics"] = diagnostics_quickfix };
    }
  6. phelipetls created this gist Apr 9, 2020.
    43 changes: 43 additions & 0 deletions lsp.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    local parse_diagnostics = function(diagnostics)
    local items = {}
    for _, diagnostic in ipairs(diagnostics) do
    local fname = vim.uri_to_fname(diagnostic.uri)
    local position = diagnostic.range.start
    table.insert(items, {
    filename = fname,
    lnum = position.line + 1,
    col = position.character + 1,
    text = diagnostic.message:gsub("\r", ""):gsub("\n", " ")
    })
    end
    return items
    end

    local diagnostics_quickfix = function(_, _, result, _)
    if not result then return end

    local uri = result.uri
    local bufnr = vim.uri_to_bufnr(uri)

    if not bufnr then
    err_message("LSP.publishDiagnostics: Couldn't find buffer for ", uri)
    return
    end

    local diagnostics = result.diagnostics
    for _, v in ipairs(diagnostics) do
    v.uri = v.uri or result.uri
    end

    local items = parse_diagnostics(diagnostics)
    vim.lsp.util.set_qflist(items)

    -- when errors are fixed, close qflist
    local qf = vim.fn.getqflist({["size"] = 0})
    if qf["size"] == nil or qf["size"] == 0 then
    vim.api.nvim_command("cclose")
    end

    -- open quick fix list on save
    vim.api.nvim_command("autocmd! BufWritePost <buffer> doautocmd QuickFixCmdPost")
    end