katakata_irb 0.1.7 → 0.1.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d50fd34032f33c722cce1adc7118a354b2fdc31736f6d00d9557b9b72b361b65
4
- data.tar.gz: b874b7a2b3f65a91eebd447df6df55ae931d8050418942fd07b453611d8ff509
3
+ metadata.gz: ce4d7b2e5ebfc546ddb501c43c13c62b166a55dc34a215a460d560b946ad4f02
4
+ data.tar.gz: f23396dc9804a798d109342d063943da759139fd849fd828f85d51f71089d5e7
5
5
  SHA512:
6
- metadata.gz: c1d44c271e5ef109670345b8c85022553ab7618ffa30e4f979743912b30677fd8e9925d0b36c933d79f8aa0e7dc2be298e06db6f0a56cff442acb59b22ebc383
7
- data.tar.gz: e64e94002296f792a6bdd9b5637f0bf065c4366de653eb15161b3bc9c7aca1598e880344f393106c0ce0e92497cd2d13ee1f604619b70ebe986090a8fa1e74a2
6
+ metadata.gz: 5e44f1dbab26c56949e89053a0309ad9f3195a8f545ea95b546a541c547b55b86a69afb3944bdbfeb5e498a62105cb2f54630e559b32188b2532cfa4625ee765
7
+ data.tar.gz: 9beaaf9996eba51111cabc5a1ba4082f94657ecdeb06b3cf1c0859c346822d772134673d190639b874caacf3f220c19d8ac8ad150c2c98f85129e658a58a2b95
data/katakata_irb.gemspec CHANGED
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
 
31
31
  # Uncomment to register a new dependency of your gem
32
32
  spec.add_dependency 'irb', '>= 1.4.0'
33
+ spec.add_dependency 'reline', '>= 0.3.0'
33
34
  spec.add_dependency 'rbs'
34
35
 
35
36
  # For more information and examples about making a new gem, check out our
@@ -54,9 +54,9 @@ module KatakataIrb::Completor
54
54
  IRB::InputCompletor::CompletionProc.define_singleton_method :call do |*args|
55
55
  completion_proc.call(*args)
56
56
  rescue => e
57
- $error = e
57
+ KatakataIrb.last_completion_error = e
58
58
  KatakataIrb.log_puts
59
- KatakataIrb.log_puts "#{e.inspect} stored to $error"
59
+ KatakataIrb.log_puts "#{e.inspect} stored to KatakataIrb.last_completion_error"
60
60
  KatakataIrb.log_puts
61
61
  end
62
62
 
@@ -130,7 +130,8 @@ module KatakataIrb::Completor
130
130
  width = contents.map { Reline::Unicode.calculate_width _1 }.max
131
131
  x = cursor_pos_to_render.x + autocomplete_dialog.width
132
132
  y = cursor_pos_to_render.y
133
- Reline::DialogRenderInfo.new(pos: Reline::CursorPos.new(x, y), contents: contents, width: width, bg_color: 44, fg_color: 37)
133
+ info = { pos: Reline::CursorPos.new(x, y), contents: contents, width: width, bg_color: 44, fg_color: 37 }
134
+ Reline::DialogRenderInfo.new(**info.slice(*Reline::DialogRenderInfo.members))
134
135
  }
135
136
  Reline.add_dialog_proc(:show_type, type_dialog_proc, Reline::DEFAULT_DIALOG_CONTEXT)
136
137
  end
@@ -233,7 +234,9 @@ module KatakataIrb::Completor
233
234
  in_class_module = parents&.any? { _1 in [:class | :module,] }
234
235
  icvar_available = !in_class_module
235
236
  return unless target in [_type, String, [Integer, Integer]]
236
- if target in [:@ivar,]
237
+ if target in [:@gvar,]
238
+ return [:gvar, name]
239
+ elsif target in [:@ivar,]
237
240
  return [:ivar, name] if icvar_available
238
241
  elsif target in [:@cvar,]
239
242
  return [:cvar, name] if icvar_available
@@ -275,12 +278,13 @@ module KatakataIrb::Completor
275
278
  [:const, calculate_receiver.call(receiver), name]
276
279
  in [:top_const_ref, [:@const,]]
277
280
  [:const, KatakataIrb::Types::SingletonType.new(Object), name]
278
- in [:def,] | [:string_content,] | [:var_field,] | [:defs,] | [:rest_param,] | [:kwrest_param,] | [:blockarg,] | [[:@ident,],]
281
+ in [:def,] | [:string_content,] | [:field | :var_field | :const_path_field,] | [:defs,] | [:rest_param,] | [:kwrest_param,] | [:blockarg,] | [[:@ident,],]
279
282
  in [Array,] # `xstring`, /regexp/
