Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RubyLSPのマルチバイト文字対応
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Iori IKEDA
November 07, 2024
Programming
1
260
RubyLSPのマルチバイト文字対応
Omotesando.rb #103
での発表資料です。RubyLSP に行ったマルチバイト文字の対応について話しました。
Iori IKEDA
November 07, 2024
Tweet
Share
More Decks by Iori IKEDA
See All by Iori IKEDA
Passkeysのはなし
notfounds
0
150
Other Decks in Programming
See All in Programming
今から始めるClaude Code超入門
448jp
8
9.1k
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
730
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
380
CSC307 Lecture 10
javiergs
PRO
1
660
CSC307 Lecture 05
javiergs
PRO
0
500
CSC307 Lecture 02
javiergs
PRO
1
780
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
180
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
180
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.9k
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
250
Oxlint JS plugins
kazupon
1
1k
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
190
Chasing Engaging Ingredients in Design
codingconduct
0
110
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
280
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
58
50k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
440
Optimising Largest Contentful Paint
csswizardry
37
3.6k
Color Theory Basics | Prateek | Gurzu
gurzu
0
200
Building Adaptive Systems
keathley
44
2.9k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
260
A Tale of Four Properties
chriscoyier
162
24k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.4k
Transcript
RubyLSPのマルチバイト文字対応 2024/11/07 - Omotesando.rb#103 Iori IKEDA @NotFounds8080
自己紹介 Iori IKEDA • 株式会社YAMAP • Webエンジニア • 壁と山を登るのが好きです •
Twitter: @NotFounds8080 • GitHub: @NotFounds
RubyLSPについて
RubyLSPとは • Rubyの言語サーバー • 2022年ごろからShopifyが開発 • 定義ジャンプやセマンティックハイライト、RobocopによるLintなど • プラグイン機構があり、RailsやRSpecの拡張 •
ブログ等で比較的よく紹介されている(気がする)
None
None
None
何かがおかしい...?
RubyLSPに潜む問題 • Definition jumps are not possible with files containing
Japanese characters. · Issue #1347 · Shopify/ruby-lsp · GitHub • 意訳: 日本語が含まれていると動かない • これじゃん!!! • どうやらメンテナも認識しているようだが未対応 • でもどうやって直せばいいんだ...
RubyLSPの問題
RubyLSPの問題 マルチバイト文字の位置計算をいい感じに する(意訳) Prismが位置計算を行うときにエン コーディングを考慮していないから どうにかする必要があるっぽい
RubyLSPの問題 いい感じのAPI生えたし 対応が進みそう!
RubyLSPの仕組み
None
None
class Hoge def fuga puts "Hello" end end Hoge.new.fuga
RubyLSPの仕組み 1. エディタ上で定義ジャンプ { "method": "textDocument/definition", "params": { "textDocument": { "uri": "file://a.rb" }, "position": { "line": 6, "character": 9 } } }
2. positionを先頭から何文字目かに変換 元のファイルでは 6行 9文字目 'class Hoge\ndef fuga\nputs "Hello"\nend\nend\n\nHoge.new.fuga\n'
class Hoge def fuga puts "Hello" end end Hoge.new.fuga RubyLSPの仕組み 53文字目
3. 先頭からの何文字数からノードを取得 抽象構文木(AST)を辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み
3. 先頭からの何文字数からノードを取得 ASTを辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み 0~42文字目
3. 先頭からの何文字数からノードを取得 ASTを辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み 13~38文字目
3. 先頭からの何文字数からノードを取得 ASTを辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み 22~34文字目
3. 先頭からの何文字数からノードを取得 ASTを辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み 44~57文字目
3. 先頭からの何文字数からノードを取得 ASTを辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み 44~48文字目
3. 先頭からの何文字数からノードを取得 ASTを辿り、53文字目のノードを探す class Hoge def fuga puts "Hello" end
end Hoge.new.fuga RubyLSPの仕組み 53~57文字目
class Hoge def fuga puts "Hello" end end Hoge.new.fuga
RubyLSPの仕組み 4. 事前に作成したコードのIndex(辞書)から対象のノードを探す ノードが見つかったらLSPに結果を返す { "uri": "file://a.rb", "range": { "start": { "line": 1, "character": 2 }, "end": { "line": 3, "character": 5 } } }
RubyLSPの問題(再掲) マルチバイト文字の位置計算をいい感じに する(意訳) Prismが位置計算を行うときにエン コーディングを考慮していないから どうにかする必要があるっぽい
RubyLSPの問題 位置の計算をしているところが問題 • インデックス作成 • 対象ノードを取得 インデックスの位置計算が不正確 ⇒ノードの正しい場所を返せない 対象ノードの取得が不正確 ⇒関係ないノードの場所を返す
RubyLSPの修正
RubyLSPの修正 • 主な修正内容 ◦ LSPを初期化時にエディタから受け取ったencodingを設定 ◦ 位置計算を行っている箇所でPrismの新しいAPIを利用
None
無事動くようになった🎉🎉🎉 Ruby LSP v0.19.2~ 🚀 latest: v0.21.3
まとめ • RubyLSPはRubyの言語サーバー • RubyLSPの仕組みザックリ解説 • 学び ◦ ファイルや文字列を扱うときはエンコーディングに注意 ぜひRubyLSPを試してみてください!
参考文献・資料 • Code indexing: How language servers understand our code
• Language Server Protocol の仕様 及び実装方法 • Ruby LSP | An opinionated language server for Ruby. Batteries included! • https://github.com/Shopify/ruby-lsp/pull/2619 • https://github.com/Shopify/ruby-lsp/pull/2669