nvim/lua/config/autocmds.lua 2.9 K raw
1
vim.api.nvim_create_autocmd('TextYankPost', {
2
  desc = 'Highlight when yanking (copying) text',
3
  group = vim.api.nvim_create_augroup('kickstart-highlight-yank', { clear = true }),
4
  callback = function()
5
    vim.highlight.on_yank()
6
  end,
7
})
8
9
vim.api.nvim_create_autocmd("LspAttach", {
10
  group = vim.api.nvim_create_augroup('lsp-attach', { clear = true }),
11
  callback = function(event)
12
    local map = function(keys, func, desc)
13
      vim.keymap.set("n", keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
14
    end
15
16
    -- defaults:
17
    -- https://neovim.io/doc/user/news-0.11.html#_defaults
18
19
    map("gl", vim.diagnostic.open_float, "Open Diagnostic Float")
20
    map("K", vim.lsp.buf.hover, "Hover Documentation")
21
    map("gs", vim.lsp.buf.signature_help, "Signature Documentation")
22
    map("gD", vim.lsp.buf.declaration, "Goto Declaration")
23
    map("<leader>la", vim.lsp.buf.code_action, "Code Action")
24
    map("<leader>lr", vim.lsp.buf.rename, "Rename all references")
25
    map("<leader>lf", vim.lsp.buf.format, "Format")
26
    map("<leader>v", "<cmd>vsplit | lua vim.lsp.buf.definition()<cr>", "Goto Definition in Vertical Split")
27
    map("<leader>d", "<cmd>Telescope diagnostics bufnr=0<CR>", "Open diagnostsics in telescope")
28
29
    local function client_supports_method(client, method, bufnr)
30
      if vim.fn.has 'nvim-0.11' == 1 then
31
        return client:supports_method(method, bufnr)
32
      else
33
        return client.supports_method(method, { bufnr = bufnr })
34
      end
35
    end
36
37
    local client = vim.lsp.get_client_by_id(event.data.client_id)
38
    if client and client_supports_method(client, vim.lsp.protocol.Methods.textDocument_documentHighlight, event.buf) then
39
      local highlight_augroup = vim.api.nvim_create_augroup('lsp-highlight', { clear = false })
40
41
      -- When cursor stops moving: Highlights all instances of the symbol under the cursor
42
      -- When cursor moves: Clears the highlighting
43
      vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
44
        buffer = event.buf,
45
        group = highlight_augroup,
46
        callback = vim.lsp.buf.document_highlight,
47
      })
48
      vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
49
        buffer = event.buf,
50
        group = highlight_augroup,
51
        callback = vim.lsp.buf.clear_references,
52
      })
53
54
      -- When LSP detaches: Clears the highlighting
55
      vim.api.nvim_create_autocmd('LspDetach', {
56
        group = vim.api.nvim_create_augroup('lsp-detach', { clear = true }),
57
        callback = function(event2)
58
          vim.lsp.buf.clear_references()
59
          vim.api.nvim_clear_autocmds { group = 'lsp-highlight', buffer = event2.buf }
60
        end,
61
      })
62
    end
63
  end,
64
65
})
66
67
vim.api.nvim_create_autocmd("VimEnter", {
68
  callback = function()
69
    if vim.fn.argv(0) == "" then
70
      require("telescope.builtin").find_files()
71
    end
72
  end,
73
})
74
75
76
require("conform").setup({
77
  format_on_save = {
78
    -- These options will be passed to conform.format()
79
    timeout_ms = 500,
80
    lsp_format = "fallback",
81
  },
82
})