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
240
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
120
Other Decks in Programming
See All in Programming
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
210
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
390
ボトムアップの生成AI活用を推進する社内AIエージェント開発
aku11i
0
1.1k
Webサーバーサイド言語としてのRustについて
kouyuume
1
5k
業務でAIを使いたい話
hnw
0
190
Leading Effective Engineering Teams in the AI Era
addyosmani
7
670
Pythonに漸進的に型をつける
nealle
1
140
ドメイン駆動設計のエッセンス
masuda220
PRO
15
6.4k
iOSでSVG画像を扱う
kishikawakatsumi
0
170
Register is more than clipboard
satorunooshie
1
140
Amazon Verified Permissions実践入門 〜Cedar活用とAppSync導入事例/Practical Introduction to Amazon Verified Permissions
fossamagna
2
100
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
330
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
57k
Raft: Consensus for Rubyists
vanstee
140
7.2k
The Language of Interfaces
destraynor
162
25k
Agile that works and the tools we love
rasmusluckow
331
21k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
640
Balancing Empowerment & Direction
lara
5
710
Building an army of robots
kneath
306
46k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.7k
Facilitating Awesome Meetings
lara
57
6.6k
The World Runs on Bad Software
bkeepers
PRO
72
11k
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