katakata_irb 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df817c7e7806e6ea21aa82e14a694130b0db5dd68c74ea84790b90ad3b895fb7
4
- data.tar.gz: 9faa1d72b27f35bccc5b4d2dcc5c9f50b4dd9946ba5d63374d3083e933e62c38
3
+ metadata.gz: e03e0f334a22895540f29f3bb27551bff9bfcbede5aac767559006edbc77118f
4
+ data.tar.gz: 4db442409ca2ac1d853b4e7675ff3ac10aed0d6ced431d1abdbe2eea7a1fa298
5
5
  SHA512:
6
- metadata.gz: 9bec77d0fd1f9484278541e48a964c683ef2546b8a5b107e731d7979b634c4c8fb9ce2b68261603fc5f4ad8eb83cfb3018e11881a2ecd4096d268e7be0e97405
7
- data.tar.gz: b6520a88ad8644423f24e7c02e783bf1cb388b3a00d2c16151b97e08598f1fd80f55f8346562d00c9d2ec0e2b619d4f4db23eeba5834c39b9ebfc72ce765cbdb
6
+ metadata.gz: fb7d8eae8b68d497c6e0c5c0d9bc8d8455d5af5638fb680b3048166188a1035a2e36229a7555203de4f8d18bc3089b92f441f496d6e2a074ad61587805b6de82
7
+ data.tar.gz: 75ef2f2f26505c98e3550342493474f47f11623fd1342f0529249e6e902e95f5ebf03f5053974c6813beee3697c0d13cb5a0adec073c65ece35dce9a3e3f01ed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- katakata_irb (0.1.6)
4
+ katakata_irb (0.1.8)
5
5
  irb (>= 1.4.0)
6
6
  rbs
7
7
 
@@ -9,12 +9,12 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  io-console (0.6.0)
12
- irb (1.6.2)
12
+ irb (1.6.4)
13
13
  reline (>= 0.3.0)
14
- minitest (5.16.3)
14
+ minitest (5.18.0)
15
15
  rake (13.0.6)
16
- rbs (2.7.0)
17
- reline (0.3.2)
16
+ rbs (3.1.0)
17
+ reline (0.3.3)
18
18
  io-console (~> 0.5)
19
19
 
20
20
  PLATFORMS
data/README.md CHANGED
@@ -10,8 +10,10 @@ gem install katakata_irb
10
10
  ## Usage
11
11
 
12
12
  Just require katakata_irb or write it to your `.irbrc` file.
13
+
13
14
  ```ruby
14
- require 'katakata_irb'
15
+ # .irbrc
16
+ require 'katakata_irb' rescue nil
15
17
  ```
16
18
 
