Support LSP 3.17 positionEncoding negotiation#442
Merged
mame merged 2 commits intoruby:masterfrom Apr 28, 2026
Merged
Conversation
Adds `position_encoding:` option to `TypeProf::Core::Service.new`. FileContext stores the encoding and computes Prism::Location columns accordingly. Default is `Encoding::UTF_16LE` (preserves existing behavior). UTF-8 uses Prism's native `start_column`/`end_column` (= byte offsets), because Prism's `code_units_cache(Encoding::UTF_8)` reports code points rather than bytes — the latter is what the LSP 3.17 spec defines for `utf-8`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Implements `general.positionEncodings` negotiation per LSP 3.17 spec. The server picks the first encoding from the client's preference list that it supports (`utf-8` / `utf-16` / `utf-32`) and reports it back via `capabilities.positionEncoding`. Falls back to UTF-16 (mandatory per spec) if the client doesn't propose any supported encoding. The negotiated value flows into per-workspace Services through `core_options.merge(position_encoding: ...)`, so each Service computes column positions in the agreed encoding. See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocuments Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ahogappa
commented
Apr 25, 2026
| attr_reader :path, :comments, :position_encoding | ||
| def initialize(path, position_encoding = nil, prism_source = nil, comments = nil) | ||
| @path = path | ||
| @position_encoding = position_encoding || Encoding::UTF_16LE |
Contributor
Author
There was a problem hiding this comment.
FYI: given that Ruby's default source encoding is UTF-8, UTF-8 would arguably be a more natural default.
However, switching from UTF-16LE would be a breaking change for existing users, so this PR keeps UTF-16LE.
mame
approved these changes
Apr 28, 2026
Member
mame
left a comment
There was a problem hiding this comment.
Thanks, I would like to give it a try!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements LSP 3.17
positionEncodingnegotiation. Today TypeProf hardcodes UTF-16LE columns and omitspositionEncodingfrom its capabilities response, so modern clients that prefer UTF-8 (Helix, Zed, Neovim) all fall back to UTF-16.This PR also exposes the encoding as
Service.new(position_encoding:)for embedders.The motivating use case is ruby-minify, which cross-references TypeProf nodes against Prism re-parses to drive variable/method renaming.
Prism's native columns are byte-based — equivalent to LSP
utf-8— soService.new(position_encoding: Encoding::UTF_8)makes the two coordinate systems line up.Without this option, ruby-minify reaches into TypeProf via
node.instance_variable_get(:@raw_node).locationto bypass UTF-16 columns.Behavior change
None by default. Clients that don't send
general.positionEncodings, and embedders that don't passposition_encoding, still get UTF-16LE.Implementation note
The server picks the first mutually-supported encoding from the client's preference-ordered list (
utf-8/utf-16/utf-32), falling back toutf-16per spec.FileContext#column_offsets_forreturns columns in the configured encoding. For UTF-8 it uses Prism's nativestart_column/end_column(= byte offsets) — note that Prism'scode_units_cache(Encoding::UTF_8)reports code points rather than bytes, which would violate the LSP spec.Verification
End-to-end against Helix, which proposes
general.positionEncodings: ["utf-8", "utf-32", "utf-16"]. Source — line 4 references undefinedあfoo, whereあis 3 bytes in UTF-8 / 1 code unit in UTF-16:Helix
helix_lsp::transportlog —InitializeResultMaster (
ruby/typeprof0.31.1):This PR:
The only difference is the new
"positionEncoding":"utf-8"field, which signals that subsequent column values use UTF-8 byte offsets.test/core/position_encoding_test.rb(UTF-8 / UTF-16 / UTF-32 columns).test/lsp/lsp_test.rb(negotiation matrix).positionEncoding: "utf-8"response and correct diagnostic range on a non-ASCII identifier.Spec: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocuments