nvim-lspconfig

And now, the end is near

And so I face the final curtain

そして今 終わりが近づいている

そして私は 最後の幕に臨む

さて、まずはLSP活用の基盤を築きましょう❗nvim-lspconfigの登場です😆

Configs for the Nvim LSP client (:help lsp).

Nvim LSP クライアント (:help lsp) のコンフィグです。

LSP

:h lsp

LSP client/framework                                     lsp LSP

Nvim supports the Language Server Protocol (LSP), which means it acts as
a client to LSP servers and includes a Lua framework `vim.lsp` for building
enhanced LSP tools.

Nvim は Language Server Protocol (LSP) をサポートしており、
LSP サーバーのクライアントとして動作し、
拡張 LSP ツールを構築するための Lua フレームワーク `vim.lsp` を含んでいます。

  https://microsoft.github.io/language-server-protocol/

LSP facilitates features like go-to-definition, find-references, hover,
completion, rename, format, refactor, etc., using semantic whole-project
analysis (unlike ctags).

LSPは、(ctags とは異なり) 意味論的なプロジェクト全体の分析を用いて、
go-to-definition、find-references、hover、completion、rename、format、refactor、
などの機能を容易にします。

本来ならここにある内容を自分で行っていく必要があるんですが、 「setupを呼んでくれるだけでいいよー」ってしてくれるのが、このnvim-lspconfigです。

LSP configs provided by nvim-lspconfig are listed below.

nvim-lspconfigが提供するLSPコンフィグを以下に示します。

Note

細かいカスタマイズをしたい場合は、デフォルト設定を選択的にオーバーライドして使うこともできます。

要するに便利ってことです❗❗

My friend, I’ll say it clear

I’ll state my case, of which I’m certain

友よ、率直に話すよ

私が確かだと思えることを、聞いてほしい

Install

Requires neovim version 0.8 above.

neovim version 0.8 以上が必要です。

Install nvim-lspconfig like any other Vim plugin, e.g. with packer.nvim:

nvim-lspconfig は他の Vim プラグインと同様に、例えば packer.nvim でインストールしてください

なんかもう何を言ってるのか全然分かる😑

I’ve lived a life that’s full

I traveled each and every highway

私は 満ち足りた人生を送ってきた

あらゆる道を旅してきたんだ

nagashima-matsui nagashima-matsui2 nagashima3 matsui55

Configuration

nvim-lspconfig does not set keybindings or enable completion by default. The following example configuration provides suggested keymaps for the most commonly used language server functions, and manually triggered completion with omnifunc (<c-x><c-o>).

nvim-lspconfig はデフォルトでキーバインドを設定したり、補完を有効にしたりしません。 次の設定例では、最もよく使われる言語サーバ機能のキーマップを提案し、 omnifunc (<c-x><c-o>) による補完を手動でトリガしています。

オフィシャルには、おっそろしく迅速にpyright、tsserver、rust_analyzerのセットアップがされていますが、 大胆にも、このサイトではこれらをスキップして、もっと汎用的な方法をとります❗

Tip

それぞれ、Python、TypeScript、Rustの Language Server です。

と、いうことで、ここではキーマップの設定だけしちゃいましょう😌

extensions/nvim-lspconfig.lua

-- Global mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float)
vim.keymap.set('n', '[d', function() vim.diagnostic.jump({ count = 1}) end)
vim.keymap.set('n', ']d', function() vim.diagnostic.jump({ count = -1}) end)
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist)

-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    local opts = { buffer = ev.buf }

    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
    vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
    vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
    vim.keymap.set('n', '<space>wl', function()
      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, opts)
    vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
    vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
    vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, opts)
    vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
    vim.keymap.set('n', '<space>f', function()
      vim.lsp.buf.format { async = true }
    end, opts)
  end,
})

ほんとにキーマップの設定だけなので、サンプルそのままでしたね😅

それだけ面倒な設定をうまく包み込んでくれてるってことです。

Note

っていうだけなのもつまんないので、ちょっとだけ…。

