rbs 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/core/file.rbs CHANGED
@@ -1492,7 +1492,7 @@ class File::Stat < Object
1492
1492
  # Create a File::Stat object for the given file name (raising an exception if
1493
1493
  # the file doesn't exist).
1494
1494
  #
1495
- def initialize: (String file) -> Object
1495
+ def initialize: (String file) -> void
1496
1496
 
1497
1497
  # <!--
1498
1498
  # rdoc-file=file.c
@@ -1826,8 +1826,6 @@ class File::Stat < Object
1826
1826
  #
1827
1827
  def rdev_minor: () -> Integer
1828
1828
 
1829
- def read: (?int? length, ?string outbuf) -> String?
1830
-
1831
1829
  # <!--
1832
1830
  # rdoc-file=file.c
1833
1831
  # - stat.readable? -> true or false
data/core/kernel.rbs CHANGED
@@ -275,8 +275,6 @@
275
275
  #
276
276
  %a{annotate:rdoc:source:from=object.c}
277
277
  module Kernel : BasicObject
278
- private
279
-
280
278
  # <!--
281
279
  # rdoc-file=vm_backtrace.c
282
280
  # - caller(start=1, length=nil) -> array or nil
@@ -1986,7 +1984,11 @@ module Kernel : BasicObject
1986
1984
  # Time.new #=> 2008-03-08 19:56:22 +0900
1987
1985
  #
1988
1986
  def self?.sleep: () -> bot
1989
- | (Numeric duration) -> Integer
1987
+ | ((Integer | Float | _Divmod) duration) -> Integer
1988
+
1989
+ interface _Divmod
1990
+ def divmod: (Numeric) -> [ Numeric, Numeric ]
1991
+ end
1990
1992
 
1991
1993
  # <!--
1992
1994
  # rdoc-file=io.c
data/docs/syntax.md CHANGED
@@ -279,13 +279,12 @@ _member_ ::= _ivar-member_ # Ivar definition
279
279
  | _extend-member_ # Mixin (extend)
280
280
  | _prepend-member_ # Mixin (prepend)
281
281
  | _alias-member_ # Alias
282
- | `public` # Public
283
- | `private` # Private
282
+ | _visibility-member_ # Visibility member
284
283
 
285
284
  _ivar-member_ ::= _ivar-name_ `:` _type_
286
285
 
287
- _method-member_ ::= `def` _method-name_ `:` _method-types_ # Instance method
288
- | `def self.` _method-name_ `:` _method-types_ # Singleton method
286
+ _method-member_ ::= _visibility_ `def` _method-name_ `:` _method-types_ # Instance method
287
+ | _visibility_ `def self.` _method-name_ `:` _method-types_ # Singleton method
289
288
  | `def self?.` _method-name_ `:` _method-types_ # Singleton and instance method
290
289
 
291
290
  _method-types_ ::= _method-type-parameters_ _method-type_ # Single method type
@@ -295,9 +294,11 @@ _method-types_ ::= _method-type-parameters_ _method-type_
295
294
  _method-type-parameters_ ::= # Empty
296
295
  | `[` _type-variable_ `,` ... `]`
297
296
 
298
- _attribute-member_ ::= _attribute-type_ _method-name_ `:` _type_ # Attribute
299
- | _attribute-type_ _method-name_ `(` _ivar-name_ `) :` _type_ # Attribute with variable name specification
300
- | _attribute-type_ _method-name_ `() :` _type_ # Attribute without variable
297
+ _attribute-member_ ::= _visibility_ _attribute-type_ _method-name_ `:` _type_ # Attribute
298
+ | _visibility_ _attribute-type_ _method-name_ `(` _ivar-name_ `) :` _type_ # Attribute with variable name specification
299
+ | _visibility_ _attribute-type_ _method-name_ `() :` _type_ # Attribute without variable
300
+
301
+ _visibility_ ::= `public` | `private`
301
302
 
302
303
  _attribute-type_ ::= `attr_reader` | `attr_writer` | `attr_accessor`
303
304
 
@@ -310,6 +311,8 @@ _prepend-member_ ::= `prepend` _class-name_ _type-arguments_
310
311
  _alias-member_ ::= `alias` _method-name_ _method-name_
