diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 9add22ba2..31cf38d4f 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -244,30 +244,11 @@ comment *comment_get_comment(comment *com, int line) { VALUE comment_to_ruby(comment *com, VALUE buffer) { VALUE content = rb_funcall(buffer, rb_intern("content"), 0); - rb_encoding *enc = rb_enc_get(content); - VALUE string = rb_enc_str_new_cstr("", enc); - - int hash_bytes = rb_enc_codelen('#', enc); - int space_bytes = rb_enc_codelen(' ', enc); - - for (size_t i = 0; i < com->line_count; i++) { - token tok = com->tokens[i]; - - char *comment_start = RSTRING_PTR(content) + tok.range.start.byte_pos + hash_bytes; - int comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; - unsigned char c = rb_enc_mbc_to_codepoint(comment_start, RSTRING_END(content), enc); - - if (c == ' ') { - comment_start += space_bytes; - comment_bytes -= space_bytes; - } - - rb_str_cat(string, comment_start, comment_bytes); - rb_str_cat_cstr(string, "\n"); - } return rbs_ast_comment( - string, + content, + INT2FIX(com->start.line), + INT2FIX(com->end.line), rbs_location_pp(buffer, &com->start, &com->end) ); } diff --git a/ext/rbs_extension/ruby_objs.c b/ext/rbs_extension/ruby_objs.c index 2a92d1c56..1c5c0fab5 100644 --- a/ext/rbs_extension/ruby_objs.c +++ b/ext/rbs_extension/ruby_objs.c @@ -272,10 +272,12 @@ VALUE rbs_method_type(VALUE type_params, VALUE type, VALUE block, VALUE location ); } -VALUE rbs_ast_comment(VALUE string, VALUE location) { +VALUE rbs_ast_comment(VALUE string, VALUE start_line, VALUE end_line, VALUE location) { VALUE args = rb_hash_new(); rb_hash_aset(args, ID2SYM(rb_intern("string")), string); rb_hash_aset(args, ID2SYM(rb_intern("location")), location); + rb_hash_aset(args, ID2SYM(rb_intern("start_line")), start_line); + rb_hash_aset(args, ID2SYM(rb_intern("end_line")), end_line); return CLASS_NEW_INSTANCE( RBS_AST_Comment, diff --git a/ext/rbs_extension/ruby_objs.h b/ext/rbs_extension/ruby_objs.h index d9ee32262..d70ed22bc 100644 --- a/ext/rbs_extension/ruby_objs.h +++ b/ext/rbs_extension/ruby_objs.h @@ -5,7 +5,7 @@ VALUE rbs_alias(VALUE typename, VALUE args, VALUE location); VALUE rbs_ast_annotation(VALUE string, VALUE location); -VALUE rbs_ast_comment(VALUE string, VALUE location); +VALUE rbs_ast_comment(VALUE string, VALUE start_line, VALUE end_line, VALUE location); VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE location); VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment); VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location); diff --git a/lib/rbs/ast/comment.rb b/lib/rbs/ast/comment.rb index 62db2c806..d333a6a9e 100644 --- a/lib/rbs/ast/comment.rb +++ b/lib/rbs/ast/comment.rb @@ -3,18 +3,34 @@ module RBS module AST class Comment - attr_reader :string attr_reader :location - def initialize(string:, location:) - @string = string + def initialize(string:, location:, start_line: nil, end_line: nil) + @string_base = string + @start_line = start_line + @end_line = end_line @location = location end + def string + @string ||= begin + if (start_line = @start_line) && (end_line = @end_line) + lines = @string_base.lines[(start_line - 1)..(end_line - 1)] or raise + lines.map { |line| line.sub(/^\s*#\s?/, '')}.join("\n") + else + @string_base + end + end + end + def ==(other) other.is_a?(Comment) && other.string == string end + def string + raise 'string called' + end + alias eql? == def hash diff --git a/sig/comment.rbs b/sig/comment.rbs index 5e0ffd379..b67af3ae0 100644 --- a/sig/comment.rbs +++ b/sig/comment.rbs @@ -12,7 +12,12 @@ module RBS attr_reader string: String attr_reader location: Location[bot, bot]? + @string_base: String + @start_line: Integer? + @end_line: Integer? + def initialize: (string: String, location: Location[bot, bot]?) -> void + | (string: String, start_line: Integer, end_line: Integer, location: Location[bot, bot]?) -> void def ==: (untyped other) -> bool