わたしは以下のキーマップだけ外して使ってます。

vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)

これ、元の動作の方が使いやすいと思うのはわたしだけなのかな…。

ただ、まだお話し相手がいない状態なので、何にもできないんですけどね😅

あとこれ、いつもの❗

extensions/init.lua

use {
  'neovim/nvim-lspconfig',
  config = function() require 'extensions.nvim-lspconfig' end,
}

And more, much more than this

そしてそれ以上に、もっと ずっと多くのこと

LspAttach

LspAttachってなんやねんってなりますが、これはもうそのままヘルプにあります。

:h LspAttach

                                                                   LspAttach
After an LSP client attaches to a buffer.

LSPクライアントがバッファにアタッチした後 (に発生するイベント)。

久しぶりに現れたAutomatic Commandは、11章 に出てきたお話です。

いつだっておじさんは熱くアドバイスしてくれます☺️

うん。まず何よりもはっきりさせておきたいのは、auというのはautocmdの先頭2文字からきているようですね。

ここはもう “nvimトレーナー” に任せておけば安心ですね。

lspconfig

ここはこれだけです。もう簡単でしょう❓

I did it my way

自らのやり方で 選んできたんだ

My Way

繰り返しになりますが、これだけではまだ何もできません。

大丈夫です、基盤なんで。どっしり構えましょう😤

…ん❓😑

え、ちょっと待って。

nvimトレーナーは “nvimチャンピオン” なの⁉️ いつの間に⁉️

わたしが遊んでたりサボってたり Shazaaaaaaam!!🦸 とか叫んでたり、 Amazon のセールに合わせて自分へのご褒美を送ってあげたり受け取ったり、 さくらさくら〜🌸 とか舞い踊って酔い潰れていた間にも

nvimトレーナーは努力を続けていたってこと⁉️

しんじれば

チャンピオンも ゆめ じゃない❗

…。😮

…いや、宴こしらえてもろうてるやないか👵

Success

でんどう いり おめでとう❗

Regrets, I've had a few

But then again, too few to mention

後悔、まあ いくつかある

とはいえ、数え上げるほどでもない

I did what I had to do

And saw it through without exemption

私は すべきことをしたし

何一つとして 逃げずに最後までやり通したんだ

I planned each charted course

Each careful step along the byway

自分の進む道を 一つ一つ 描き

脇道を進む時も 一歩一歩 慎重に踏み出した

And more, much more than this

そしてそれ以上に、もっと ずっと多くのことを

I did it my way

自ら選び 旅してきたんだ

Yes, there were times, I'm sure you knew

When I bit off more than I could chew

そう、君も知っているように

自分の手に負えないほどのものに 手を出した時があった

But through it all, when there was doubt

I ate it up and spit it out

だがそんな状況に在っても 迷った時には

貪り喰って、吐き出した

I faced it all, and I stood tall

向き合い、そして立ち向かい

And did it my way

そうして 私は旅を進めてきたんだ

I've loved, I've laughed and cried

I’ve had my fill, my share of losing

愛してきた、笑ってきた、涙だって流した

十分に味わった、 負けることだってあった

And now, as tears subside

I find it all so amusing

しかし 涙はもう引いた

今となっては、全てがただ可笑しく想えるんだ

To think I did all that

And may I say, not in a shy way

思えば 私はそんな全てを経験してきた

遠慮なく言わせてもらうなら…

Oh, no, oh, no, not me

いや 違う 違ったな そんなのは私らしくない

I did it my way

自ら進み 歩んできたんだ

For what is a man, what has he got?

人とは何か 何を得るというのか?

If not himself, then he has naught

自分を持たないのであれば 何者でもない

To say the things he truly feels

And not the words of one who kneels

平伏す者が発する言葉ではなく

自分の本心を率直に伝えることだ

The record shows I took the blows

幾多の苦難は 私の道に刻まれている

And did it my way

私が歩んできた道

Yes, it was my way

これこそ 私の人生…