diff options
Diffstat (limited to 'lua/lsp')
| -rw-r--r-- | lua/lsp/init.lua | 98 | ||||
| -rw-r--r-- | lua/lsp/keybinds.lua | 67 | ||||
| -rw-r--r-- | lua/lsp/null-ls.lua | 113 | ||||
| -rw-r--r-- | lua/lsp/service.lua | 122 | 
4 files changed, 185 insertions, 215 deletions
| diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index 62c42fd8..67007e81 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -1,55 +1,79 @@ -local utils = require "utils" -local service = require "lsp.service" -local null_ls = require "lsp.null-ls"  local M = {} - +local u = require "utils"  function M.config()    require("lsp.kind").setup()    require("lsp.handlers").setup()    require("lsp.signs").setup() -  require("lsp.keybinds").setup()  end -function M.setup(lang) -  local lang_server = lvim.lang[lang].lsp -  local provider = lang_server.provider -  if utils.check_lsp_client_active(provider) then -    return +local function lsp_highlight_document(client) +  if lvim.lsp.document_highlight == false then +    return -- we don't need further +  end +  -- Set autocommands conditional on server_capabilities +  if client.resolved_capabilities.document_highlight then +    vim.api.nvim_exec( +      [[ +      hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646 +      hi LspReferenceText cterm=bold ctermbg=red guibg=#464646 +      hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646 +      augroup lsp_document_highlight +        autocmd! * <buffer> +        autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() +        autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references() +      augroup END +    ]], +      false +    )    end +end -  local overrides = lvim.lsp.override +local function formatter_handler(client) +  local buffer_filetype = vim.bo.filetype +  local ext_provider = lvim.lang[buffer_filetype].formatter.exe -  if utils.is_table(overrides) then -    if utils.has_value(overrides, lang) then -      return -    end +  if ext_provider then +    client.resolved_capabilities.document_formatting = false +    u.lvim_log( +      string.format("Overriding [%s] formatting if exists, Using provider [%s] instead", client.name, ext_provider) +    )    end +end -  if utils.is_string(overrides) then -    if overrides == lang then -      return -    end +function M.common_capabilities() +  local capabilities = vim.lsp.protocol.make_client_capabilities() +  capabilities.textDocument.completion.completionItem.snippetSupport = true +  capabilities.textDocument.completion.completionItem.resolveSupport = { +    properties = { +      "documentation", +      "detail", +      "additionalTextEdits", +    }, +  } +  return capabilities +end + +function M.common_on_init(client, bufnr) +  if lvim.lsp.on_init_callback then +    lvim.lsp.on_init_callback(client, bufnr) +    return    end -  local sources = null_ls.setup(lang) - -  for _, source in pairs(sources) do -    local method = source.method -    local format_method = "NULL_LS_FORMATTING" - -    if utils.is_table(method) then -      if utils.has_value(method, format_method) then -        lang_server.setup.on_attach = service.no_formatter_on_attach -      end -    end - -    if utils.is_string(method) then -      if method == format_method then -        lang_server.setup.on_attach = service.no_formatter_on_attach -      end -    end +  formatter_handler(client) +end + +function M.common_on_attach(client, bufnr) +  if lvim.lsp.on_attach_callback then +    lvim.lsp.on_attach_callback(client, bufnr)    end +  lsp_highlight_document(client) +  require("lsp.keybinds").setup() +  require("lsp.null-ls").setup(vim.bo.filetype) +end -  if provider == "" or provider == nil then +function M.setup(lang) +  local lang_server = lvim.lang[lang].lsp +  local provider = lang_server.provider +  if require("utils").check_lsp_client_active(provider) then      return    end diff --git a/lua/lsp/keybinds.lua b/lua/lsp/keybinds.lua index 820ebce9..cc0d4ec9 100644 --- a/lua/lsp/keybinds.lua +++ b/lua/lsp/keybinds.lua @@ -1,5 +1,70 @@  local M = {} +-- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ +function M.preview_location(location, context, before_context) +  -- location may be LocationLink or Location (more useful for the former) +  context = context or 15 +  before_context = before_context or 0 +  local uri = location.targetUri or location.uri +  if uri == nil then +    return +  end +  local bufnr = vim.uri_to_bufnr(uri) +  if not vim.api.nvim_buf_is_loaded(bufnr) then +    vim.fn.bufload(bufnr) +  end + +  local range = location.targetRange or location.range +  local contents = vim.api.nvim_buf_get_lines( +    bufnr, +    range.start.line - before_context, +    range["end"].line + 1 + context, +    false +  ) +  local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") +  return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border }) +end + +function M.preview_location_callback(_, method, result) +  local context = 15 +  if result == nil or vim.tbl_isempty(result) then +    print("No location found: " .. method) +    return nil +  end +  if vim.tbl_islist(result) then +    M.floating_buf, M.floating_win = M.preview_location(result[1], context) +  else +    M.floating_buf, M.floating_win = M.preview_location(result, context) +  end +end + +function M.PeekDefinition() +  if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then +    vim.api.nvim_set_current_win(M.floating_win) +  else +    local params = vim.lsp.util.make_position_params() +    return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback) +  end +end + +function M.PeekTypeDefinition() +  if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then +    vim.api.nvim_set_current_win(M.floating_win) +  else +    local params = vim.lsp.util.make_position_params() +    return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback) +  end +end + +function M.PeekImplementation() +  if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then +    vim.api.nvim_set_current_win(M.floating_win) +  else +    local params = vim.lsp.util.make_position_params() +    return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback) +  end +end +  function M.setup()    if lvim.lsp.default_keybinds then      vim.cmd "nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>" @@ -13,7 +78,7 @@ function M.setup()        { noremap = true, silent = true }      ) -    vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.service'.PeekDefinition()<CR>" +    vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.keybinds'.PeekDefinition()<CR>"      vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"      vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<CR>"      vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<CR>" diff --git a/lua/lsp/null-ls.lua b/lua/lsp/null-ls.lua index 6a31de26..d2222602 100644 --- a/lua/lsp/null-ls.lua +++ b/lua/lsp/null-ls.lua @@ -1,76 +1,79 @@  local M = {} +local u = require "utils" +local null_ls = require "null-ls" -local _, null_ls = pcall(require, "null-ls") -local utils = require "utils" -local sources = {} +local nodejs_local_providers = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } -local local_executables = { "prettier", "prettierd", "prettier_d_slim", "eslint_d", "eslint" } +M.requested_providers = {} -local find_local_exe = function(exe) -  vim.cmd "let root_dir = FindRootDirectory()" -  local root_dir = vim.api.nvim_get_var "root_dir" -  local local_exe = root_dir .. "/node_modules/.bin/" .. exe -  return local_exe +local function is_nodejs_provider(provider) +  for _, local_provider in ipairs(nodejs_local_providers) do +    if local_provider == provider.exe then +      return true +    end +  end +  return false  end --- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint -local get_normalized_exe = function(exe, type) -  if type == "diagnostics" and exe == "eslint_d" then -    return "eslint" +local function is_provider_found(provider) +  -- special case: fallback to "eslint" +  -- https://github.com/jose-elias-alvarez/null-ls.nvim/blob/9b8458bd1648e84169a7e8638091ba15c2f20fc0/doc/BUILTINS.md#eslint +  provider._opts.command = provider._opts.command == "eslint_d" and "eslint" or provider._opts.command + +  local retval = { is_local = false, path = nil } +  if vim.fn.executable(provider._opts.command) == 1 then +    return false, provider._opts.command    end -  return exe +  if is_nodejs_provider(provider) then +    vim.cmd "let root_dir = FindRootDirectory()" +    local root_dir = vim.api.nvim_get_var "root_dir" +    local local_provider_command = root_dir .. "/node_modules/.bin/" .. provider._opts.command +    if vim.fn.executable(local_provider_command) == 1 then +      retval.is_local = true +      retval.path = local_provider_command +    end +  end +  return retval.is_local, retval.path  end -local function setup_ls(exe, type) -  if utils.has_value(local_executables, exe) then -    local normalized_exe = get_normalized_exe(exe, type) -    local smart_executable = null_ls.builtins[type][normalized_exe] -    local local_executable = find_local_exe(exe) -    if vim.fn.executable(local_executable) == 1 then -      smart_executable._opts.command = local_executable -      table.insert(sources, smart_executable) -    else -      if vim.fn.executable(exe) == 1 then -        smart_executable._opts.command = exe -        table.insert(sources, smart_executable) -      end -    end -  else -    if null_ls.builtins[type][exe] and vim.fn.executable(null_ls.builtins[type][exe]._opts.command) then -      table.insert(sources, null_ls.builtins[type][exe]) -    end +local function validate_provider(provider) +  local is_local, provider_path = is_provider_found(provider) +  if not provider_path then +    u.lvim_log(string.format("Unable to find the path for: [%s]", provider)) +    return false    end -  null_ls.register { sources = sources } +  if is_local then +    provider._opts.command = provider_path +  end +  return true  end  -- TODO: for linters and formatters with spaces and '-' replace with '_' -local function setup(filetype, type) -  local executables = nil -  if type == "diagnostics" then -    executables = lvim.lang[filetype].linters +function M.setup(filetype) +  for _, formatter in pairs(lvim.lang[filetype].formatters) do +    local builtin_formatter = null_ls.builtins.formatting[formatter.exe] +    -- FIXME: why doesn't this work? +    -- builtin_formatter._opts.args = formatter.args or builtin_formatter._opts.args +    -- builtin_formatter._opts.to_stdin = formatter.stdin or builtin_formatter._opts.to_stdin +    table.insert(M.requested_providers, builtin_formatter) +    u.lvim_log(string.format("Using format provider: [%s]", formatter.exe))    end -  if type == "formatting" then -    executables = lvim.lang[filetype].formatter.exe + +  for _, linter in pairs(lvim.lang[filetype].linters) do +    local builtin_diagnoser = null_ls.builtins.diagnostics[linter.exe] +    -- FIXME: why doesn't this work? +    -- builtin_diagnoser._opts.args = linter.args or builtin_diagnoser._opts.args +    -- builtin_diagnoser._opts.to_stdin = linter.stdin or builtin_diagnoser._opts.to_stdin +    table.insert(M.requested_providers, builtin_diagnoser) +    u.lvim_log(string.format("Using linter provider: [%s]", linter.exe))    end -  if utils.is_table(executables) then -    for _, exe in pairs(executables) do -      if exe ~= "" then -        setup_ls(exe, type) -      end +  for idx, provider in pairs(M.requested_providers) do +    if not validate_provider(provider) then +      table.remove(M.requested_providers, idx)      end    end -  if utils.is_string(executables) and executables ~= "" then -    setup_ls(executables, type) -  end -end - --- TODO: return the formatter if one was registered, then turn off the builtin formatter -function M.setup(filetype) -  setup(filetype, "formatting") -  setup(filetype, "diagnostics") -  lvim.sources = sources -  return sources +  null_ls.register { sources = M.requested_providers }  end  return M diff --git a/lua/lsp/service.lua b/lua/lsp/service.lua deleted file mode 100644 index 0c49bacd..00000000 --- a/lua/lsp/service.lua +++ /dev/null @@ -1,122 +0,0 @@ -local M = {} - -local function lsp_highlight_document(client) -  if lvim.lsp.document_highlight == false then -    return -- we don't need further -  end -  -- Set autocommands conditional on server_capabilities -  if client.resolved_capabilities.document_highlight then -    vim.api.nvim_exec( -      [[ -      hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646 -      hi LspReferenceText cterm=bold ctermbg=red guibg=#464646 -      hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646 -      augroup lsp_document_highlight -        autocmd! * <buffer> -        autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() -        autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references() -      augroup END -    ]], -      false -    ) -  end -end - -function M.lsp_highlight_document(client) -  lsp_highlight_document(client) -end - --- Taken from https://www.reddit.com/r/neovim/comments/gyb077/nvimlsp_peek_defination_javascript_ttserver/ -function M.preview_location(location, context, before_context) -  -- location may be LocationLink or Location (more useful for the former) -  context = context or 15 -  before_context = before_context or 0 -  local uri = location.targetUri or location.uri -  if uri == nil then -    return -  end -  local bufnr = vim.uri_to_bufnr(uri) -  if not vim.api.nvim_buf_is_loaded(bufnr) then -    vim.fn.bufload(bufnr) -  end - -  local range = location.targetRange or location.range -  local contents = vim.api.nvim_buf_get_lines( -    bufnr, -    range.start.line - before_context, -    range["end"].line + 1 + context, -    false -  ) -  local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") -  return vim.lsp.util.open_floating_preview(contents, filetype, { border = lvim.lsp.popup_border }) -end - -function M.preview_location_callback(_, method, result) -  local context = 15 -  if result == nil or vim.tbl_isempty(result) then -    print("No location found: " .. method) -    return nil -  end -  if vim.tbl_islist(result) then -    M.floating_buf, M.floating_win = M.preview_location(result[1], context) -  else -    M.floating_buf, M.floating_win = M.preview_location(result, context) -  end -end - -function M.PeekDefinition() -  if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then -    vim.api.nvim_set_current_win(M.floating_win) -  else -    local params = vim.lsp.util.make_position_params() -    return vim.lsp.buf_request(0, "textDocument/definition", params, M.preview_location_callback) -  end -end - -function M.PeekTypeDefinition() -  if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then -    vim.api.nvim_set_current_win(M.floating_win) -  else -    local params = vim.lsp.util.make_position_params() -    return vim.lsp.buf_request(0, "textDocument/typeDefinition", params, M.preview_location_callback) -  end -end - -function M.PeekImplementation() -  if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then -    vim.api.nvim_set_current_win(M.floating_win) -  else -    local params = vim.lsp.util.make_position_params() -    return vim.lsp.buf_request(0, "textDocument/implementation", params, M.preview_location_callback) -  end -end - -function M.common_on_attach(client, bufnr) -  if lvim.lsp.on_attach_callback then -    lvim.lsp.on_attach_callback(client, bufnr) -  end -  lsp_highlight_document(client) -end - -function M.no_formatter_on_attach(client, bufnr) -  if lvim.lsp.on_attach_callback then -    lvim.lsp.on_attach_callback(client, bufnr) -  end -  lsp_highlight_document(client) -  client.resolved_capabilities.document_formatting = false -end - -function M.common_capabilities() -  local capabilities = vim.lsp.protocol.make_client_capabilities() -  capabilities.textDocument.completion.completionItem.snippetSupport = true -  capabilities.textDocument.completion.completionItem.resolveSupport = { -    properties = { -      "documentation", -      "detail", -      "additionalTextEdits", -    }, -  } -  return capabilities -end - -return M | 