17
19
  ```
@@ -120,10 +120,9 @@ module KatakataIrb::Completor
120
120
  contents = types.filter_map do |type|
121
121
  case type
122
122
  when KatakataIrb::Types::InstanceType
123
- KatakataIrb::Types.class_name_of type.klass
124
- when KatakataIrb::Types::SingletonType
125
- module_name = KatakataIrb::Types.class_name_of type.module_or_class
126
- "#{module_name}.itself" if module_name
123
+ type.inspect_without_params
124
+ else
125
+ type.inspect
127
126
  end
128
127
  end.uniq
129
128
  return if contents.empty?
@@ -247,7 +246,7 @@ module KatakataIrb::Completor
247
246
  # `require 'target'`
248
247
  return [require_method.to_sym, name.rstrip]
249
248
  end
250
- if (target in [:@ident,]) && (expression in [:symbol,]) && (parents[-2] in [:args_add_block, Array => args, [:symbol_literal, ^expression]])
249
+ if (target in [:@ident,]) && (expression in [:symbol,]) && (parents[-2] in [:args_add_block, Array => _args, [:symbol_literal, ^expression]])
251
250
  # `method(&:target)`
252
251
  receiver_ref = [:var_ref, [:@ident, '_1', [0, 0]]]
253
252
  block_statements = [receiver_ref]
@@ -14,6 +14,7 @@ module KatakataIrb
14
14
  @binding, @self_object = binding, self_object
15
15
  @cache = { SELF => KatakataIrb::Types.type_from_object(self_object) }
16
16
  @local_variables = binding.local_variables.map(&:to_s).to_set
17
+ @global_variables = global_variables.map(&:to_s).to_set
17
18
  end
18
19
 
19
20
  def level() = 0
@@ -36,6 +37,8 @@ module KatakataIrb
36
37
  BaseScope.type_of(fallback: fallback) { @binding.local_variable_get(name) }
37
38
  when :const
38
39
  BaseScope.type_of(fallback: fallback) { @binding.eval name }
40
+ when :gvar
41
+ BaseScope.type_of(fallback: fallback) { @binding.eval name if @global_variables.include? name }
39
42
  end
40
43
  )
41
44
  end
@@ -77,7 +80,12 @@ module KatakataIrb
77
80
  when :lvar
78
81
  @local_variables.include? name
79
82
  when :const
80
- @binding.eval("#{name};true") rescue false
83
+ begin
84
+ @binding.eval(name)
85
+ true
86
+ rescue
87
+ false
88
+ end
81
89
  when :gvar, :cvar, :ivar
82
90
  true
83
91
  when :internal
@@ -707,13 +707,12 @@ class KatakataIrb::TypeSimulator
707
707
 
708
708
  def evaluate_massign(sexp, values, scope)
709
709
  values = sized_splat values, :to_ary, sexp.size unless values.is_a? Array
710
-
711
710
  rest_index = sexp.find_index { _1 in [:rest_param, ]}
712
711
  if rest_index
713
712
  pre = rest_index ? sexp[0...rest_index] : sexp
714
713
  post = rest_index ? sexp[rest_index + 1..] : []
715
714
  sexp[rest_index] in [:rest_param, rest_field]
716
- rest_values = values[pre.size...-post.size] || []
715
+ rest_values = values[pre.size...values.size - post.size] || []
717
716
  rest_type = KatakataIrb::Types::InstanceType.new Array, Elem: KatakataIrb::Types::UnionType[*rest_values]
718
717
  pairs = pre.zip(values.first(pre.size)) + [[rest_field, rest_type]] + post.zip(values.last(post.size))
719
718
  else
@@ -12,7 +12,7 @@ module KatakataIrb::Types
12
12
  loader.add path: Pathname('sig')
13
13
  RBS::DefinitionBuilder.new env: RBS::Environment.from_loader(loader).resolve_type_names
14
14
  rescue => e
15
- puts "\nKatakataIRB failed to initialize RBS::DefinitionBuild\n#{e}"
15
+ puts "\r\nKatakataIRB failed to initialize RBS::DefinitionBuilder\r\n#{e}\r\n"
16
16
  Object.new
17
17
  end
18
18
 
@@ -95,15 +95,11 @@ module KatakataIrb::Types
95
95
  given << UnionType[*centers.drop(opts.size)]
96
96
  expected << rest.type
97
97
  end
98
- score += given.zip(expected).sum do |t, e|
99
- e = from_rbs_type e, receiver_type
100
- if intersect? t, e
101
- 1
102
- elsif (intersect?(STRING, e) && t.methods.include?(:to_str)) || (intersect?(INTEGER, e) && t.methods.include?(:to_int)) || (intersect?(ARRAY, e) && t.methods.include?(:to_ary))
103
- 0.5
104
- else
105
- 0
106
- end
98
+ if given.any?
99
+ score += given.zip(expected).count do |t, e|
100
+ e = from_rbs_type e, receiver_type
101
+ intersect?(t, e) || (intersect?(STRING, e) && t.methods.include?(:to_str)) || (intersect?(INTEGER, e) && t.methods.include?(:to_int)) || (intersect?(ARRAY, e) && t.methods.include?(:to_ary))
102
+ end.fdiv(given.size)
107
103
  end
108
104
  end
109
105
  [[method_type, given || [], expected || []], score]
@@ -114,15 +110,16 @@ module KatakataIrb::Types
114
110
  end
115
111
 
116
112
  def self.intersect?(a, b)
117
- atypes = ((a in UnionType) ? a.types : [a]).group_by(&:class)
118
- btypes = ((b in UnionType) ? b.types : [b]).group_by(&:class)
119
- intersect = ->(type, &block) do
120
- aa, bb = [atypes, btypes].map {|types| (types[type] || []).map(&block) }
121
- (aa & bb).any?
122
- end
113
+ atypes = a.types.group_by(&:class)
114
+ btypes = b.types.group_by(&:class)
123
115
  return true if atypes[ProcType] && btypes[ProcType]
124
- return true if intersect.call(SingletonType, &:module_or_class)
125
- intersect.call(InstanceType, &:klass)
116
+ if atypes[SingletonType] && btypes[SingletonType]
117
+ aa, bb = [atypes, btypes].map {|types| types[SingletonType].map(&:module_or_class) }
118
+ return true if (aa & bb).any?
119
+ end
120
+
121
+ aa, bb = [atypes, btypes].map {|types| (types[InstanceType] || []).map(&:klass) }
122
+ (aa.flat_map(&:ancestors) & bb).any?
126
123
  end
127
124
 
128
125
  def self.type_from_object(object)
@@ -196,16 +193,22 @@ module KatakataIrb::Types
196
193
  def nillable?() = (@klass == NilClass)
197
194
  def nonnillable() = self
198
195
  def inspect
199
- case klass
200
- when NilClass
196
+ if params.empty?
197
+ inspect_without_params
198
+ else
199
+ params_string = "[#{params.map { "#{_1}: #{_2.inspect}" }.join(', ')}]"
200
+ "#{inspect_without_params}#{params_string}"
201
+ end
202
+ end
203
+ def inspect_without_params
204
+ if klass == NilClass
201
205
  'nil'
202
- when TrueClass
206
+ elsif klass == TrueClass
203
207
  'true'
204
- when FalseClass
208
+ elsif klass == FalseClass
205
209
  'false'
206
210
  else
207
- params_string = "[#{params.map { "#{_1}: #{_2.inspect}" }.join(', ')}]" unless params.empty?
208
- "#{klass.name}#{params_string}"
211
+ klass.singleton_class? ? klass.superclass.to_s : klass.to_s
209
212
  end
210
213
  end
211
214
  end
@@ -327,18 +330,10 @@ module KatakataIrb::Types
327
330
  BOOLEAN
328
331
  when RBS::Types::Bases::Instance
329
332
  self_type.transform do |type|
330
- case type
331
- in SingletonClass
332
- InstanceType.new type.klass
333
- in InstanceType
334
- case type.klass
335
- in Class
336
- InstanceType.new Class
337
- in Module
338
- InstanceType.new Module
339
- else
340
- OBJECT
341
- end
333
+ if type.is_a?(SingletonType) && type.module_or_class.is_a?(Class)
334
+ InstanceType.new type.module_or_class
335
+ else
336
+ OBJECT
342
337
  end
343
338
  end
344
339
  when RBS::Types::Union
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KatakataIrb
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.8"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katakata_irb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - tompng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-23 00:00:00.000000000 Z
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: irb