repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/lua/bugabinga/lsp.lua

Raw
local puk     = require 'std.puk'
local project = require 'std.project'
local dbg     = require 'std.dbg'
local icon    = require 'std.icon'
local map     = require 'std.map'
local auto    = require 'std.auto'
local table   = require 'std.table'
local localrc = require 'std.localrc'

if dbg.get() then
  vim.lsp.set_log_level('debug')
end

local language_servers = {
  lua_ls = {
    root_dir = project.find_lua_project_root,
    settings = {
      Lua = {
        hint = { enable = true, setType = true, },
        telemetry = { enable = false, },
      },
    },
  },
  jdtls = {
    root_dir = project.find_java_project_root,
    settings = {
      java = {
        signatureHelp = { enabled = true, },
        sources = {
          autobuild = { enabled = true, },
          signatureHelp = { enabled = true, },
          saveActions = { origanizeImports = true, },
          organizeImports = {
            starThreshold = 9999,
            staticStarThreshold = 9999,
          },
          codeGeneration = {
            toString = {
              template = '${object.className}{${member.name()}=${member.value()}, ${otherMembers}}',
            },
            hashCodeEquals = { useJava7Objects = true, },
            useBlocks = true,
          },
          eclipse = { downloadSources = true, },
        },
        configuration = {
          updateBuildConfiguration = 'interactive',
        },
        maven = { downloadSources = true, },
        implementationsCodeLens = { enabled = true, },
        referencesCodeLens = { enabled = true, },
        references = { includeDecompiledSources = true, },
        inlayHints = { parameterNames = { enabled = 'all', }, },
        format = { enabled = true, },
      }
    },
  },
  zls = { root_dir = project.find_zig_project_root, },
  ziggy = { root_dir = project.find_zig_project_root, },
  ziggy_schema = { root_dir = project.find_zig_project_root, },
  harper_ls = {
    settings = {
      ["harper-ls"] = {
        linters = {
          spell_check = true,
          spelled_numbers = true,
          an_a = true,
          sentence_capitalization = false,
          unclosed_quotes = true,
          wrong_quotes = true,
          long_sentences = true,
          repeated_words = true,
          spaces = false,
          matcher = true,
          correct_number_suffix = true,
          number_suffix_capitalization = true,
          multiple_sequential_pronouns = true,
          linking_verbs = true,
          avoid_curses = true,
          terminating_conjunctions = true
        },
        codeActions = {
          forceStable = true
        },
      }
    }
  },
  asm_lsp = {},
  clangd = {},
  denols = {},
  dotls = {},
  docker_compose_language_service = {},
  dockerls = {},
  ast_grep = {},
  cssls = {},
  css_variables = {},
  contextive = {},
  emmet_language_server = {},
  gopls = {},
  gradle_ls = {},
  html = {},
  htmx = {},
  hyprls = {},
  jsonls = {},
  lemminx = {},
  ltex = {},
  marksman = {},
  markdown_oxide = {},
  nil_ls = {},
  nixd = {},
  nushell = {},
  ocamllsp = {},
  protols = {},
  roc_ls = {},
  rust_analyzer = {},
  statix = {},
  superhtml = {},
  taplo = {},
  ts_ls = {},
  zk = {},
}

local lspconfig = puk 'lspconfig'
for server, config in pairs(language_servers) do
  local capabilities = vim.lsp.protocol.make_client_capabilities()
  config.capabilities = require('bugabinga.completion').get_lsp_capabilities(config.capabilities)
  capabilities = table.extend('force', capabilities, config.capabilities)
  -- local settings = config.settings or {}
  -- TODO Re-implement local settings overrides
  -- local settings_override = local_settings(server) or {}
  -- config.settings = table.extend('force', settings, settings_override)
  lspconfig[server].setup(config)
end

local lsp_attach = function(args)
  local bufnr = args.buf
  local client_id = args.data.client_id
  local client = vim.lsp.get_client_by_id(client_id)
  if not client then return end

  vim.lsp.completion.enable(true, client_id, bufnr)

  -- the following are already set by default
  -- but i will remap them anyways for consistency

  map.normal {
    description = 'code action',
    category = 'lsp',
    keys = 'gra',
    command = vim.lsp.buf.code_action,
  }
  map.normal {
    description = 'implementation',
    category = 'lsp',
    keys = 'gri',
    command = vim.lsp.buf.implementation,
  }
  map.normal {
    description = 'rename',
    category = 'lsp',
    keys = 'grn',
    command = vim.lsp.buf.rename,
  }
  map.normal {
    description = 'references',
    category = 'lsp',
    keys = 'grr',
    command = vim.lsp.buf.references,
  }

  -- create mappings for rest of lsp functions in the default style: gr prefix
  map.normal {
    description = 'definition',
    category = 'lsp',
    keys = 'grd',
    command = vim.lsp.buf.definition,
  }
  map.normal {
    description = 'list workspace folders',
    category = 'lsp',
    keys = 'grwl',
    command = vim.lsp.buf.list_workspace_folders,
  }
  map.normal {
    description = 'add workspace folder',
    category = 'lsp',
    keys = 'grwa',
    command = vim.lsp.buf.add_workspace_folder,
  }
  map.normal {
    description = 'remove workspace folder',
    category = 'lsp',
    keys = 'grwr',
    command = vim.lsp.buf.remove_workspace_folder,
  }
  map.normal {
    description = 'incoming calls',
    category = 'lsp',
    keys = 'grc',
    command = vim.lsp.buf.incoming_calls,
  }
  map.normal {
    description = 'outgoing calls',
    category = 'lsp',
    keys = 'grC',
    command = vim.lsp.buf.outgoing_calls,
  }
  map.normal {
    description = 'type definition',
    category = 'lsp',
    keys = 'grt',
    command = vim.lsp.buf.type_definition,
  }
  map.normal {
    description = 'type hierarchy (subtypes)',
    category = 'lsp',
    keys = 'grh',
    command = function() vim.lsp.buf.typehierarchy 'subtypes' end,
  }
  map.normal {
    description = 'type hierarchy (supertypes)',
    category = 'lsp',
    keys = 'grH',
    command = function() vim.lsp.buf.typehierarchy 'supertypes' end,
  }

  map.insert {
    description = 'signature help',
    category = 'lsp',
    keys = '<c-space>',
    command = vim.lsp.buf.signature_help,
  }

  if client.server_capabilities.inlayHintProvider then
    map.normal {
      description = 'Toggle inlay hints',
      category = 'lsp',
      buffer = bufnr,
      keys = '<leader>ti',
      command = function()
        vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
      end,
    }
  end
end

auto 'lsp' {
  description = 'lsp: client attached to buffer',
  events = 'LspAttach',
  command = lsp_attach,
}

local completion_icons = {
  Class = icon.class,
  Color = icon.color,
  Constant = icon.constant,
  Constructor = icon.constructor,
  Enum = icon.enum,
  EnumMember = icon.enum_member,
  Field = icon.field,
  File = icon.file,
  Folder = icon.folder,
  Function = icon['function'],
  Interface = icon.interface,
  Keyword = icon.keyword,
  Method = icon.method,
  Module = icon.module,
  Property = icon.property,
  Snippet = icon.snippet,
  Struct = icon.struct,
  Text = icon.text,
  Unit = icon.unit,
  Value = icon.value,
  Variable = icon.variable,
}

local completion_kinds = vim.lsp.protocol.CompletionItemKind
for i, kind in ipairs(completion_kinds) do
  completion_kinds[i] = completion_icons[kind] or kind
end