rbs 3.9.4 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.clang-format +75 -0
- data/.clangd +2 -0
- data/.dockerignore +37 -0
- data/.github/dependabot.yml +16 -14
- data/.github/workflows/bundle-update.yml +63 -0
- data/.github/workflows/c-check.yml +66 -0
- data/.github/workflows/comments.yml +5 -3
- data/.github/workflows/dependabot.yml +2 -2
- data/.github/workflows/jruby.yml +79 -0
- data/.github/workflows/release-gems.yml +164 -0
- data/.github/workflows/ruby.yml +84 -30
- data/.github/workflows/rust.yml +186 -0
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +5 -2
- data/.github/workflows/wasm.yml +55 -0
- data/.github/workflows/windows.yml +9 -3
- data/.gitignore +16 -0
- data/.rubocop.yml +2 -2
- data/CHANGELOG.md +430 -0
- data/Dockerfile.jruby +53 -0
- data/README.md +41 -4
- data/Rakefile +777 -60
- data/Steepfile +11 -0
- data/config.yml +660 -62
- data/core/array.rbs +541 -398
- data/core/basic_object.rbs +9 -8
- data/core/binding.rbs +0 -2
- data/core/builtin.rbs +9 -8
- data/core/class.rbs +11 -8
- data/core/comparable.rbs +55 -34
- data/core/complex.rbs +104 -78
- data/core/dir.rbs +61 -49
- data/core/encoding.rbs +12 -15
- data/core/enumerable.rbs +297 -196
- data/core/enumerator/arithmetic_sequence.rbs +70 -0
- data/core/enumerator/product.rbs +5 -5
- data/core/enumerator.rbs +91 -28
- data/core/errno.rbs +11 -2
- data/core/errors.rbs +58 -29
- data/core/exception.rbs +13 -13
- data/core/fiber.rbs +74 -54
- data/core/file.rbs +260 -1151
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/file_test.rbs +3 -3
- data/core/float.rbs +257 -116
- data/core/gc.rbs +425 -281
- data/core/hash.rbs +1151 -829
- data/core/integer.rbs +156 -195
- data/core/io/buffer.rbs +53 -42
- data/core/io/wait.rbs +13 -35
- data/core/io.rbs +220 -154
- data/core/kernel.rbs +239 -163
- data/core/marshal.rbs +4 -4
- data/core/match_data.rbs +16 -14
- data/core/math.rbs +107 -66
- data/core/method.rbs +69 -33
- data/core/module.rbs +302 -150
- data/core/nil_class.rbs +7 -6
- data/core/numeric.rbs +77 -63
- data/core/object.rbs +9 -11
- data/core/object_space/weak_key_map.rbs +7 -7
- data/core/object_space.rbs +30 -23
- data/core/pathname.rbs +1312 -0
- data/core/proc.rbs +95 -58
- data/core/process.rbs +222 -202
- data/core/ractor.rbs +364 -518
- data/core/random.rbs +21 -3
- data/core/range.rbs +181 -79
- data/core/rational.rbs +60 -89
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +63 -56
- data/core/rbs/unnamed/env_class.rbs +19 -14
- data/core/rbs/unnamed/main_class.rbs +123 -0
- data/core/rbs/unnamed/random.rbs +11 -118
- data/core/regexp.rbs +258 -214
- data/core/ruby.rbs +53 -0
- data/core/ruby_vm.rbs +78 -34
- data/core/rubygems/config_file.rbs +5 -5
- data/core/rubygems/errors.rbs +6 -70
- data/core/rubygems/requirement.rbs +5 -15
- data/core/rubygems/rubygems.rbs +18 -81
- data/core/rubygems/specification.rbs +8 -0
- data/core/rubygems/version.rbs +2 -163
- data/core/set.rbs +493 -363
- data/core/signal.rbs +26 -16
- data/core/string.rbs +3234 -1285
- data/core/struct.rbs +43 -42
- data/core/symbol.rbs +41 -34
- data/core/thread.rbs +139 -83
- data/core/time.rbs +81 -50
- data/core/trace_point.rbs +41 -35
- data/core/true_class.rbs +2 -2
- data/core/unbound_method.rbs +24 -16
- data/core/warning.rbs +7 -7
- data/docs/CONTRIBUTING.md +2 -1
- data/docs/aliases.md +79 -0
- data/docs/collection.md +3 -3
- data/docs/config.md +171 -0
- data/docs/encoding.md +56 -0
- data/docs/gem.md +0 -1
- data/docs/inline.md +634 -0
- data/docs/rbs_by_example.md +20 -20
- data/docs/release.md +151 -0
- data/docs/rust.md +96 -0
- data/docs/sigs.md +3 -3
- data/docs/syntax.md +48 -18
- data/docs/type_fingerprint.md +21 -0
- data/docs/wasm_serialization.md +80 -0
- data/exe/rbs +1 -1
- data/ext/rbs_extension/ast_translation.c +1855 -0
- data/ext/rbs_extension/ast_translation.h +41 -0
- data/ext/rbs_extension/class_constants.c +187 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +23 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +26 -1
- data/ext/rbs_extension/legacy_location.c +299 -0
- data/ext/rbs_extension/legacy_location.h +82 -0
- data/ext/rbs_extension/main.c +603 -23
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +1047 -0
- data/include/rbs/defines.h +99 -0
- data/include/rbs/lexer.h +207 -0
- data/include/rbs/location.h +40 -0
- data/include/rbs/parser.h +153 -0
- data/include/rbs/serialize.h +39 -0
- data/include/rbs/string.h +47 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -70
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +24 -0
- data/include/rbs.h +9 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +30 -32
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/ast/annotation.rb +1 -1
- data/lib/rbs/ast/comment.rb +1 -1
- data/lib/rbs/ast/declarations.rb +10 -10
- data/lib/rbs/ast/members.rb +14 -14
- data/lib/rbs/ast/ruby/annotations.rb +451 -0
- data/lib/rbs/ast/ruby/comment_block.rb +247 -0
- data/lib/rbs/ast/ruby/declarations.rb +291 -0
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
- data/lib/rbs/ast/ruby/members.rb +762 -0
- data/lib/rbs/ast/type_param.rb +24 -4
- data/lib/rbs/buffer.rb +142 -20
- data/lib/rbs/cli/diff.rb +16 -15
- data/lib/rbs/cli/validate.rb +63 -126
- data/lib/rbs/cli.rb +59 -29
- data/lib/rbs/collection/config/lockfile_generator.rb +28 -3
- data/lib/rbs/collection/sources/git.rb +7 -0
- data/lib/rbs/definition.rb +6 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +121 -65
- data/lib/rbs/definition_builder/method_builder.rb +74 -33
- data/lib/rbs/definition_builder.rb +177 -20
- data/lib/rbs/diff.rb +7 -1
- data/lib/rbs/environment/class_entry.rb +69 -0
- data/lib/rbs/environment/module_entry.rb +66 -0
- data/lib/rbs/environment.rb +410 -215
- data/lib/rbs/environment_loader.rb +2 -8
- data/lib/rbs/errors.rb +31 -21
- data/lib/rbs/inline_parser/comment_association.rb +117 -0
- data/lib/rbs/inline_parser.rb +568 -0
- data/lib/rbs/location_aux.rb +36 -4
- data/lib/rbs/locator.rb +5 -1
- data/lib/rbs/method_type.rb +5 -3
- data/lib/rbs/namespace.rb +47 -18
- data/lib/rbs/parser_aux.rb +37 -7
- data/lib/rbs/prototype/helpers.rb +57 -0
- data/lib/rbs/prototype/rb.rb +3 -28
- data/lib/rbs/prototype/rbi.rb +196 -45
- data/lib/rbs/prototype/runtime.rb +12 -2
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +120 -44
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/source.rb +99 -0
- data/lib/rbs/subtractor.rb +7 -4
- data/lib/rbs/test/type_check.rb +25 -3
- data/lib/rbs/type_name.rb +34 -21
- data/lib/rbs/types.rb +91 -79
- data/lib/rbs/unit_test/convertibles.rb +1 -0
- data/lib/rbs/unit_test/type_assertions.rb +44 -8
- data/lib/rbs/validator.rb +2 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/runtime.rb +196 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs.rb +25 -2
- data/lib/rbs_jars.rb +39 -0
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +8 -3
- data/rbs.gemspec +37 -5
- data/schema/typeParam.json +17 -1
- data/sig/ancestor_builder.rbs +1 -1
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +13 -10
- data/sig/ast/ruby/annotations.rbs +470 -0
- data/sig/ast/ruby/comment_block.rbs +127 -0
- data/sig/ast/ruby/declarations.rbs +158 -0
- data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
- data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
- data/sig/ast/ruby/members.rbs +198 -0
- data/sig/buffer.rbs +81 -5
- data/sig/cli/diff.rbs +5 -11
- data/sig/cli/validate.rbs +12 -8
- data/sig/cli.rbs +18 -18
- data/sig/collection/config/lockfile_generator.rbs +2 -0
- data/sig/definition.rbs +6 -0
- data/sig/definition_builder.rbs +3 -1
- data/sig/environment/class_entry.rbs +50 -0
- data/sig/environment/module_entry.rbs +50 -0
- data/sig/environment.rbs +94 -87
- data/sig/errors.rbs +26 -20
- data/sig/inline_parser/comment_association.rbs +71 -0
- data/sig/inline_parser.rbs +126 -0
- data/sig/location.rbs +32 -7
- data/sig/locator.rbs +0 -2
- data/sig/manifest.yaml +0 -2
- data/sig/method_builder.rbs +9 -4
- data/sig/namespace.rbs +20 -5
- data/sig/parser.rbs +77 -13
- data/sig/prototype/helpers.rbs +2 -0
- data/sig/prototype/rbi.rbs +33 -4
- data/sig/resolver/type_name_resolver.rbs +36 -10
- data/sig/rewriter.rbs +45 -0
- data/sig/source.rbs +48 -0
- data/sig/type_param.rbs +13 -8
- data/sig/typename.rbs +15 -5
- data/sig/types.rbs +10 -8
- data/sig/unit_test/spy.rbs +0 -8
- data/sig/unit_test/type_assertions.rbs +15 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +1628 -0
- data/src/lexer.c +3221 -0
- data/src/lexer.re +155 -0
- data/src/lexstate.c +221 -0
- data/src/location.c +31 -0
- data/src/parser.c +4258 -0
- data/src/serialize.c +958 -0
- data/src/string.c +41 -0
- data/src/util/rbs_allocator.c +171 -0
- data/src/util/rbs_assert.c +19 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +18 -92
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +167 -0
- data/stdlib/abbrev/0/array.rbs +1 -1
- data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
- data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
- data/stdlib/cgi/0/core.rbs +9 -393
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +171 -0
- data/stdlib/coverage/0/coverage.rbs +7 -4
- data/stdlib/csv/0/csv.rbs +5 -5
- data/stdlib/date/0/date.rbs +92 -79
- data/stdlib/date/0/date_time.rbs +25 -24
- data/stdlib/delegate/0/delegator.rbs +11 -7
- data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
- data/stdlib/digest/0/digest.rbs +117 -1
- data/stdlib/erb/0/erb.rbs +748 -347
- data/stdlib/etc/0/etc.rbs +73 -54
- data/stdlib/fileutils/0/fileutils.rbs +179 -160
- data/stdlib/forwardable/0/forwardable.rbs +13 -10
- data/stdlib/io-console/0/io-console.rbs +2 -2
- data/stdlib/ipaddr/0/ipaddr.rbs +0 -5
- data/stdlib/json/0/json.rbs +232 -185
- data/stdlib/monitor/0/monitor.rbs +5 -5
- data/stdlib/net-http/0/net-http.rbs +162 -134
- data/stdlib/objspace/0/objspace.rbs +17 -34
- data/stdlib/open-uri/0/open-uri.rbs +48 -8
- data/stdlib/open3/0/open3.rbs +469 -10
- data/stdlib/openssl/0/openssl.rbs +521 -397
- data/stdlib/optparse/0/optparse.rbs +26 -17
- data/stdlib/pathname/0/pathname.rbs +11 -1381
- data/stdlib/pp/0/pp.rbs +9 -8
- data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
- data/stdlib/pstore/0/pstore.rbs +35 -30
- data/stdlib/psych/0/psych.rbs +65 -12
- data/stdlib/psych/0/store.rbs +2 -4
- data/stdlib/pty/0/pty.rbs +9 -6
- data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
- data/stdlib/rdoc/0/code_object.rbs +4 -3
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/parser.rbs +1 -1
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +2 -2
- data/stdlib/resolv/0/resolv.rbs +26 -69
- data/stdlib/ripper/0/ripper.rbs +25 -19
- data/stdlib/securerandom/0/manifest.yaml +2 -0
- data/stdlib/securerandom/0/securerandom.rbs +7 -20
- data/stdlib/shellwords/0/shellwords.rbs +3 -3
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/addrinfo.rbs +9 -9
- data/stdlib/socket/0/basic_socket.rbs +3 -3
- data/stdlib/socket/0/ip_socket.rbs +10 -8
- data/stdlib/socket/0/socket.rbs +23 -10
- data/stdlib/socket/0/tcp_server.rbs +1 -1
- data/stdlib/socket/0/tcp_socket.rbs +11 -3
- data/stdlib/socket/0/udp_socket.rbs +1 -1
- data/stdlib/socket/0/unix_server.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +1209 -95
- data/stdlib/strscan/0/string_scanner.rbs +101 -80
- data/stdlib/tempfile/0/manifest.yaml +3 -0
- data/stdlib/tempfile/0/tempfile.rbs +25 -21
- data/stdlib/time/0/time.rbs +8 -6
- data/stdlib/timeout/0/timeout.rbs +58 -7
- data/stdlib/tsort/0/cyclic.rbs +4 -1
- data/stdlib/tsort/0/interfaces.rbs +8 -8
- data/stdlib/tsort/0/tsort.rbs +16 -15
- data/stdlib/uri/0/common.rbs +42 -20
- data/stdlib/uri/0/file.rbs +3 -3
- data/stdlib/uri/0/generic.rbs +21 -18
- data/stdlib/uri/0/http.rbs +2 -2
- data/stdlib/uri/0/ldap.rbs +2 -2
- data/stdlib/uri/0/mailto.rbs +3 -3
- data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
- data/stdlib/zlib/0/deflate.rbs +4 -3
- data/stdlib/zlib/0/gzip_reader.rbs +8 -8
- data/stdlib/zlib/0/gzip_writer.rbs +14 -12
- data/stdlib/zlib/0/inflate.rbs +1 -1
- data/stdlib/zlib/0/need_dict.rbs +1 -1
- data/wasm/README.md +60 -0
- data/wasm/rbs_wasm.c +423 -0
- metadata +131 -19
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
data/lib/rbs/ast/type_param.rb
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
module RBS
|
|
4
4
|
module AST
|
|
5
5
|
class TypeParam
|
|
6
|
-
attr_reader :name, :variance, :location, :upper_bound_type, :default_type
|
|
6
|
+
attr_reader :name, :variance, :location, :upper_bound_type, :lower_bound_type, :default_type
|
|
7
7
|
|
|
8
|
-
def initialize(name:, variance:, upper_bound:, location:, default_type: nil, unchecked: false)
|
|
8
|
+
def initialize(name:, variance:, upper_bound:, lower_bound:, location:, default_type: nil, unchecked: false)
|
|
9
9
|
@name = name
|
|
10
10
|
@variance = variance
|
|
11
11
|
@upper_bound_type = upper_bound
|
|
12
|
+
@lower_bound_type = lower_bound
|
|
12
13
|
@location = location
|
|
13
14
|
@default_type = default_type
|
|
14
15
|
@unchecked = unchecked
|
|
@@ -21,6 +22,13 @@ module RBS
|
|
|
21
22
|
end
|
|
22
23
|
end
|
|
23
24
|
|
|
25
|
+
def lower_bound
|
|
26
|
+
case lower_bound_type
|
|
27
|
+
when Types::ClassInstance, Types::ClassSingleton, Types::Interface
|
|
28
|
+
lower_bound_type
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
24
32
|
def unchecked!(value = true)
|
|
25
33
|
@unchecked = value ? true : false
|
|
26
34
|
self
|
|
@@ -35,6 +43,7 @@ module RBS
|
|
|
35
43
|
other.name == name &&
|
|
36
44
|
other.variance == variance &&
|
|
37
45
|
other.upper_bound_type == upper_bound_type &&
|
|
46
|
+
other.lower_bound_type == lower_bound_type &&
|
|
38
47
|
other.default_type == default_type &&
|
|
39
48
|
other.unchecked? == unchecked?
|
|
40
49
|
end
|
|
@@ -42,7 +51,7 @@ module RBS
|
|
|
42
51
|
alias eql? ==
|
|
43
52
|
|
|
44
53
|
def hash
|
|
45
|
-
self.class.hash ^ name.hash ^ variance.hash ^ upper_bound_type.hash ^ unchecked?.hash ^ default_type.hash
|
|
54
|
+
self.class.hash ^ name.hash ^ variance.hash ^ upper_bound_type.hash ^ lower_bound_type.hash ^ unchecked?.hash ^ default_type.hash
|
|
46
55
|
end
|
|
47
56
|
|
|
48
57
|
def to_json(state = JSON::State.new)
|
|
@@ -52,6 +61,7 @@ module RBS
|
|
|
52
61
|
unchecked: unchecked?,
|
|
53
62
|
location: location,
|
|
54
63
|
upper_bound: upper_bound_type,
|
|
64
|
+
lower_bound: lower_bound_type,
|
|
55
65
|
default_type: default_type
|
|
56
66
|
}.to_json(state)
|
|
57
67
|
end
|
|
@@ -61,6 +71,10 @@ module RBS
|
|
|
61
71
|
_upper_bound_type = yield(b)
|
|
62
72
|
end
|
|
63
73
|
|
|
74
|
+
if b = lower_bound_type
|
|
75
|
+
_lower_bound_type = yield(b)
|
|
76
|
+
end
|
|
77
|
+
|
|
64
78
|
if dt = default_type
|
|
65
79
|
_default_type = yield(dt)
|
|
66
80
|
end
|
|
@@ -69,6 +83,7 @@ module RBS
|
|
|
69
83
|
name: name,
|
|
70
84
|
variance: variance,
|
|
71
85
|
upper_bound: _upper_bound_type,
|
|
86
|
+
lower_bound: _lower_bound_type,
|
|
72
87
|
location: location,
|
|
73
88
|
default_type: _default_type
|
|
74
89
|
).unchecked!(unchecked?)
|
|
@@ -108,6 +123,7 @@ module RBS
|
|
|
108
123
|
name: new_name,
|
|
109
124
|
variance: param.variance,
|
|
110
125
|
upper_bound: param.upper_bound_type&.map_type {|type| type.sub(subst) },
|
|
126
|
+
lower_bound: param.lower_bound_type&.map_type {|type| type.sub(subst) },
|
|
111
127
|
location: param.location,
|
|
112
128
|
default_type: param.default_type&.map_type {|type| type.sub(subst) }
|
|
113
129
|
).unchecked!(param.unchecked?)
|
|
@@ -136,6 +152,10 @@ module RBS
|
|
|
136
152
|
s << " < #{type}"
|
|
137
153
|
end
|
|
138
154
|
|
|
155
|
+
if type = lower_bound_type
|
|
156
|
+
s << " > #{type}"
|
|
157
|
+
end
|
|
158
|
+
|
|
139
159
|
if dt = default_type
|
|
140
160
|
s << " = #{dt}"
|
|
141
161
|
end
|
|
@@ -176,7 +196,7 @@ module RBS
|
|
|
176
196
|
end
|
|
177
197
|
|
|
178
198
|
def self.normalize_args(params, args)
|
|
179
|
-
app = application(params, args) or return
|
|
199
|
+
app = application(params, args) or return args
|
|
180
200
|
|
|
181
201
|
min_count = params.count { _1.default_type.nil? }
|
|
182
202
|
unless min_count <= args.size && args.size <= params.size
|
data/lib/rbs/buffer.rb
CHANGED
|
@@ -4,39 +4,58 @@ module RBS
|
|
|
4
4
|
class Buffer
|
|
5
5
|
attr_reader :name
|
|
6
6
|
attr_reader :content
|
|
7
|
+
attr_reader :parent
|
|
7
8
|
|
|
8
|
-
def initialize(name:,
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
def initialize(name: nil, content:, parent: nil)
|
|
10
|
+
case
|
|
11
|
+
when name && content
|
|
12
|
+
@name = name
|
|
13
|
+
@content = content
|
|
14
|
+
@parent = nil
|
|
15
|
+
when parent && content
|
|
16
|
+
@name = parent[0].name
|
|
17
|
+
@content = content
|
|
18
|
+
@parent = parent
|
|
19
|
+
end
|
|
11
20
|
end
|
|
12
21
|
|
|
13
22
|
def lines
|
|
14
|
-
|
|
23
|
+
ranges.map { self.content[_1] || raise } #$ String
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def line_count
|
|
27
|
+
ranges.size
|
|
15
28
|
end
|
|
16
29
|
|
|
17
30
|
def ranges
|
|
18
|
-
@ranges ||=
|
|
19
|
-
|
|
20
|
-
|
|
31
|
+
@ranges ||= begin
|
|
32
|
+
if content.empty?
|
|
33
|
+
ranges = [0...0] #: Array[Range[Integer]]
|
|
34
|
+
lines = [""]
|
|
35
|
+
else
|
|
36
|
+
lines = content.lines
|
|
37
|
+
lines << "" if content.end_with?("\n")
|
|
38
|
+
|
|
39
|
+
ranges = [] #: Array[Range[Integer]]
|
|
21
40
|
offset = 0
|
|
41
|
+
|
|
22
42
|
lines.each do |line|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
end
|
|
43
|
+
size0 = line.size
|
|
44
|
+
line = line.chomp
|
|
45
|
+
range = offset...(offset+line.size)
|
|
46
|
+
ranges << range
|
|
28
47
|
|
|
29
|
-
|
|
30
|
-
@ranges[-1] = @ranges[-1].begin...(@ranges[-1].end+1)
|
|
48
|
+
offset += size0
|
|
31
49
|
end
|
|
32
|
-
|
|
33
|
-
@ranges
|
|
34
50
|
end
|
|
51
|
+
|
|
52
|
+
ranges
|
|
53
|
+
end
|
|
35
54
|
end
|
|
36
55
|
|
|
37
56
|
def pos_to_loc(pos)
|
|
38
57
|
index = ranges.bsearch_index do |range|
|
|
39
|
-
pos
|
|
58
|
+
pos <= range.end ? true : false
|
|
40
59
|
end
|
|
41
60
|
|
|
42
61
|
if index
|
|
@@ -49,7 +68,7 @@ module RBS
|
|
|
49
68
|
def loc_to_pos(loc)
|
|
50
69
|
line, column = loc
|
|
51
70
|
|
|
52
|
-
if range = ranges
|
|
71
|
+
if range = ranges.fetch(line - 1, nil)
|
|
53
72
|
range.begin + column
|
|
54
73
|
else
|
|
55
74
|
last_position
|
|
@@ -57,11 +76,114 @@ module RBS
|
|
|
57
76
|
end
|
|
58
77
|
|
|
59
78
|
def last_position
|
|
60
|
-
|
|
79
|
+
if ranges.empty?
|
|
80
|
+
0
|
|
81
|
+
else
|
|
82
|
+
ranges[-1].end
|
|
83
|
+
end
|
|
61
84
|
end
|
|
62
85
|
|
|
63
86
|
def inspect
|
|
64
|
-
"#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{
|
|
87
|
+
"#<RBS::Buffer:#{__id__} @name=#{name}, @content=#{content.bytesize} bytes, @lines=#{ranges.size} lines,>"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def character_offset(byte_offset)
|
|
91
|
+
top = top_buffer
|
|
92
|
+
return top.character_offset(byte_offset) unless top.equal?(self)
|
|
93
|
+
|
|
94
|
+
keys, vals = (@character_offset_cache ||= [[0], [0]])
|
|
95
|
+
|
|
96
|
+
idx = keys.bsearch_index { |k| k > byte_offset }
|
|
97
|
+
lo = idx ? idx - 1 : keys.size - 1
|
|
98
|
+
|
|
99
|
+
base_byte = keys[lo]
|
|
100
|
+
base_char = vals[lo]
|
|
101
|
+
delta = byte_offset - base_byte
|
|
102
|
+
return base_char if delta == 0
|
|
103
|
+
|
|
104
|
+
result = base_char + (content.byteslice(base_byte, delta) or raise).length
|
|
105
|
+
|
|
106
|
+
if base_byte == keys[-1]
|
|
107
|
+
keys << byte_offset
|
|
108
|
+
vals << result
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
result
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def rbs_location(location, loc2=nil)
|
|
115
|
+
top = top_buffer
|
|
116
|
+
if loc2
|
|
117
|
+
Location.new(top, character_offset(location.start_offset), character_offset(loc2.end_offset))
|
|
118
|
+
else
|
|
119
|
+
Location.new(top, character_offset(location.start_offset), character_offset(location.end_offset))
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def sub_buffer(lines:, byte_lines_hint: nil)
|
|
124
|
+
buf = +""
|
|
125
|
+
|
|
126
|
+
if byte_lines_hint
|
|
127
|
+
byte_lines_hint.each_with_index do |range, index|
|
|
128
|
+
slice = content.byteslice(range.begin, range.end - range.begin) or raise
|
|
129
|
+
if slice.include?("\n")
|
|
130
|
+
raise "Line #{index + 1} cannot contain newline character."
|
|
131
|
+
end
|
|
132
|
+
buf << slice
|
|
133
|
+
buf << "\n"
|
|
134
|
+
end
|
|
135
|
+
else
|
|
136
|
+
lines.each_with_index do |range, index|
|
|
137
|
+
start_pos = range.begin
|
|
138
|
+
end_pos = range.end
|
|
139
|
+
slice = content[start_pos...end_pos] or raise
|
|
140
|
+
if slice.include?("\n")
|
|
141
|
+
raise "Line #{index + 1} cannot contain newline character."
|
|
142
|
+
end
|
|
143
|
+
buf << slice
|
|
144
|
+
buf << "\n"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
buf.chomp!
|
|
149
|
+
|
|
150
|
+
Buffer.new(content: buf, parent: [self, lines])
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def parent_buffer
|
|
154
|
+
if parent
|
|
155
|
+
parent[0]
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def parent_position(position)
|
|
160
|
+
parent or raise "#parent_position is unavailable with buffer without parent"
|
|
161
|
+
return nil unless position <= last_position
|
|
162
|
+
|
|
163
|
+
line, column = pos_to_loc(position)
|
|
164
|
+
parent_range = parent[1][line - 1]
|
|
165
|
+
parent_range.begin + column
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def absolute_position(position)
|
|
169
|
+
if parent_buffer
|
|
170
|
+
pos = parent_position(position) or return
|
|
171
|
+
parent_buffer.absolute_position(pos)
|
|
172
|
+
else
|
|
173
|
+
position
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def top_buffer
|
|
178
|
+
if parent_buffer
|
|
179
|
+
parent_buffer.top_buffer
|
|
180
|
+
else
|
|
181
|
+
self
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def detach
|
|
186
|
+
Buffer.new(name: name, content: content)
|
|
65
187
|
end
|
|
66
188
|
end
|
|
67
189
|
end
|
data/lib/rbs/cli/diff.rb
CHANGED
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
module RBS
|
|
4
4
|
class CLI
|
|
5
5
|
class Diff
|
|
6
|
-
def initialize(
|
|
7
|
-
@format = nil
|
|
6
|
+
def initialize(stdout: $stdout, stderr: $stderr)
|
|
8
7
|
@stdout = stdout
|
|
9
8
|
@stderr = stderr
|
|
9
|
+
end
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
def run(argv:, library_options:)
|
|
12
|
+
format = nil #: String?
|
|
13
|
+
type_name = nil #: String?
|
|
13
14
|
library_options = library_options
|
|
14
15
|
before_path = [] #: Array[String]
|
|
15
16
|
after_path = [] #: Array[String]
|
|
@@ -32,7 +33,7 @@ module RBS
|
|
|
32
33
|
# Confirmation of methods related to Time class added by including stdlib/time
|
|
33
34
|
$ rbs diff --format diff --type-name Time --after stdlib/time
|
|
34
35
|
HELP
|
|
35
|
-
o.on("--format NAME") { |arg|
|
|
36
|
+
o.on("--format NAME") { |arg| format = arg }
|
|
36
37
|
o.on("--type-name NAME") { |arg| type_name = arg }
|
|
37
38
|
o.on("--before DIR") { |arg| before_path << arg }
|
|
38
39
|
o.on("--after DIR") { |arg| after_path << arg }
|
|
@@ -40,28 +41,28 @@ module RBS
|
|
|
40
41
|
end
|
|
41
42
|
opt.parse!(argv)
|
|
42
43
|
|
|
43
|
-
unless
|
|
44
|
+
unless format && type_name && ["markdown", "diff"].include?(format)
|
|
44
45
|
@stderr.puts opt.banner
|
|
45
|
-
|
|
46
|
+
return 1
|
|
46
47
|
end
|
|
47
48
|
|
|
48
|
-
|
|
49
|
+
diff = RBS::Diff.new(
|
|
49
50
|
type_name: TypeName.parse(type_name).absolute!,
|
|
50
51
|
library_options: library_options,
|
|
51
52
|
after_path: after_path,
|
|
52
53
|
before_path: before_path,
|
|
53
54
|
detail: detail,
|
|
54
55
|
)
|
|
55
|
-
end
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
public_send("run_#{format}", diff)
|
|
58
|
+
|
|
59
|
+
0
|
|
59
60
|
end
|
|
60
61
|
|
|
61
|
-
def run_diff
|
|
62
|
+
def run_diff(diff)
|
|
62
63
|
first = true
|
|
63
64
|
io = RBS::CLI::ColoredIO.new(stdout: @stdout)
|
|
64
|
-
|
|
65
|
+
diff.each_diff do |before, after|
|
|
65
66
|
io.puts if !first
|
|
66
67
|
io.puts_red "- #{before}"
|
|
67
68
|
io.puts_green "+ #{after}"
|
|
@@ -69,10 +70,10 @@ module RBS
|
|
|
69
70
|
end
|
|
70
71
|
end
|
|
71
72
|
|
|
72
|
-
def run_markdown
|
|
73
|
+
def run_markdown(diff)
|
|
73
74
|
@stdout.puts "| before | after |"
|
|
74
75
|
@stdout.puts "| --- | --- |"
|
|
75
|
-
|
|
76
|
+
diff.each_diff do |before, after|
|
|
76
77
|
before.gsub!("|", "\\|")
|
|
77
78
|
after.gsub!("|", "\\|")
|
|
78
79
|
@stdout.puts "| `#{before}` | `#{after}` |"
|