Skip to content

Commit bb72bde

Browse files
committed
Format constant declarations relative to their enclosing scope
1 parent ef0935b commit bb72bde

9 files changed

Lines changed: 24 additions & 9 deletions

File tree

lib/typeprof/core/service.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,21 @@ def completion(path, trigger, pos)
414414
end
415415
end
416416

417+
def format_declared_const_path(cpath, stack)
418+
scope_cpath =
419+
stack.reverse_each.find do |entry|
420+
(entry.is_a?(AST::ClassNode) || entry.is_a?(AST::ModuleNode)) &&
421+
entry.static_cpath &&
422+
!entry.static_cpath.empty?
423+
end&.static_cpath
424+
425+
return cpath.join("::") unless scope_cpath
426+
return cpath.join("::") unless cpath[0, scope_cpath.size] == scope_cpath
427+
428+
rel_cpath = cpath.drop(scope_cpath.size)
429+
rel_cpath.empty? ? cpath.join("::") : rel_cpath.join("::")
430+
end
431+
417432
def dump_declarations(path)
418433
stack = []
419434
out = []
@@ -465,7 +480,7 @@ def dump_declarations(path)
465480
when AST::ConstantWriteNode
466481
if node.static_cpath
467482
if event == :enter
468-
out << " " * stack.size + "#{ node.static_cpath.join("::") }: #{ node.ret.show }"
483+
out << " " * stack.size + "#{ format_declared_const_path(node.static_cpath, stack) }: #{ node.ret.show }"
469484
end
470485
end
471486
else

scenario/const/basic1.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def foo(_)
1212

1313
## assert
1414
class C
15-
C::X: Integer
15+
X: Integer
1616
end
1717
class D < C
1818
end

scenario/const/basic2.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def foo
1111
## assert
1212
C: Integer
1313
class Foo
14-
Foo::C: Float
14+
C: Float
1515
def foo: -> Integer
1616
end
1717
Foo::Bar: :bar

scenario/const/module_const.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def check
1414
## diagnostics
1515
## assert
1616
module M
17-
M::C: :test
17+
C: :test
1818
end
1919
class C
2020
include M

scenario/known-issues/multi-const-write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ class C
66

77
## assert
88
class C
9-
C::D: (Integer | String)
9+
D: (Integer | String)
1010
end

scenario/variable/and_write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_attr=(x)
3838
## diagnostics
3939
## assert
4040
class C
41-
C::D: :ConstD
41+
D: :ConstD
4242
def get_lv: -> (:LVar | :LVar0)
4343
def set_iv: -> (:IVar | :IVar0)
4444
def get_iv: -> (:IVar | :IVar0)

scenario/variable/operator_write.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_attr=(x)
4242
## diagnostics
4343
## assert
4444
class C
45-
C::D: Integer
46-
C::D: Integer
45+
D: Integer
46+
D: Integer
4747
def get_lv: -> Integer
4848
def set_iv: -> Integer
4949
def get_iv: -> Integer

scenario/variable/or_write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_attr=(x)
3636
## diagnostics
3737
## assert
3838
class C
39-
C::D: :ConstD
39+
D: :ConstD
4040
def get_lv: -> :LVar
4141
def set_iv: -> :IVar
4242
def get_iv: -> :IVar

0 commit comments

Comments
 (0)