👁‍🗨

socatでLSPをみてみる

に公開

ssh越しにxdg-openできないかなあとかを調べてたらsocatというツールを知りました。
TCPとかUNIXソケットとかファイルなどの間の双方向の通信をすることができるツールらしいです。
これを使えば標準入出力でやり取りするタイプの言語サーバーの生のやり取りを眺めることができそうだったので、やってみました[1]。

やってみる

ManのExamplesをみてるとなんとなく使い方がわかります。

今回は標準入出力でやり取りする言語サーバーを使います[2]。
なので、socatではsocatの標準入出力(-)と言語サーバーのプロセス(EXEC)の両者を繋ぐようにします。
それで双方向のやり取りを-rおよび-Rオプションを使ってファイルにダンプします。
例えばrust-anlyzerのやり取りをみたければ、rust-anlyzerの代わりに

socat -r messages_from_client.txt -R messages_from_server.txt - EXEC:rust-anlyzer

を実行すれば、messages_from_server.txtとmessages_from_client.txtにJSONRPCの生データが残ります。

言語サーバークライアントの設定に反映させる必要がありますが、今回はNeovimを使ってやるので、

after/lsp/lua-language-server.lua
return {
    cmd = { 'socat', '-r', log_file_from_client, '-R', log_file_from_server, '-', 'EXEC:' .. 'lua-language-server' },
}

のようなものを書けば、こんな感じのものが見れます:

client → server
Content-Length: 4311

{"params":{"rootUri":"file:///home/qwjyh/dotfiles","rootPath":"/home/qwjyh/dotfiles","clientInfo":{"version":"0.11.5+v0.11.5","name":"Neovim"},"trace":"off","workDoneToken":"1","workspaceFolders":[{"name":"/home/qwjyh/dotfiles","uri":"file:///home/qwjyh/dotfiles"}],"processId":540974,"capabilities":{"general":{"positionEncodings":["utf-8","utf-16","utf-32"]},"workspace":{"didChangeWatchedFiles":{"relativePatternSupport":true,"dynamicRegistration":false},"workspaceEdit":{"resourceOperations":["rename","create","delete"]},"didChangeConfiguration":{"dynamicRegistration":false},"workspaceFolders":true,"configuration":true,"semanticTokens":{"refreshSupport":true},"inlayHint":{"refreshSupport":true},"applyEdit":true,"symbol":{"symbolKind":{"valueSet":[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]},"dynamicRegistration":false}},"textDocument":{"diagnostic":{"dynamicRegistration":false,"tagSupport":{"valueSet":[1,2]}},"definition":{"linkSupport":true,"dynamicRegistration":true},"declaration":{"linkSupport":true},"references":{"dynamicRegistration":false},"synchronization":{"willSaveWaitUntil":true,"dynamicRegistration":false,"didSave":true,"willSave":true},"semanticTokens":{"serverCancelSupport":false,"multilineTokenSupport":false,"overlappingTokenSupport":true,"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"],"requests":{"range":false,"full":{"delta":true}},"formats":["relative"],"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"dynamicRegistration":false,"augmentsSyntaxTokens":true},"inlayHint":{"resolveSupport":{"properties":["textEdits","tooltip","location","command"]},"dynamicRegistration":true},"callHierarchy":{"dynamicRegistration":false},"publishDiagnostics":{"tagSupport":{"valueSet":[1,2]},"relatedInformation":true,"dataSupport":true},"implementation":{"linkSupport":true},"codeAction":{"dynamicRegistration":true,"resolveSupport":{"properties":["edit","command"]},"dataSupport":true,"isPreferredSupport":true,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"documentSymbol":{"dynamicRegistration":false,"symbolKind":{"valueSet":[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]},"hierarchicalDocumentSymbolSupport":true},"completion":{"insertTextMode":1,"completionItemKind":{"valueSet":[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]},"completionItem":{"labelDetailsSupport":true,"snippetSupport":true,"resolveSupport":{"properties":["documentation","additionalTextEdits","insertTextFormat","insertTextMode","command"]},"deprecatedSupport":true,"preselectSupport":true,"commitCharactersSupport":true,"insertReplaceSupport":true,"documentationFormat":["markdown","plaintext"],"insertTextModeSupport":{"valueSet":[1,2]},"tagSupport":{"valueSet":[1]}},"completionList":{"itemDefaults":["commitCharacters","editRange","insertTextFormat","insertTextMode","data"]},"contextSupport":true,"dynamicRegistration":false},"rangeFormatting":{"rangesSupport":true,"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":false},"formatting":{"dynamicRegistration":true},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"rename":{"prepareSupport":true,"dynamicRegistration":true},"signatureHelp":{"signatureInformation":{"documentationFormat":["markdown","plaintext"],"activeParameterSupport":true,"parameterInformation":{"labelOffsetSupport":true}},"dynamicRegistration":false},"typeDefinition":{"linkSupport":true},"foldingRange":{"foldingRangeKind":{"valueSet":["comment","imports","region"]},"dynamicRegistration":false,"lineFoldingOnly":true,"foldingRange":{"collapsedText":true}},"codeLens":{"resolveSupport":{"properties":["command"]},"dynamicRegistration":false}},"window":{"showDocument":{"support":true},"workDoneProgress":true,"showMessage":{"messageActionItem":{"additionalPropertiesSupport":true}}}}},"id":1,"jsonrpc":"2.0","method":"initialize"}Content-Length: 52

{"params":{},"jsonrpc":"2.0","method":"initialized"}Content-Length: 280

{"params":{"settings":{"Lua":{"runtime":{"version":"LuaJIT"},"codeLens":{"enable":true},"hint":{"enable":true,"semicolon":"Disable"}},"workspace":{"library":"/usr/share/nvim/runtime"},"diagnostics":{"globals":["vim"]}}},"jsonrpc":"2.0","method":"workspace/didChangeConfiguration"}Content-Length: 27545

server → client
Content-Length: 55

{"jsonrpc":"2.0","method":"$/hello","params":["world"]}Content-Length: 172

{"jsonrpc":"2.0","method":"window/logMessage","params":{"message":"Log path: file:///tmp/lua-language-server-1000/instance.397R/log/file_home_qwjyh_dotfiles.log","type":4}}Content-Length: 1884

{"id":1,"jsonrpc":"2.0","result":{"capabilities":{"codeActionProvider":{"codeActionKinds":["","quickfix","refactor.rewrite","refactor.extract"],"resolveProvider":false},"codeLensProvider":{"resolveProvider":true},"colorProvider":true,"completionProvider":{"resolveProvider":true,"triggerCharacters":["\t","\n",".",":","(","'","\"","[",",","#","*","@","|","=","-","{"," ","+","?"]},"definitionProvider":true,"documentFormattingProvider":true,"documentHighlightProvider":true,"documentOnTypeFormattingProvider":{"firstTriggerCharacter":"\n"},"documentRangeFormattingProvider":true,"documentSymbolProvider":true,"executeCommandProvider":{"commands":["lua.removeSpace","lua.solve","lua.jsonToLua","lua.setConfig","lua.getConfig","lua.autoRequire"]},"foldingRangeProvider":true,"hoverProvider":true,"implementationProvider":true,"inlayHintProvider":{"resolveProvider":true},"offsetEncoding":"utf-16","referencesProvider":true,"renameProvider":{"prepareProvider":true},"semanticTokensProvider":{"full":true,"legend":{"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary","global"],"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"]},"range":true},"signatureHelpProvider":{"triggerCharacters":["(",","]},"textDocumentSync":{"change":2,"openClose":true,"save":{"includeText":false}},"typeDefinitionProvider":true,"workspace":{"fileOperations":{"didRename":{"filters":[{"pattern":{"glob":"/home/qwjyh/dotfiles/**","options":{"ignoreCase":true}}}]}},"workspaceFolders":{"changeNotifications":true,"supported":true}},"workspaceSymbolProvider":true},"serverInfo":{"name":"sumneko.lua","version":"3.16.4"}}}Content-Length: 459

{"id":1,"jsonrpc":"2.0","method":"workspace/configuration","params":{"items":[{"scopeUri":"file:///home/qwjyh/dotfiles","section":"Lua"},{"scopeUri":"file:///home/qwjyh/dotfiles","section":"files.associations"},{"scopeUri":"file:///home/qwjyh/dotfiles","section":"files.exclude"},{"scopeUri":"file:///home/qwjyh/dotfiles","section":"editor.semanticHighlighting.enabled"},{"scopeUri":"file:///home/qwjyh/dotfiles","section":"editor.acceptSuggestionOnEnter"}]}}Content-Length: 459

脚注
  1. 別にTCP使っててもできると思います。 ↩︎

  2. 自分が試したのは、lua-language-server、rust-anlyzer、jetlsです。 ↩︎

GitHubで編集を提案

Discussion