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
190
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
99
Other Decks in Programming
See All in Programming
Being an ethical software engineer
xgouchet
PRO
0
210
Agentic Applications with Symfony
el_stoffel
2
300
Optimizing JRuby 10
headius
0
310
SEAL - Dive into the sea of search engines - Symfony Live Berlin 2025
alexanderschranz
1
130
AIコーディングの理想と現実
tomohisa
21
27k
gen_statem - OTP's Unsung Hero
whatyouhide
1
200
Memory API : Patterns, Performance et Cas d'Utilisation
josepaumard
0
130
リストビュー画面UX改善の振り返り
splcywolf
0
140
Fiber Scheduler vs. General-Purpose Parallel Client
hayaokimura
1
100
リアルタイムレイトレーシング + ニューラルレンダリング簡単紹介 / Real-Time Ray Tracing & Neural Rendering: A Quick Introduction (2025)
shocker_0x15
1
300
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
0
110
Defying Front-End Inertia: Inertia.js on Rails
skryukov
0
480
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
34
2.2k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
Thoughts on Productivity
jonyablonski
69
4.6k
We Have a Design System, Now What?
morganepeng
52
7.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
119
51k
How to Ace a Technical Interview
jacobian
276
23k
Raft: Consensus for Rubyists
vanstee
137
6.9k
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