diff --git a/lib/typeprof/core/ast/base.rb b/lib/typeprof/core/ast/base.rb index c23eefc8..2d4f0d4e 100644 --- a/lib/typeprof/core/ast/base.rb +++ b/lib/typeprof/core/ast/base.rb @@ -270,7 +270,7 @@ def initialize(sym, code_range, ret) @ret = ret end - attr_reader :lenv, :prev_node, :ret + attr_reader :lenv, :prev_node, :code_range, :ret def boxes(_) [] diff --git a/lib/typeprof/core/ast/method.rb b/lib/typeprof/core/ast/method.rb index d57b0208..636a19ca 100644 --- a/lib/typeprof/core/ast/method.rb +++ b/lib/typeprof/core/ast/method.rb @@ -2,28 +2,23 @@ module TypeProf::Core class AST def self.get_rbs_comment_before(raw_node, lenv) comments = Fiber[:comments] - i = comments.bsearch_index {|comment| comment.location.start_line >= raw_node.location.start_line } || comments.size - lineno = raw_node.location.start_line + node_line = raw_node.location.start_line + last_comment_index = comments.bsearch_index {|c| c.location.start_line >= node_line } || comments.size rbs_comments = [] - while i > 0 - i -= 1 - lineno -= 1 + expected_line = node_line - 1 + (last_comment_index - 1).downto(0) do |i| comment = comments[i] + break unless comment.location.start_line == expected_line && comment.location.slice.start_with?("#:") comment_loc = comment.location comment_text = comment_loc.slice - if comment_loc.start_line == lineno && comment_text.start_with?("#:") - rbs_comments[comment_loc.start_line] = " " * (comment_loc.start_column + 2) + comment_text[2..] - else - break - end + rbs_comments[comment_loc.start_line - 1] = " " * (comment_loc.start_column + 2) + comment_text[2..] + expected_line -= 1 end return nil if rbs_comments.empty? rbs_comments = rbs_comments.map {|line| line || "" }.join("\n") method_type = RBS::Parser.parse_method_type(rbs_comments) if method_type AST.create_rbs_func_type(method_type, method_type.type_params, method_type.block, lenv) - else - nil end rescue RBS::ParsingError # TODO: report the error diff --git a/scenario/rbs/inline-hover.rb b/scenario/rbs/inline-hover.rb index bb9a5dad..41ba7b6c 100644 --- a/scenario/rbs/inline-hover.rb +++ b/scenario/rbs/inline-hover.rb @@ -1,11 +1,25 @@ ## update: test.rb #: (Integer) -> void -def check(var) # TODO: fix "??? no type ???" +def check(var) var end ## hover: test.rb:2:11 -??? no type ??? +Integer ## hover: test.rb:3:3 Integer + +## update: test2.rb +class Foo + #: (String) -> void + def bar(x) + x + end +end + +## hover: test2.rb:3:10 +String + +## hover: test2.rb:4:4 +String