Migrate consumers to new byte-based Location API - Rewriter: use start_byte/end_byte, start_byte_column, bytesplice - Errors: use start_byte_column/end_byte_column for marker alignment - Writer: use start_byte_column for indentation - Locator: add byte_column/char_column keyword arguments to find/find2, deprecate column keyword, add char_column_to_byte_column helper - CommentBlock, LocationHelper: use Prism start_offset (byte) instead of start_character_offset (char) - ParserAux, Declarations: use start_byte/end_byte - Update test expectation for byte-based column values Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
diff --git a/lib/rbs/ast/ruby/comment_block.rb b/lib/rbs/ast/ruby/comment_block.rb index 34f009c..9956551 100644 --- a/lib/rbs/ast/ruby/comment_block.rb +++ b/lib/rbs/ast/ruby/comment_block.rb
@@ -25,9 +25,9 @@ offsets << tuple - start_char = comment.location.start_character_offset + tuple[1] - end_char = comment.location.end_character_offset - ranges << (start_char ... end_char) + start_byte = comment.location.start_offset + tuple[1] + end_byte = comment.location.end_offset + ranges << (start_byte ... end_byte) end @comment_buffer = source_buffer.sub_buffer(lines: ranges) @@ -53,7 +53,7 @@ def line_starts offsets.map do |comment, prefix_size| - comment.location.start_character_offset + prefix_size + comment.location.start_offset + prefix_size end end @@ -168,7 +168,7 @@ def text(comment_index) range = comment_buffer.ranges[comment_index] - comment_buffer.content[range] or raise + comment_buffer.content.byteslice(range) or raise end def line_location(start_line, end_line)
diff --git a/lib/rbs/ast/ruby/declarations.rb b/lib/rbs/ast/ruby/declarations.rb index 4651db0..6994c22 100644 --- a/lib/rbs/ast/ruby/declarations.rb +++ b/lib/rbs/ast/ruby/declarations.rb
@@ -44,8 +44,8 @@ if type_annotation Location.new( type_name_location.buffer, - type_name_location.start_pos, - type_annotation.location.end_pos + type_name_location.start_byte, + type_annotation.location.end_byte ) else type_name_location
diff --git a/lib/rbs/ast/ruby/helpers/location_helper.rb b/lib/rbs/ast/ruby/helpers/location_helper.rb index 677919c..9620ffe 100644 --- a/lib/rbs/ast/ruby/helpers/location_helper.rb +++ b/lib/rbs/ast/ruby/helpers/location_helper.rb
@@ -6,7 +6,7 @@ module Helpers module LocationHelper def rbs_location(location) - Location.new(buffer, location.start_character_offset, location.end_character_offset) + Location.new(buffer, location.start_offset, location.end_offset) end end end
diff --git a/lib/rbs/errors.rb b/lib/rbs/errors.rb index 9f4e51e..4fd6c82 100644 --- a/lib/rbs/errors.rb +++ b/lib/rbs/errors.rb
@@ -34,8 +34,8 @@ # Support only one line return msg unless location.start_line == location.end_line - indent = " " * location.start_column - marker = "^" * ([location.end_column - location.start_column, 1].max or raise) + indent = " " * location.start_byte_column + marker = "^" * ([location.end_byte_column - location.start_byte_column, 1].max or raise) io = StringIO.new io.puts msg
diff --git a/lib/rbs/locator.rb b/lib/rbs/locator.rb index 88363e4..eea1995 100644 --- a/lib/rbs/locator.rb +++ b/lib/rbs/locator.rb
@@ -10,8 +10,16 @@ @decls = decls end - def find(line:, column:) - pos = buffer.loc_to_pos([line, column]) + # Find RBS elements at the given position. + # + # Returns a list of components, inner component comes first. + # + # @param line [Integer] Line number (1-origin) + # @param byte_column [Integer] Byte offset within the line (0-origin) + # @param char_column [Integer] Character offset within the line (0-origin). Converted to byte offset internally. + # @param column [Integer] Deprecated. Treated as char column for backwards compatibility. + def find(line:, column: nil, byte_column: nil, char_column: nil) + pos = resolve_position(line: line, column: column, byte_column: byte_column, char_column: char_column) dirs.each do |dir| array = [] #: Array[component] @@ -26,8 +34,14 @@ [] end - def find2(line:, column:) - path = find(line: line, column: column) + # Find RBS elements at the given position, returning the inner most symbol and outer components. + # + # @param line [Integer] Line number (1-origin) + # @param byte_column [Integer] Byte offset within the line (0-origin) + # @param char_column [Integer] Character offset within the line (0-origin). Converted to byte offset internally. + # @param column [Integer] Deprecated. Treated as char column for backwards compatibility. + def find2(line:, column: nil, byte_column: nil, char_column: nil) + path = find(line: line, column: column, byte_column: byte_column, char_column: char_column) return if path.empty? @@ -238,10 +252,33 @@ def test_loc(pos, location:) if location - location.start_pos <= pos && pos <= location.end_pos + location.start_byte <= pos && pos <= location.end_byte else false end end + + private + + # Converts char column to byte column within a line. + def char_column_to_byte_column(line, char_column) #: Integer + line_range = buffer.ranges[line - 1] or return 0 + line_content = buffer.content.byteslice(line_range) or return 0 + prefix = line_content.encode(Encoding::UTF_8).chars.first(char_column).join + prefix.bytesize + end + + def resolve_position(line:, column:, byte_column:, char_column:) #: Integer + if byte_column + buffer.loc_to_pos([line, byte_column]) + elsif char_column + buffer.loc_to_pos([line, char_column_to_byte_column(line, char_column)]) + elsif column + RBS.print_warning { "`column` keyword argument of RBS::Locator#find is deprecated. Use `char_column` or `byte_column` instead." } + buffer.loc_to_pos([line, char_column_to_byte_column(line, column)]) + else + raise ArgumentError, "One of `byte_column`, `char_column`, or `column` must be specified" + end + end end end
diff --git a/lib/rbs/parser_aux.rb b/lib/rbs/parser_aux.rb index 974c54e..bae7e70 100644 --- a/lib/rbs/parser_aux.rb +++ b/lib/rbs/parser_aux.rb
@@ -23,7 +23,7 @@ resolved = magic_comment(buf) start_pos = if resolved - (resolved.location || raise).end_pos + (resolved.location || raise).end_byte else 0 end
diff --git a/lib/rbs/rewriter.rb b/lib/rbs/rewriter.rb index ec1b1d9..5ad7ba9 100644 --- a/lib/rbs/rewriter.rb +++ b/lib/rbs/rewriter.rb
@@ -13,7 +13,7 @@ def rewrite(location, string) @rewrites.each do |existing_location, _| - if location.start_pos < existing_location.end_pos && existing_location.start_pos < location.end_pos + if location.start_byte < existing_location.end_byte && existing_location.start_byte < location.end_byte raise "Overlapping rewrites: #{existing_location} and #{location}" end end @@ -23,9 +23,9 @@ end def add_comment(*locations, content:) - earliest = locations.min_by(&:start_pos) or raise "At least one location is required" - insert_pos = earliest.start_pos - indent = " " * earliest.start_column + earliest = locations.min_by(&:start_char) or raise "At least one location is required" + insert_pos = earliest.start_byte + indent = " " * earliest.start_byte_column formatted = format_comment(content, indent) @@ -35,15 +35,15 @@ def replace_comment(comment, content:) location = comment.location or raise "Comment must have a location" - indent = " " * location.start_column + indent = " " * location.start_byte_column rewrite(location, format_comment(content, indent)) end def delete_comment(comment) location = comment.location or raise "Comment must have a location" - line_start = location.start_pos - location.start_column - line_end = location.end_pos + 1 + line_start = location.start_byte - location.start_byte_column + line_end = location.end_byte + 1 loc = Location.new(buffer, line_start, line_end) rewrite(loc, "") end @@ -51,8 +51,8 @@ def string result = buffer.content.dup - @rewrites.sort_by { |location, _| location.start_pos }.reverse_each do |location, replacement| - result[location.start_pos...location.end_pos] = replacement + @rewrites.sort_by { |location, _| location.start_byte }.reverse_each do |location, replacement| + result.bytesplice(location.start_byte...location.end_byte, replacement) end result
diff --git a/lib/rbs/writer.rb b/lib/rbs/writer.rb index 4d2a1d0..b11c915 100644 --- a/lib/rbs/writer.rb +++ b/lib/rbs/writer.rb
@@ -305,7 +305,7 @@ def write_loc_source(located) if preserve? && loc = located.location - put_lines(loc.source, leading_spaces: loc.start_column) + put_lines(loc.source, leading_spaces: loc.start_byte_column) else yield end
diff --git a/sig/locator.rbs b/sig/locator.rbs index a16543b..f933f2a 100644 --- a/sig/locator.rbs +++ b/sig/locator.rbs
@@ -27,15 +27,27 @@ def initialize: (buffer: Buffer, decls: Array[AST::Declarations::t], dirs: Array[AST::Directives::t]) -> void - # Returns list of components. - # Inner component comes first. + # Find RBS elements at the given position. + # Returns a list of components, inner component comes first. # - def find: (line: Integer, column: Integer) -> Array[component] + # `line` is the line number (1-origin). + # `byte_column` is the byte offset within the line (0-origin). + # `char_column` is the character offset within the line (0-origin), converted to byte offset internally. + # + def find: (line: Integer, byte_column: Integer) -> Array[component] + | (line: Integer, char_column: Integer) -> Array[component] + | %a{deprecated: use char_column or byte_column instead} (line: Integer, column: Integer) -> Array[component] - # Returns pair of the inner most symbol and outer components. + # Find RBS elements at the given position, returning the inner most symbol and outer components. # It ensures the array starts with a AST/type component. # - def find2: (line: Integer, column: Integer) -> [Symbol?, Array[component]]? + # `line` is the line number (1-origin). + # `byte_column` is the byte offset within the line (0-origin). + # `char_column` is the character offset within the line (0-origin), converted to byte offset internally. + # + def find2: (line: Integer, byte_column: Integer) -> [Symbol?, Array[component]]? + | (line: Integer, char_column: Integer) -> [Symbol?, Array[component]]? + | %a{deprecated: use char_column or byte_column instead} (line: Integer, column: Integer) -> [Symbol?, Array[component]]? def find_in_directive: (Integer pos, AST::Directives::t, Array[component]) -> bool @@ -52,5 +64,16 @@ def find_in_loc: (Integer pos, location: Location[untyped, untyped]?, array: Array[component]) -> bool def test_loc: (Integer pos, location: Location[untyped, untyped]?) -> bool + + private + + # Converts a character column offset to a byte column offset within the given line. + # + def char_column_to_byte_column: (Integer line, Integer char_column) -> Integer + + # Resolves line and column arguments to a byte position in the buffer. + # Exactly one of `byte_column`, `char_column`, or `column` must be given. + # + def resolve_position: (line: Integer, column: Integer?, byte_column: Integer?, char_column: Integer?) -> Integer end end
diff --git a/test/rbs/type_parsing_test.rb b/test/rbs/type_parsing_test.rb index 97c5754..93e5ec1 100644 --- a/test/rbs/type_parsing_test.rb +++ b/test/rbs/type_parsing_test.rb
@@ -1027,6 +1027,6 @@ Parser.parse_type(input, byte_range: 2...) end - assert_equal "a.rbs:1:2...1:3: Syntax error: unexpected token for simple type, token=`🐈` (ErrorToken)", exn.message + assert_equal "a.rbs:1:5...1:9: Syntax error: unexpected token for simple type, token=`🐈` (ErrorToken)", exn.message end end