280
283
  else
281
284
  KatakataIrb.log_puts
282
- KatakataIrb.log_puts [:NEW_EXPRESSION, expression].inspect
285
+ KatakataIrb.log_puts [:UNIMPLEMENTED_EXPRESSION, expression].inspect
283
286
  KatakataIrb.log_puts
287
+ nil
284
288
  end
285
289
  end
286
290
 
@@ -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
@@ -80,6 +80,7 @@ class KatakataIrb::TypeSimulator
80
80
  end
81
81
  if @dig_targets.dig? sexp
82
82
  params in [:paren, params]
83
+ params ||= [:params, nil, nil, nil, nil, nil, nil, nil] # params might be nil in ruby 3.0
83
84
  params_table = extract_param_names(params).to_h { [_1, KatakataIrb::Types::NIL] }
84
85
  method_scope = KatakataIrb::Scope.new(
85
86
  scope,
@@ -556,7 +557,7 @@ class KatakataIrb::TypeSimulator
556
557
  scope.update pattern_scope
557
558
  KatakataIrb::Types::UnionType[*results]
558
559
  in [:case, target_exp, match_exp]
559
- target = simulate_evaluate target_exp, scope
560
+ target = target_exp ? simulate_evaluate(target_exp, scope) : KatakataIrb::Types::NIL
560
561
  simulate_evaluate match_exp, scope, case_target: target
561
562
  in [:void_stmt]
562
563
  KatakataIrb::Types::NIL
@@ -894,6 +895,7 @@ class KatakataIrb::TypeSimulator
894
895
  in [:field | :aref_field,]
895
896
  # a.b, c[i] = value
896
897
  in [:excessed_comma]
898
+ in [:args_forward]
897
899
  end
898
900
  end
899
901
  [*pre_required, *post_required].each(&extract_mlhs)
@@ -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)
@@ -189,12 +186,19 @@ module KatakataIrb::Types
189
186
  @params = params
190
187
  end
191
188
  def transform() = yield(self)
192
- def methods() = @klass.instance_methods
193
- def all_methods() = @klass.instance_methods | @klass.private_instance_methods
189
+ def methods() = rbs_methods.select { _2.public? }.keys | @klass.instance_methods
190
+ def all_methods() = rbs_methods.keys | @klass.instance_methods | @klass.private_instance_methods
194
191
  def constants() = []
195
192
  def types() = [self]
196
193
  def nillable?() = (@klass == NilClass)
197
194
  def nonnillable() = self
195
+ def rbs_methods
196
+ name = KatakataIrb::Types.class_name_of(@klass)
197
+ return {} unless name
198
+
199
+ type_name = RBS::TypeName(name).absolute!
200
+ KatakataIrb::Types.rbs_builder.build_instance(type_name).methods rescue {}
201
+ end
198
202
  def inspect
199
203
  if params.empty?
200
204
  inspect_without_params
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KatakataIrb
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.9"
5
5
  end
data/lib/katakata_irb.rb CHANGED
@@ -2,12 +2,15 @@ require 'katakata_irb/version'
2
2
  require 'katakata_irb/completor'
3
3
 
4
4
  module KatakataIrb
5
- def self.log_output=(output)
6
- @log_output = output
7
- end
8
-
9
- def self.log_puts(...)
10
- STDOUT.cooked { @log_output&.puts(...) }
5
+ class << self
6
+ attr_accessor :log_output, :last_completion_error
7
+ def log_puts(...)
8
+ if STDOUT.tty?
9
+ STDOUT.cooked { log_output&.puts(...) }
10
+ else
11
+ log_output&.puts(...)
12
+ end
13
+ end
11
14
  end
12
15
  end
13
16
 
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.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - tompng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-08 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: irb
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: reline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rbs
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -46,7 +60,6 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - Gemfile
49
- - Gemfile.lock
50
63
  - LICENSE.txt
51
64
  - README.md
52
65
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,29 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- katakata_irb (0.1.7)
5
- irb (>= 1.4.0)
6
- rbs
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- io-console (0.6.0)
12
- irb (1.6.4)
13
- reline (>= 0.3.0)
14
- minitest (5.18.0)
15
- rake (13.0.6)
16
- rbs (3.1.0)
17
- reline (0.3.3)
18
- io-console (~> 0.5)
19
-
20
- PLATFORMS
21
- x86_64-darwin-20
22
-
23
- DEPENDENCIES
24
- katakata_irb!
25
- minitest (~> 5.0)
26
- rake (~> 13.0)
27
-
28
- BUNDLED WITH
29
- 2.4.5