aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/home/.config/nvim/lua/custom/plugins/lsp.lua
blob: 6bd73d7b21aa883948c94eba56fac9a27057ed93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
local map = require("mapper")

local function handler_splitcmd(_, uri, splitcmd)
  if not uri or uri == "" then
    vim.api.nvim_echo({ { "Corresponding file cannot be determined" } }, false, {})
    return
  end
  local file_name = vim.uri_to_fname(uri)
  vim.api.nvim_cmd({
    cmd = splitcmd,
    args = { file_name },
  }, {})
end

local function switch_source_header_splitcmd(bufnr, splitcmd)
  bufnr = bufnr or 0
  vim.lsp.buf_request(bufnr, "textDocument/switchSourceHeader", {
    uri = vim.uri_from_bufnr(bufnr),
  }, function(err, uri) return handler_splitcmd(err, uri, splitcmd) end)
end

local function edit_source_header(bufnr)
  switch_source_header_splitcmd(bufnr, "edit")
end

local function split_source_header(bufnr)
  switch_source_header_splitcmd(bufnr, "split")
end

local function vsplit_source_header(bufnr)
  switch_source_header_splitcmd(bufnr, "vsplit")
end

local function tabedit_source_header(bufnr)
  switch_source_header_splitcmd(bufnr, "tabedit")
end

return {
  {
    "lewis6991/hover.nvim",
    config = function()
      require("hover").setup {
        init = function()
          require("hover.providers.lsp")
          require('hover.providers.gh')
          require('hover.providers.man')
          -- require('hover.providers.dictionary')
        end,
      }

      vim.keymap.set("n", "K", require("hover").hover, { desc = "hover.nvim" })
      vim.keymap.set("n", "gh", require("hover").hover, { desc = "hover.nvim" })
      vim.keymap.set("n", "gK", require("hover").hover_select,
        { desc = "hover.nvim (select)" })
    end
  },
  {
    'neovim/nvim-lspconfig',
    config = function()
      local lspconfig = require("lspconfig")
      -- Enable (broadcasting) snippet capability for completion
      local capabilities = vim.lsp.protocol.make_client_capabilities()
      capabilities.textDocument.completion.completionItem.snippetSupport = true
      capabilities.workspace.didChangeWatchedFiles.dynamicRegistration = true
      if pcall(require, "cmp_nvim_lsp") then
        capabilities = require("cmp_nvim_lsp").default_capabilities()
      end

      lspconfig.util.default_config = vim.tbl_extend(
        "force",
        lspconfig.util.default_config,
        {
          capabilities = capabilities,
        }
      )

      local servers = {
        bashls = {},
        dockerls = {},
        fortls = {},
        lua_ls = {},
        ruff_lsp = {},
        pyright = {},
        clangd = {
          on_attach = function(_, bufnr)
            map.n("gH", edit_source_header, { buffer = bufnr })
            map.n("gvH", vsplit_source_header, { buffer = bufnr })
            map.n("gxH", split_source_header, { buffer = bufnr })
            map.n("gtH", tabedit_source_header, { buffer = bufnr })
          end,
          init_options = {
            clangdFileStatus = true,
          },
        },
      }

      for server, config in pairs(servers) do
        local default_config = lspconfig[server].default_config or
            lspconfig[server].document_config.default_config
        local cmd = config.cmd or default_config.cmd
        if vim.fn.executable(cmd[1]) == 1 then lspconfig[server].setup(config) end
      end

      vim.api.nvim_create_autocmd("LspAttach", {
        callback = function(args)
          local bufnr = args.buf
          local client = assert(vim.lsp.get_client_by_id(args.data.client_id))

          if client.supports_method("textDocument/codeLens") then
            vim.api.nvim_create_autocmd(
              { "CursorHold", "CursorHoldI", "InsertLeave" },
              { buffer = bufnr, callback = vim.lsp.codelens.refresh }
            )
            map.n("gl", vim.lsp.codelens.run, { buffer = bufnr })
          end

          map.n("<c-]>", vim.lsp.buf.definition, { buffer = bufnr })
          map.n("gD", vim.lsp.buf.declaration, { buffer = bufnr })
          map.n("gR", vim.lsp.buf.rename, { buffer = bufnr })
          map.n("ga", vim.lsp.buf.code_action, { buffer = bufnr })
          map.v("ga", vim.lsp.buf.code_action, { buffer = bufnr })
        end,
      })
    end,
    dependencies = {
      { 'folke/neodev.nvim', opts = {} },
    },
  },
  {
    "ray-x/lsp_signature.nvim",
    event = "VeryLazy",
    config = function()
      local lsp_signature = require "lsp_signature"
      lsp_signature.setup({})

      vim.api.nvim_create_autocmd("LspAttach", {
        callback = function(args)
          local bufnr = args.buf
          local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
          if client.supports_method("textDocument/signatureHelp") then
            require("lsp_signature").on_attach({}, bufnr)
            map.n("gs", vim.lsp.buf.signature_help, { buffer = bufnr })
          end
        end,
      })
    end
  },
  {
    "stevearc/conform.nvim",
    event = { "BufWritePre" },
    cmd = { "ConformInfo" },
    keys = {
      {
        "<leader>f",
        function()
          require("conform").format({ async = true, lsp_fallback = true })
        end,
        mode = "",
        desc = "Format buffer",
      },
    },
    opts = {
      formatters_by_ft = {
        python = { "ruff_format" },
        cmake = { "cmake_format" },
        json = { "jq" },
        rust = { "rustfmt" },
        sh = { "shfmt" },
        bash = { "shfmt" },
        zsh = { "shfmt" },
      },
      formatters = {
        shfmt = {
          prepend_args = { "-i", "2" },
        },
      },
    },
    init = function()
      vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
      vim.api.nvim_create_autocmd("BufWritePre", {
        callback = require "cfg.utils".format_hunks,
      })
    end,
  },
  {
    'mrcjkb/rustaceanvim',
    lazy = false,
  },
  {
    "mfussenegger/nvim-lint",
    event = { 'BufReadPre', 'BufNewFile' },
    config = function()
      local lint = require('lint')
      lint.linters_by_ft = {
        dockerfile = { "hadolint" },
      }
      vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost", "InsertLeave" }, {
        callback = function()
          lint.try_lint()
        end
      })
    end
  },
  { "j-hui/fidget.nvim", opts = {} },
}