311
312
  | `alias self.` _method-name_ `self.` _method-name_
312
313
 
314
+ _visibility-member_ ::= _visibility_
315
+
313
316
  _ivar-name_ ::= /@\w+/
314
317
  _method-name_ ::= ...
315
318
  | /`[^`]+`/
@@ -328,7 +331,7 @@ An instance variable definition consists of the name of an instance variable and
328
331
 
329
332
  Method definition has several syntax variations.
330
333
 
331
- You can write `self.` or `self?.` before the name of the method to specify the kind of method: instance, singleton, or both instance and singleton.
334
+ You can write `self.` or `self?.` before the name of the method to specify the kind of method: instance, singleton, or module function.
332
335
 
333
336
  ```
334
337
  def to_s: () -> String # Defines a instance method
@@ -336,6 +339,8 @@ def self.new: () -> AnObject # Defines singleton method
336
339
  def self?.sqrt: (Numeric) -> Numeric # self? is for `module_function`s
337
340
  ```
338
341
 
342
+ `self?` method definition adds two methods: a public singleton method and a private instance method, which is equivalent to `module_function` in Ruby.
343
+
339
344
  The method type can be connected with `|`s to define an overloaded method.
340
345
 
341
346
  ```
@@ -351,6 +356,16 @@ def +: (Float | Integer) -> (Float | Integer)
351
356
  | (Numeric) -> Numeric
352
357
  ```
353
358
 
359
+ Adding `public` and `private` modifier changes the visibility of the method.
360
+
361
+ ```
362
+ private def puts: (*untyped) -> void # Defines private instance method
363
+
364
+ public def self.puts: (*untyped) -> void # Defines public singleton method
365
+
366
+ public def self?.puts: (*untyped) -> void # 🚨🚨🚨 Error: `?.` has own visibility semantics (== `module_function`) 🚨🚨🚨
367
+ ```
368
+
354
369
  ### Attribute definition
355
370
 
356
371
  Attribute definitions help to define methods and instance variables based on the convention of `attr_reader`, `attr_writer` and `attr_accessor` methods in Ruby.
@@ -374,6 +389,14 @@ attr_accessor people (): Array[Person]
374
389
  # def people=: (Array[Person]) -> Array[Person]
375
390
  ```
376
391
 
392
+ Attribute definitions can have the `public` and `private` modifiers like method definitions:
393
+
394
+ ```
395
+ private attr_accessor id: Integer
396
+
397
+ private attr_reader self.name: String
398
+ ```
399
+
377
400
  ### Mixin (include), Mixin (extend), Mixin (prepend)
378
401
 
379
402
  You can define mixins between class and modules.
@@ -402,11 +425,31 @@ def map: [X] () { (String) -> X } -> Array[X]
402
425
  alias collect map # `#collect` has the same type with `map`
403
426
  ```
404
427
 
405
- ### `public`, `private`
428
+ ### Visibility member
429
+
430
+ Visibility member allows specifying the default visibility of instance methods and instance attributes.
431
+
432
+ ```rbs
433
+ public
434
+
435
+ def foo: () -> void # public instance method
436
+
437
+ attr_reader name: String # public instance attribute
438
+
439
+ private
406
440
 
407
- `public` and `private` allows specifying the visibility of methods.
441
+ def bar: () -> void # private instance method
408
442
 
