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
Iori IKEDA
November 07, 2024
Programming
1
210
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
110
Other Decks in Programming
See All in Programming
Result型で“失敗”を型にするPHPコードの書き方
kajitack
4
260
エンジニア向け採用ピッチ資料
inusan
0
160
deno-redisの紹介とJSRパッケージの運用について (toranoana.deno #21)
uki00a
0
140
Enterprise Web App. Development (2): Version Control Tool Training Ver. 5.1
knakagawa
1
120
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
17
4.9k
エラーって何種類あるの?
kajitack
5
290
技術同人誌をMCP Serverにしてみた
74th
0
280
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
4
910
Team operations that are not burdened by SRE
kazatohiei
1
180
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
470
生成AIで日々のエラー調査を進めたい
yuyaabo
0
640
Deep Dive into ~/.claude/projects
hiragram
7
1.2k
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
BBQ
matthewcrist
89
9.7k
The Pragmatic Product Professional
lauravandoore
35
6.7k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Six Lessons from altMBA
skipperchong
28
3.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Adopting Sorbet at Scale
ufuk
77
9.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
Git: the NoSQL Database
bkeepers
PRO
430
65k
A designer walks into a library…
pauljervisheath
206
24k
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