Skip to content

Commit

Permalink
Merge pull request #350 from Afront/optimize_comment
Browse files Browse the repository at this point in the history
Optimize `push_comment` method
  • Loading branch information
soutaro committed Aug 2, 2020
2 parents f73a6b8 + 63f8632 commit b5758d5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/rbs/ast/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def hash
def to_json(*a)
{ string: string, location: location }.to_json(*a)
end

def concat(string:, location:)
@string.concat string
@location.concat location
self
end
end
end
end
15 changes: 15 additions & 0 deletions lib/rbs/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def self.concat(*locations)
locations.inject {|l1, l2| l1 + l2 }
end

def concat(*others)
others.each { |other| self << other }
self
end

def <<(other)
if other
raise "Invalid concat: buffer=#{buffer.name}, other.buffer=#{other.buffer.name}" unless other.buffer == buffer
@end_pos = other.end_pos
@source = nil
@end_loc = nil
end
self
end

def pred?(loc)
loc.is_a?(Location) &&
loc.name == name &&
Expand Down
14 changes: 6 additions & 8 deletions lib/rbs/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1164,15 +1164,13 @@ def leading_comment(location)
end

def push_comment(string, location)
new_comment = AST::Comment.new(string: string+"\n", location: location)

if (prev_comment = leading_comment(location)) && prev_comment.location.start_column == location.start_column
@comments.delete prev_comment.location.end_line
new_comment = AST::Comment.new(string: prev_comment.string + new_comment.string,
location: prev_comment.location + new_comment.location)
if (comment = leading_comment(location)) && comment.location.start_column == location.start_column
comment.concat(string: "#{string}\n", location: location)
@comments[comment.location.end_line] = comment
else
new_comment = AST::Comment.new(string: "#{string}\n", location: location)
@comments[new_comment.location.end_line] = new_comment
end

@comments[new_comment.location.end_line] = new_comment
end

def new_token(type, value = input.matched)
Expand Down
23 changes: 23 additions & 0 deletions test/rbs/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "test_helper"

class RBS::CommentTest < Minitest::Test
include TestHelper

def test_concat
buffer = RBS::Buffer.new(name: Pathname("foo.rbs"), content: "")

comment = RBS::AST::Comment.new(
string: 'foo',
location: RBS::Location.new(buffer: buffer, start_pos: 0, end_pos: 3)
)

comment.concat(
string: 'bar',
location: RBS::Location.new(buffer: buffer, start_pos: 4, end_pos: 7)
)

assert_equal "foobar", comment.string
assert_equal 0, comment.location.start_pos
assert_equal 7, comment.location.end_pos
end
end

0 comments on commit b5758d5

Please sign in to comment.