Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory in comment #1780

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions ext/rbs_extension/parserstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
4 changes: 3 additions & 1 deletion ext/rbs_extension/ruby_objs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ext/rbs_extension/ruby_objs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 19 additions & 3 deletions lib/rbs/ast/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions sig/comment.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading