Skip to content

Commit 77b7807

Browse files
Copilotkwerle
andcommitted
Fix module and class scope closing by passing false for close_siblings
Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com>
1 parent d0e1e5d commit 77b7807

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

lib/ruby_language_server/scope_parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def visit_class_node(node)
4141
end_line = node.location.end_line
4242
column = node.location.start_column
4343

44-
scope = push_scope(ScopeData::Scope::TYPE_CLASS, name, line, column, end_line)
44+
scope = push_scope(ScopeData::Scope::TYPE_CLASS, name, line, column, end_line, false)
4545

4646
# Handle superclass
4747
if node.superclass
@@ -60,7 +60,7 @@ def visit_module_node(node)
6060
end_line = node.location.end_line
6161
column = node.location.start_column
6262

63-
push_scope(ScopeData::Scope::TYPE_MODULE, name, line, column, end_line)
63+
push_scope(ScopeData::Scope::TYPE_MODULE, name, line, column, end_line, false)
6464
super
6565
pop_scope
6666
end

spec/lib/ruby_language_server/scope_parser_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,69 @@ def some_method
167167
assert_equal('item', scope_parser.root_scope.self_and_descendants.last.variables.first.name)
168168
end
169169
end
170+
171+
describe 'sibling modules' do
172+
let(:sibling_modules_source) do
173+
<<-RUBY
174+
module Foo
175+
module Bar
176+
end
177+
module Baz
178+
end
179+
end
180+
RUBY
181+
end
182+
let(:scope_parser) { RubyLanguageServer::ScopeParser.new(sibling_modules_source) }
183+
184+
it 'should place sibling modules at the same level' do
185+
foo = scope_parser.root_scope.children.first
186+
assert_equal('Foo', foo.name)
187+
188+
# Both Bar and Baz should be children of Foo, not Bar containing Baz
189+
children = foo.children
190+
assert_equal(2, children.size, "Foo should have 2 children, but has #{children.size}")
191+
192+
bar = children.detect { |c| c.name == 'Bar' }
193+
baz = children.detect { |c| c.name == 'Baz' }
194+
195+
assert_not_nil(bar, "Bar should be a child of Foo")
196+
assert_not_nil(baz, "Baz should be a child of Foo")
197+
198+
# Verify Baz is not a child of Bar
199+
assert_equal(0, bar.children.size, "Bar should have no children, but has #{bar.children.size}")
200+
end
201+
end
202+
203+
describe 'sibling classes' do
204+
let(:sibling_classes_source) do
205+
<<-RUBY
206+
module Foo
207+
class Bar
208+
end
209+
class Baz
210+
end
211+
end
212+
RUBY
213+
end
214+
let(:scope_parser) { RubyLanguageServer::ScopeParser.new(sibling_classes_source) }
215+
216+
it 'should place sibling classes at the same level' do
217+
foo = scope_parser.root_scope.children.first
218+
assert_equal('Foo', foo.name)
219+
220+
# Both Bar and Baz should be children of Foo, not Bar containing Baz
221+
children = foo.children
222+
assert_equal(2, children.size, "Foo should have 2 children, but has #{children.size}")
223+
224+
bar = children.detect { |c| c.name == 'Bar' }
225+
baz = children.detect { |c| c.name == 'Baz' }
226+
227+
assert_not_nil(bar, "Bar should be a child of Foo")
228+
assert_not_nil(baz, "Baz should be a child of Foo")
229+
230+
# Verify Baz is not a child of Bar
231+
assert_equal(0, bar.children.size, "Bar should have no children, but has #{bar.children.size}")
232+
end
233+
end
170234
end
171235
end

0 commit comments

Comments
 (0)