diff --git a/lib/typeprof/core/service.rb b/lib/typeprof/core/service.rb index a79ca1c0..887a3973 100644 --- a/lib/typeprof/core/service.rb +++ b/lib/typeprof/core/service.rb @@ -414,6 +414,21 @@ def completion(path, trigger, pos) end end + def format_declared_const_path(cpath, stack) + scope_cpath = + stack.reverse_each.find do |entry| + (entry.is_a?(AST::ClassNode) || entry.is_a?(AST::ModuleNode)) && + entry.static_cpath && + !entry.static_cpath.empty? + end&.static_cpath + + return cpath.join("::") unless scope_cpath + return cpath.join("::") unless cpath[0, scope_cpath.size] == scope_cpath + + rel_cpath = cpath.drop(scope_cpath.size) + rel_cpath.empty? ? cpath.join("::") : rel_cpath.join("::") + end + def dump_declarations(path) stack = [] out = [] @@ -465,7 +480,7 @@ def dump_declarations(path) when AST::ConstantWriteNode if node.static_cpath if event == :enter - out << " " * stack.size + "#{ node.static_cpath.join("::") }: #{ node.ret.show }" + out << " " * stack.size + "#{ format_declared_const_path(node.static_cpath, stack) }: #{ node.ret.show }" end end else diff --git a/scenario/const/basic1.rb b/scenario/const/basic1.rb index 8eba87de..d6f68421 100644 --- a/scenario/const/basic1.rb +++ b/scenario/const/basic1.rb @@ -12,7 +12,7 @@ def foo(_) ## assert class C - C::X: Integer + X: Integer end class D < C end diff --git a/scenario/const/basic2.rb b/scenario/const/basic2.rb index 13be07a2..93d631a0 100644 --- a/scenario/const/basic2.rb +++ b/scenario/const/basic2.rb @@ -11,7 +11,7 @@ def foo ## assert C: Integer class Foo - Foo::C: Float + C: Float def foo: -> Integer end Foo::Bar: :bar diff --git a/scenario/known-issues/const-scope.rb b/scenario/const/const-scope.rb similarity index 100% rename from scenario/known-issues/const-scope.rb rename to scenario/const/const-scope.rb diff --git a/scenario/const/module_const.rb b/scenario/const/module_const.rb index 94cbad33..0431226b 100644 --- a/scenario/const/module_const.rb +++ b/scenario/const/module_const.rb @@ -14,7 +14,7 @@ def check ## diagnostics ## assert module M - M::C: :test + C: :test end class C include M diff --git a/scenario/known-issues/multi-const-write.rb b/scenario/known-issues/multi-const-write.rb index 29c06d41..8fe4f630 100644 --- a/scenario/known-issues/multi-const-write.rb +++ b/scenario/known-issues/multi-const-write.rb @@ -6,5 +6,5 @@ class C ## assert class C - C::D: (Integer | String) + D: (Integer | String) end diff --git a/scenario/variable/and_write.rb b/scenario/variable/and_write.rb index e5ac0c2f..2bfaa684 100644 --- a/scenario/variable/and_write.rb +++ b/scenario/variable/and_write.rb @@ -38,7 +38,7 @@ def test_attr=(x) ## diagnostics ## assert class C - C::D: :ConstD + D: :ConstD def get_lv: -> (:LVar | :LVar0) def set_iv: -> (:IVar | :IVar0) def get_iv: -> (:IVar | :IVar0) diff --git a/scenario/variable/operator_write.rb b/scenario/variable/operator_write.rb index 1867e6c5..d4fb7094 100644 --- a/scenario/variable/operator_write.rb +++ b/scenario/variable/operator_write.rb @@ -42,8 +42,8 @@ def test_attr=(x) ## diagnostics ## assert class C - C::D: Integer - C::D: Integer + D: Integer + D: Integer def get_lv: -> Integer def set_iv: -> Integer def get_iv: -> Integer diff --git a/scenario/variable/or_write.rb b/scenario/variable/or_write.rb index e79a4047..56ae9f50 100644 --- a/scenario/variable/or_write.rb +++ b/scenario/variable/or_write.rb @@ -36,7 +36,7 @@ def test_attr=(x) ## diagnostics ## assert class C - C::D: :ConstD + D: :ConstD def get_lv: -> :LVar def set_iv: -> :IVar def get_iv: -> :IVar