Skip to content

Commit baf6599

Browse files
authored
Fix documentation reference when methods are too close (#2806)
* delete line key from @comments_by_line when comment has been visited * fix 'test_completion_resolve_with_owner_present' test * add source_lines and refactor collect_comments * minimize the changes
1 parent 670defb commit baf6599

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def initialize(index, dispatcher, parse_result, file_path, collect_comments: fal
3737
parse_result.code_units_cache(@index.configuration.encoding),
3838
T.any(T.proc.params(arg0: Integer).returns(Integer), Prism::CodeUnitsCache),
3939
)
40+
@source_lines = T.let(parse_result.source.lines, T::Array[String])
4041

4142
# The nesting stack we're currently inside. Used to determine the fully qualified name of constants, but only
4243
# stored by unresolved aliases which need the original nesting to be lazily resolved
@@ -661,8 +662,7 @@ def collect_comments(node)
661662
comments = +""
662663

663664
start_line = node.location.start_line - 1
664-
start_line -= 1 unless @comments_by_line.key?(start_line)
665-
665+
start_line -= 1 unless comment_exists_at?(start_line)
666666
start_line.downto(1) do |line|
667667
comment = @comments_by_line[line]
668668
break unless comment
@@ -683,6 +683,11 @@ def collect_comments(node)
683683
comments
684684
end
685685

686+
sig { params(line: Integer).returns(T::Boolean) }
687+
def comment_exists_at?(line)
688+
@comments_by_line.key?(line) || !@source_lines[line - 1].to_s.strip.empty?
689+
end
690+
686691
sig { params(name: String).returns(String) }
687692
def fully_qualify_name(name)
688693
if @stack.empty? || name.start_with?("::")

lib/ruby_indexer/test/method_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,39 @@ def bar; end
149149
end
150150
end
151151

152+
def test_comments_documentation
153+
index(<<~RUBY)
154+
# Documentation for Foo
155+
156+
class Foo
157+
# ####################
158+
# Documentation for bar
159+
# ####################
160+
#
161+
def bar
162+
end
163+
164+
# test
165+
166+
# Documentation for baz
167+
def baz; end
168+
def ban; end
169+
end
170+
RUBY
171+
172+
foo_comment = @index["Foo"].first.comments
173+
assert_equal("Documentation for Foo", foo_comment)
174+
175+
bar_comment = @index["bar"].first.comments
176+
assert_equal("####################\nDocumentation for bar\n####################\n", bar_comment)
177+
178+
baz_comment = @index["baz"].first.comments
179+
assert_equal("Documentation for baz", baz_comment)
180+
181+
ban_comment = @index["ban"].first.comments
182+
assert_empty(ban_comment)
183+
end
184+
152185
def test_method_with_parameters
153186
index(<<~RUBY)
154187
class Foo

test/requests/completion_resolve_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def initialize
5050
end
5151
5252
class Foo
53-
# Foo!
5453
def initialize
54+
# Foo!
5555
@a = 1
5656
end
5757
end

0 commit comments

Comments
 (0)