4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[覚書] neovimのlspをいろいろ使いやすくしたりとか (lua)

Posted at

覚書なので随時追加していく系かもです

注意事項

  • 三文記事です
  • 内容はほぼ日記と覚書です
  • 基本的には公式ドキュメント嫁案件ではあります
  • なんか間違えていたら教えてくださるとありがたいです
  • 実行環境は以下に依存しています
    • neovim 0.9.2
    • deno 1.38.5

私のneovimの設定

詳しくはこちらをご参照ください

この記事を書いた時点

最新

LSPがロードされたタイミングで定義情報とかをカーソルの上下にホバーで表示してほしい

your/config/lsp.lua
      api.nvim_set_option('updatetime', 1000)

      vim.api.nvim_create_autocmd("LspAttach", {
        callback = function(args)
          -- ここに `textDocument/hover` で表示させたくないファイルタイプを指定する
          if args.filetype == 'NvimTree' or args.filetype == 'NeogitCommitMessage' then
            return
          end
          --    vim.cmd [[autocmd CursorHold,CursorHoldI * silent lua vim.lsp.buf.hover()]]
          vim.cmd([[autocmd CursorHold,CursorHoldI * lua vim.diagnostic.open_float(nil, {focus=false})]])
          vim.cmd [[autocmd CursorHold,CursorHoldI * silent lua vim.lsp.buf.hover()]]
        end,
      })

LSPで任意のシンボルの定義元や参照先を一括で一覧で表示させたい

⚠️注意: lspsaga.nvim に依存します

your/config/lspsaga.lua
      require('lspsaga').setup({
        finder = {
          max_height = 0.6,
          -- これは必須です / REQUIRED
          default = 'tyd+ref+imp+def',
          -- ここは任意でお好きなキーバインドにしてください / optional
          keys = {
            toggle_or_open = '<CR>',
            vsplit = 'v',
            split = 's',
            tabnew = 't',
            tab = 'T',
            quit = 'q',
            close = '<Esc>',
          },
          -- これは必須です / REQUIRED
          methods = {
            tyd = 'textDocument/typeDefinition',
          }
        }
      })

      -- ここで `leader` キーと `,` で、ポップアップで表示されます
      -- 私の場合の `leader` キーは `,` なので、 `,,` で出てきてくれます
      -- うれしい
      keymap.set('n', '<leader>,',  "<Cmd>Lspsaga finder<CR>",  { desc = 'Telescope: live grep args', })

表示例

こんな風に表示されます

image.png

lazy.nvimでの設定例

your/config/lspsaga.lua
local keymap = vim.keymap
return {
  {
    'nvimdev/lspsaga.nvim',
    dependencies = {
      -- 'nvim-treesitter/nvim-treesitter',
      'nvim-tree/nvim-web-devicons',
    },
    config = function()
      require('lspsaga').setup({
        finder = {
          max_height = 0.6,
          default = 'tyd+ref+imp+def',
          keys = {
            toggle_or_open = '<CR>',
            vsplit = 'v',
            split = 's',
            tabnew = 't',
            tab = 'T',
            quit = 'q',
            close = '<Esc>',
          },
          methods = {
            tyd = 'textDocument/typeDefinition',
          }
        }
      })

      keymap.set('n', '<leader>,',  "<Cmd>Lspsaga finder<CR>",  { desc = 'Telescope: live grep args', })
    end,
  }
}

LSPの code actions をいい感じに出してほしい

⚠️注意: actions-preview.nvim に依存します

いい感じってどんな感じのこと?
image.png
image.png

次の設定で、 <leader>👉<space> で表示されます

your/config/lsp.lua
  {
    lazy = true,
    "aznhe21/actions-preview.nvim",
    event = { 'LspAttach' },
    dependencies = {
      'kosayoda/nvim-lightbulb',
      "neovim/nvim-lspconfig",
    },
    init = function()
      vim.api.nvim_create_autocmd("LspAttach", {
        desc = "Setup code action preview",
        callback = function(args)
          local bufnr = args.buf

          vim.keymap.set("n", "<leader><space>", function()
            require("actions-preview").code_actions()
          end, { buffer = bufnr, desc = "LSP: Code action" })
        end,
      })
    end,
    config = function()
      require("actions-preview").setup {}
    end,
  },

最後に

LSP、べんり!!

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?