Closed14

初めてのNeovim Plugin開発

mena1mena1

作るプラグイン

  • エラーをyankしたいケースが多々あったため
  • lsp系のpopupをyankできたら 楽
mena1mena1

nvim --cmd "set rtp+=./"
nvimを起動する際に現在のディレクトリをruntimepathに追加する
-> 開発してるpluginの識別をさせる + pluginのデバッグ等する

mena1mena1

package.loaded['xxx'] = nil

必要な場面

 -- greetings/awesome-module.lua
local function greet()
  print('hello')
end

return greet

メインのプログラムでこのmoduleを使用しているとする

-- main.lua
local myGreetModule = require('greetings.awesome-module')

myGreetModule()  -- 'hello' を出力

awesome-module.lua を修正して、greet 関数の挙動を変更した

-- greetings/awesome-module.lua
local function greet()
  print('hello, world!')
end

return greet

変更後、出力するとhelloとなる
☟ なぜ?
luaはmoduleを一度ロードしたらそれをcacheして再度loadしないようにする
そのため、修正を反映させるにはpackage.loadedを使用してmoduleをunloadしてから再度loadすれば、変更が反映される

-- module unload
vim.api.nvim_set_keymap('n', ',r', ':luafile dev/init.lua<cr>', {})

-- 関数呼び出し
Greetings = require('greetings')
vim.api.nvim_set_keymap('n', ',w', ':lua Greetings.greet()<cr>', {})

https://inzkyk.xyz/lua_5_4/standard_libraries/modules/#gsc.tab=0

mena1mena1

lspのメソッドをいじって取得するらしい?

Methods are the names of requests and notifications as defined by the LSP
specification. These LSP requests/notifications are defined by default:

    callHierarchy/incomingCalls
    callHierarchy/outgoingCalls
    textDocument/codeAction
    textDocument/completion
    textDocument/declaration*
    textDocument/definition
    textDocument/documentHighlight
    textDocument/documentSymbol
    textDocument/formatting
    textDocument/hover
    textDocument/implementation*
    textDocument/publishDiagnostics
    textDocument/rangeFormatting
    textDocument/references
    textDocument/rename
    textDocument/signatureHelp
    textDocument/typeDefinition*
    window/logMessage
    window/showMessage
    window/showDocument
    window/showMessageRequest
    workspace/applyEdit
    workspace/symbol

このメソッドから欲しいものをリクエストする

mena1mena1

vim.lsp.handlers -> サーバからクライアントへの通知の処理
ex

vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, result)
    -- 診断情報の通知をハンドル
end

vim.lsp.request -> クライアントがサーバに対してリクエストを送信
ex

vim.lsp.request(0, 'textDocument/hover', params, function(_, _, result)
    -- リクエストの結果を処理
end)
mena1mena1

vim.Diagnosticを使用することで現在のcursor位置のMessageを取得することができた

mena1mena1

clipboardに関数結果を保存する
local example = example_fn()
vim.fn.serteg('+', example)

このスクラップは6ヶ月前にクローズされました