409
- These work only as _statements_, not per-method specifier.
443
+ attr_reader email: String # private instance attribute
444
+ ```
445
+
446
+ The visibility _modifiers_ overwrite the default visibility per member bases.
447
+
448
+ The visibility member requires a new line `\n` after the token.
449
+
450
+ ```rbs
451
+ private alias foo bar # Syntax error
452
+ ```
410
453
 
411
454
  ## Declarations
412
455
 
@@ -1,3 +1,4 @@
1
1
  require 'mkmf'
2
2
  $INCFLAGS << " -I$(top_srcdir)" if $extmk
3
+ $CFLAGS += " -std=c99 "
3
4
  create_makefile 'rbs_extension'
@@ -149,6 +149,11 @@ unsigned int peek(lexstate *state);
149
149
  * */
150
150
  void skip(lexstate *state);
151
151
 
152
+ /**
153
+ * Skip n characters.
154
+ * */
155
+ void skipn(lexstate *state, size_t size);
156
+
152
157
  /**
153
158
  * Return new token with given type.
154
159
  * */
@@ -134,6 +134,12 @@ void skip(lexstate *state) {
134
134
  }
135
135
  }
136
136
 
137
+ void skipn(lexstate *state, size_t size) {
138
+ for (size_t i = 0; i < size; i ++) {
139
+ skip(state);
140
+ }
141
+ }
142
+
137
143
  char *peek_token(lexstate *state, token tok) {
138
144
  return RSTRING_PTR(state->string) + tok.range.start.byte_pos;
139
145
  }
@@ -1430,6 +1430,8 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1430
1430
 
1431
1431
  /**
1432
1432
  * def_member ::= {kDEF} method_name `:` <method_types>
1433
+ * | {kPRIVATE2} kDEF method_name `:` <method_types>
1434
+ * | {kPUBLIC2} kDEF method_name `:` <method_types>
1433
1435
  *
1434
1436
  * method_types ::= {} <method_type>
1435
1437
  * | {} <`...`>
@@ -1440,30 +1442,61 @@ InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool all
1440
1442
  * */
1441
1443
  VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) {
1442
1444
  range member_range;
1445
+ range visibility_range;
1443
1446
  range keyword_range;
1444
1447
  range name_range;
1445
1448
  range kind_range;
1446
1449
  range overload_range = NULL_RANGE;
1447
1450
 
1448
- keyword_range = state->current_token.range;
1449
- member_range.start = keyword_range.start;
1451
+ VALUE visibility;
1450
1452
 
1451
- comment_pos = nonnull_pos_or(comment_pos, keyword_range.start);
1453
+ member_range.start = state->current_token.range.start;
1454
+ comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1452
1455
  VALUE comment = get_comment(state, comment_pos.line);
1453
1456
 
1457
+ switch (state->current_token.type)
1458
+ {
1459
+ case kPRIVATE:
1460
+ visibility_range = state->current_token.range;
1461
+ visibility = ID2SYM(rb_intern("private"));
1462
+ member_range.start = visibility_range.start;
1463
+ parser_advance(state);
1464
+ break;
1465
+ case kPUBLIC:
1466
+ visibility_range = state->current_token.range;
1467
+ visibility = ID2SYM(rb_intern("public"));
1468
+ member_range.start = visibility_range.start;
1469
+ parser_advance(state);
1470
+ break;
1471
+ default:
1472
+ visibility_range = NULL_RANGE;
1473
+ visibility = Qnil;
1474
+ break;
1475
+ }
1476
+
1477
+ keyword_range = state->current_token.range;
1478
+
1454
1479
  InstanceSingletonKind kind;
1455
1480
  if (instance_only) {
1456
1481
  kind_range = NULL_RANGE;
1457
1482
  kind = INSTANCE_KIND;
1458
1483
  } else {
1459
- kind = parse_instance_singleton_kind(state, true, &kind_range);
1484
+ kind = parse_instance_singleton_kind(state, NIL_P(visibility), &kind_range);
1460
1485
  }
1461
1486
 
1462
1487
  VALUE name = parse_method_name(state, &name_range);
1463
1488
  VALUE method_types = rb_ary_new();
1464
1489
  VALUE overload = Qfalse;
1465
1490
 
1466
- parser_advance_assert(state, pCOLON);
1491
+ if (state->next_token.type == pDOT && RB_SYM2ID(name) == rb_intern("self?")) {
1492
+ raise_syntax_error(
1493
+ state,
1494
+ state->next_token,
1495
+ "`self?` method cannot have visibility"
1496
+ );
1497
+ } else {
1498
+ parser_advance_assert(state, pCOLON);
1499
+ }
1467
1500
 
1468
1501
  parser_push_typevar_table(state, kind != INSTANCE_KIND);
1469
1502
 
@@ -1533,6 +1566,7 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1533
1566
  rbs_loc_add_required_child(loc, rb_intern("name"), name_range);
1534
1567
  rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
1535
1568
  rbs_loc_add_optional_child(loc, rb_intern("overload"), overload_range);
1569
+ rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);
1536
1570
 
1537
1571
  return rbs_ast_members_method_definition(
1538
1572
  name,
@@ -1541,7 +1575,8 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1541
1575
  annotations,
1542
1576
  location,
1543
1577
  comment,
1544
- overload
1578
+ overload,
1579
+ visibility
1545
1580
  );
1546
1581
  }
1547
1582
 
@@ -1839,10 +1874,14 @@ VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
1839
1874
 
1840
1875
  /*
1841
1876
  attribute_member ::= {attr_keyword} attr_name attr_var `:` <type>
1877
+ | {visibility} attr_keyword attr_name attr_var `:` <type>
1842
1878
  | {attr_keyword} `self` `.` attr_name attr_var `:` <type>
1879
+ | {visibility} attr_keyword `self` `.` attr_name attr_var `:` <type>
1843
1880
 
1844
1881
  attr_keyword ::= `attr_reader` | `attr_writer` | `attr_accessor`
1845
1882
 
1883
+ visibility ::= `public` | `private`
1884
+
1846
1885
  attr_var ::= # empty
1847
1886
  | `(` tAIDENT `)` # Ivar name
1848
1887
  | `(` `)` # No variable
@@ -1850,7 +1889,7 @@ VALUE parse_visibility_member(parserstate *state, VALUE annotations) {
1850
1889
  VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) {
1851
1890
  range member_range;
1852
1891
  range keyword_range, name_range, colon_range;
1853
- range kind_range = NULL_RANGE, ivar_range = NULL_RANGE, ivar_name_range = NULL_RANGE;
1892
+ range kind_range = NULL_RANGE, ivar_range = NULL_RANGE, ivar_name_range = NULL_RANGE, visibility_range = NULL_RANGE;
1854
1893
 
1855
1894
  InstanceSingletonKind is_kind;
1856
1895
  VALUE klass;
@@ -1860,12 +1899,31 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
1860
1899
  VALUE type;
1861
1900
  VALUE comment;
1862
1901
  VALUE location;
1902
+ VALUE visibility;
1863
1903
  rbs_loc *loc;
1864
1904
 
1865
1905
  member_range.start = state->current_token.range.start;
1866
1906
  comment_pos = nonnull_pos_or(comment_pos, member_range.start);
1867
1907
  comment = get_comment(state, comment_pos.line);
1868
1908
 
1909
+ switch (state->current_token.type)
1910
+ {
1911
+ case kPRIVATE:
1912
+ visibility = ID2SYM(rb_intern("private"));
1913
+ visibility_range = state->current_token.range;
1914
+ parser_advance(state);
1915
+ break;
1916
+ case kPUBLIC:
1917
+ visibility = ID2SYM(rb_intern("public"));
1918
+ visibility_range = state->current_token.range;
1919
+ parser_advance(state);
1920
+ break;
1921
+ default:
1922
+ visibility = Qnil;
1923
+ visibility_range = NULL_RANGE;
1924
+ break;
1925
+ }
1926
+
1869
1927
  keyword_range = state->current_token.range;
1870
1928
  switch (state->current_token.type)
1871
1929
  {
@@ -1924,6 +1982,7 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
1924
1982
  rbs_loc_add_optional_child(loc, rb_intern("kind"), kind_range);
1925
1983
  rbs_loc_add_optional_child(loc, rb_intern("ivar"), ivar_range);
1926
1984
  rbs_loc_add_optional_child(loc, rb_intern("ivar_name"), ivar_name_range);
1985
+ rbs_loc_add_optional_child(loc, rb_intern("visibility"), visibility_range);
1927
1986
 
1928
1987
  return rbs_ast_members_attribute(
1929
1988
  klass,
@@ -1933,7 +1992,8 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
1933
1992
  kind,
1934
1993
  annotations,
1935
1994
  location,
1936
- comment
1995
+ comment,
1996
+ visibility
1937
1997
  );
1938
1998
  }
1939
1999
 
@@ -2113,7 +2173,6 @@ VALUE parse_module_members(parserstate *state) {
2113
2173
  member = parse_alias_member(state, false, annot_pos, annotations);
2114
2174
  break;
2115
2175
 
2116
-
2117
2176
  case tAIDENT:
2118
2177
  case tA2IDENT:
2119
2178
  case kSELF:
@@ -2128,7 +2187,23 @@ VALUE parse_module_members(parserstate *state) {
2128
2187
 
2129
2188
  case kPUBLIC:
2130
2189
  case kPRIVATE:
2131
- member = parse_visibility_member(state, annotations);
2190
+ if (state->next_token.range.start.line == state->current_token.range.start.line) {
2191
+ switch (state->next_token.type)
2192
+ {
2193
+ case kDEF:
2194
+ member = parse_member_def(state, false, true, annot_pos, annotations);
2195
+ break;
2196
+ case kATTRREADER:
2197
+ case kATTRWRITER:
2198
+ case kATTRACCESSOR:
2199
+ member = parse_attribute_member(state, annot_pos, annotations);
2200
+ break;
2201
+ default:
2202
+ raise_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier");
2203
+ }
2204
+ } else {
2205
+ member = parse_visibility_member(state, annotations);
2206
+ }
2132
2207
  break;
2133
2208
 
2134
2209
  default:
@@ -400,7 +400,7 @@ VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE
400
400
  );
401
401
  }
402
402
 
403
- VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE types, VALUE annotations, VALUE location, VALUE comment, VALUE overload) {
403
+ VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE types, VALUE annotations, VALUE location, VALUE comment, VALUE overload, VALUE visibility) {
404
404
  VALUE args = rb_hash_new();
405
405
  rb_hash_aset(args, ID2SYM(rb_intern("name")), name);
406
406
  rb_hash_aset(args, ID2SYM(rb_intern("kind")), kind);
@@ -409,6 +409,7 @@ VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE types, VAL
409
409
  rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
410
410
  rb_hash_aset(args, ID2SYM(rb_intern("comment")), comment);
411
411
  rb_hash_aset(args, ID2SYM(rb_intern("overload")), overload);
412
+ rb_hash_aset(args, ID2SYM(rb_intern("visibility")), visibility);
412
413
 
413
414
  return CLASS_NEW_INSTANCE(
414
415
  RBS_AST_Members_MethodDefinition,
@@ -446,7 +447,7 @@ VALUE rbs_ast_members_mixin(VALUE klass, VALUE name, VALUE module_args, VALUE an
446
447
  );
447
448
  }
448
449
 
449
- VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment) {
450
+ VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) {
450
451
  VALUE args = rb_hash_new();
451
452
  rb_hash_aset(args, ID2SYM(rb_intern("name")), name);
452
453
  rb_hash_aset(args, ID2SYM(rb_intern("type")), type);
@@ -455,6 +456,7 @@ VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_
455
456
  rb_hash_aset(args, ID2SYM(rb_intern("annotations")), annotations);
456
457
  rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
457
458
  rb_hash_aset(args, ID2SYM(rb_intern("comment")), comment);
459
+ rb_hash_aset(args, ID2SYM(rb_intern("visibility")), visibility);
458
460
 
459
461
  return CLASS_NEW_INSTANCE(
460
462
  klass,
@@ -16,8 +16,8 @@ VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE
16
16
  VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location);
17
17
  VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment);
18
18
  VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment);
19
- VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment);
20
- VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE types, VALUE annotations, VALUE location, VALUE comment, VALUE overload);
19
+ VALUE rbs_ast_members_attribute(VALUE klass, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility);
20
+ VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE types, VALUE annotations, VALUE location, VALUE comment, VALUE overload, VALUE visibility);
21
21
  VALUE rbs_ast_members_mixin(VALUE klass, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment);
22
22
  VALUE rbs_ast_members_variable(VALUE klass, VALUE name, VALUE type, VALUE location, VALUE comment);
23
23
  VALUE rbs_ast_members_visibility(VALUE klass, VALUE location);
data/goodcheck.yml CHANGED
@@ -1,15 +1,4 @@
1
1
  rules:
2
- - id: rbs.no_mark
3
- pattern: 💪👽🚨
4
- message: Do you forget to delete `arglists` section?
5
- glob:
6
- - "{core,stdlib}/**/*.rbs"
7
- fail:
8
- - |
9
- # arglists 💪👽🚨 << Delete this section
10
- # File.absolute_path?(file_name) -> true or false
11
- #
12
-
13
2
  - id: rbs.no_arg
14
3
  pattern:
15
4
  regexp: arg\d+
@@ -11,7 +11,7 @@ module RBS
11
11
  @include_filename = true
12
12
  end
13
13
 
14
- def annotate_file(path)
14
+ def annotate_file(path, preserve:)
15
15
  content = path.read()
16
16
 
17
17
  decls = Parser.parse_signature(content)
@@ -19,7 +19,7 @@ module RBS
19
19
  annotate_decls(decls)
20
20
 
21
21
  path.open("w") do |io|
22
- Writer.new(out: io).write(decls)
22
+ Writer.new(out: io).preserve!(preserve: preserve).write(decls)
23
23
  end
24
24
  end
25
25
 
@@ -12,8 +12,9 @@ module RBS
12
12
  attr_reader :location
13
13
  attr_reader :comment
14
14
  attr_reader :overload
15
+ attr_reader :visibility
15
16
 
16
- def initialize(name:, kind:, types:, annotations:, location:, comment:, overload:)
17
+ def initialize(name:, kind:, types:, annotations:, location:, comment:, overload:, visibility: nil)
17
18
  @name = name
18
19
  @kind = kind
19
20
  @types = types
@@ -21,6 +22,7 @@ module RBS
21
22
  @location = location
22
23
  @comment = comment
23
24
  @overload = overload ? true : false
25
+ @visibility = visibility
24
26
  end
25
27
 
26
28
  def ==(other)
@@ -28,13 +30,14 @@ module RBS
28
30
  other.name == name &&
29
31
  other.kind == kind &&
30
32
  other.types == types &&
31
- other.overload == overload
33
+ other.overload == overload &&
34
+ other.visibility == visibility
32
35
  end
33
36
 
34
37
  alias eql? ==
35
38
 
36
39
  def hash
37
- self.class.hash ^ name.hash ^ kind.hash ^ types.hash ^ overload.hash
40
+ name.hash ^ kind.hash ^ types.hash ^ overload.hash
38
41
  end
39
42
 
40
43
  def instance?
@@ -49,7 +52,7 @@ module RBS
49
52
  overload
50
53
  end
51
54
 
52
- def update(name: self.name, kind: self.kind, types: self.types, annotations: self.annotations, location: self.location, comment: self.comment, overload: self.overload)
55
+ def update(name: self.name, kind: self.kind, types: self.types, annotations: self.annotations, location: self.location, comment: self.comment, overload: self.overload, visibility: self.visibility)
53
56
  self.class.new(
54
57
  name: name,
55
58
  kind: kind,
@@ -57,7 +60,8 @@ module RBS
57
60
  annotations: annotations,
58
61
  location: location,
59
62
  comment: comment,
60
- overload: overload
63
+ overload: overload,
64
+ visibility: visibility
61
65
  )
62
66
  end
63
67
 
@@ -94,7 +98,7 @@ module RBS
94
98
  alias eql? ==
95
99
 
96
100
  def hash
97
- self.class.hash ^ name.hash ^ type.hash
101
+ name.hash ^ type.hash
98
102
  end
99
103
  end
100
104
 
@@ -164,7 +168,7 @@ module RBS
164
168
  end
165
169
 
166
170
  def hash
167
- self.class.hash ^ name.hash ^ args.hash
171
+ name.hash ^ args.hash
168
172
  end
169
173
  end
170
174
 
@@ -221,8 +225,9 @@ module RBS
221
225
  attr_reader :annotations
222
226
  attr_reader :location
223
227
  attr_reader :comment
228
+ attr_reader :visibility
224
229
 
225
- def initialize(name:, type:, ivar_name:, kind:, annotations:, location:, comment:)
230
+ def initialize(name:, type:, ivar_name:, kind:, annotations:, location:, comment:, visibility: nil)
226
231
  @name = name
227
232
  @type = type
228
233
  @ivar_name = ivar_name
@@ -230,6 +235,7 @@ module RBS
230
235
  @location = location
231
236
  @comment = comment
232
237
  @kind = kind
238
+ @visibility = visibility
233
239
  end
234
240
 
235
241
  def ==(other)
@@ -237,16 +243,17 @@ module RBS
237
243
  other.name == name &&
238
244
  other.type == type &&
239
245
  other.ivar_name == ivar_name &&
240
- other.kind == kind
246
+ other.kind == kind &&
247
+ other.visibility == visibility
241
248
  end
242
249
 
243
250
  alias eql? ==
244
251
 
245
252
  def hash
246
- self.class.hash ^ name.hash ^ type.hash ^ ivar_name.hash ^ kind.hash
253
+ name.hash ^ type.hash ^ ivar_name.hash ^ kind.hash ^ visibility.hash
247
254
  end
248
255
 
249
- def update(name: self.name, type: self.type, ivar_name: self.ivar_name, kind: self.kind, annotations: self.annotations, location: self.location, comment: self.comment)
256
+ def update(name: self.name, type: self.type, ivar_name: self.ivar_name, kind: self.kind, annotations: self.annotations, location: self.location, comment: self.comment, visibility: self.visibility)
250
257
  klass = _ = self.class
251
258
  klass.new(
252
259
  name: name,
@@ -255,7 +262,8 @@ module RBS
255
262
  kind: kind,
256
263
  annotations: annotations,
257
264
  location: location,
258
- comment: comment
265
+ comment: comment,
266
+ visibility: visibility
259
267
  )
260
268
  end
261
269
  end
@@ -372,7 +380,7 @@ module RBS
372
380
  alias eql? ==
373
381
 
374
382
  def hash
375
- self.class.hash ^ new_name.hash ^ old_name.hash ^ kind.hash
383
+ new_name.hash ^ old_name.hash ^ kind.hash
376
384
  end
377
385
 
378
386
  def to_json(state = _ = nil)
data/lib/rbs/buffer.rb CHANGED
@@ -2,23 +2,29 @@ module RBS
2
2
  class Buffer
3
3
  attr_reader :name
4
4
  attr_reader :content
5
- attr_reader :lines
6
- attr_reader :ranges
7
5
 
8
6
  def initialize(name:, content:)
9
7
  @name = name
10
8
  @content = content
9
+ end
11
10
 
12
- @lines = content.lines
11
+ def lines
12
+ @lines ||= content.lines
13
+ end
13
14
 
14
- @ranges = []
15
- offset = 0
16
- lines.each do |line|
17
- size = line.size
18
- range = offset...(offset+size)
19
- ranges << range
20
- offset += size
21
- end
15
+ def ranges
16
+ @ranges ||=
17
+ begin
18
+ @ranges = []
19
+ offset = 0
20
+ lines.each do |line|
21
+ size = line.size
22
+ range = offset...(offset+size)
23
+ @ranges << range
24
+ offset += size
25
+ end
26
+ @ranges
27
+ end
22
28
  end
23
29
 
24
30
  def pos_to_loc(pos)
data/lib/rbs/cli.rb CHANGED
@@ -797,6 +797,8 @@ Examples:
797
797
  source = RBS::Annotate::RDocSource.new()
798
798
  annotator = RBS::Annotate::RDocAnnotator.new(source: source)
799
799
 
800
+ preserve = true
801
+
800
802
  OptionParser.new do |opts|
801
803
  opts.banner = <<-EOB
802
804
  Usage: rbs annotate [options...] [files...]
@@ -817,6 +819,7 @@ Options:
817
819
  opts.on("-d", "--dir DIRNAME", "Load RDoc from DIRNAME") {|d| source.extra_dirs << Pathname(d) }
818
820
  opts.on("--[no-]arglists", "Generate arglists section (defaults to true)") {|b| annotator.include_arg_lists = b }
819
821
  opts.on("--[no-]filename", "Include source file name in the documentation (defaults to true)") {|b| annotator.include_filename = b }
822
+ opts.on("--[no-]preserve", "Try preserve the format of the original file (defaults to true)") {|b| preserve = b }
820
823
  end.parse!(args)
821
824
 
822
825
  source.load()
@@ -826,11 +829,11 @@ Options:
826
829
  if path.directory?
827
830
  Pathname.glob((path + "**/*.rbs").to_s).each do |path|
828
831
  stdout.puts "Processing #{path}..."
829
- annotator.annotate_file(path)
832
+ annotator.annotate_file(path, preserve: preserve)
830
833
  end
831
834
  else
832
835
  stdout.puts "Processing #{path}..."
833
- annotator.annotate_file(path)
836
+ annotator.annotate_file(path, preserve: preserve)
834
837
  end
835
838
  end
836
839
  end