From fcafc8fa3977109f891483a67607183888fcc3ac Mon Sep 17 00:00:00 2001 From: tompng Date: Tue, 28 Nov 2023 18:54:51 +0900 Subject: [PATCH] Change Gem name to repl_type_completor --- Gemfile | 2 +- README.md | 26 +++---- Rakefile | 6 +- bin/console | 4 +- ...l_completion.rb => repl_type_completor.rb} | 10 +-- .../methods.rb | 2 +- .../require_paths.rb | 2 +- .../result.rb | 6 +- .../scope.rb | 3 +- .../type_analyzer.rb | 2 +- .../types.rb | 2 +- .../version.rb | 2 +- ...ion.gemspec => repl_type_completor.gemspec | 6 +- ...completion.rbs => repl_type_completor.rbs} | 2 +- test/repl_completion/test_require_paths.rb | 26 ------- .../helper.rb | 2 +- .../test_repl_type_completor.rb} | 72 +++++++++---------- .../repl_type_completor/test_require_paths.rb | 26 +++++++ .../test_scope.rb | 42 +++++------ .../test_type_analyze.rb | 44 ++++++------ .../test_types.rb | 50 ++++++------- 21 files changed, 168 insertions(+), 169 deletions(-) rename lib/{repl_completion.rb => repl_type_completor.rb} (95%) rename lib/{repl_completion => repl_type_completor}/methods.rb (94%) rename lib/{repl_completion => repl_type_completor}/require_paths.rb (98%) rename lib/{repl_completion => repl_type_completor}/result.rb (97%) rename lib/{repl_completion => repl_type_completor}/scope.rb (99%) rename lib/{repl_completion => repl_type_completor}/type_analyzer.rb (99%) rename lib/{repl_completion => repl_type_completor}/types.rb (99%) rename lib/{repl_completion => repl_type_completor}/version.rb (68%) rename repl_completion.gemspec => repl_type_completor.gemspec (90%) rename sig/{repl_completion.rbs => repl_type_completor.rbs} (95%) delete mode 100644 test/repl_completion/test_require_paths.rb rename test/{repl_completion => repl_type_completor}/helper.rb (85%) rename test/{repl_completion/test_repl_completion.rb => repl_type_completor/test_repl_type_completor.rb} (73%) create mode 100644 test/repl_type_completor/test_require_paths.rb rename test/{repl_completion => repl_type_completor}/test_scope.rb (61%) rename test/{repl_completion => repl_type_completor}/test_type_analyze.rb (95%) rename test/{repl_completion => repl_type_completor}/test_types.rb (55%) diff --git a/Gemfile b/Gemfile index ef160dc..2d0066e 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in repl_completion.gemspec +# Specify your gem's dependencies in repl_type_completor.gemspec gemspec gem 'rake', '~> 13.0' diff --git a/README.md b/README.md index e41d12f..a32b129 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# ReplCompletion +# ReplTypeCompletor -ReplCompletion is a type based completor for REPL. +ReplTypeCompletor is a type based completor for REPL. It uses RBS type information, performs static type analytics, uses dynamic runtime information from binding. ## Installation @@ -19,20 +19,20 @@ If bundler is not being used to manage dependencies, install the gem by executin Require the library ```ruby -require 'repl_completion' +require 'repl_type_completor' ``` Load RBS with one of these. It will load core library signatures, `./rbs_collection.yaml` and `./sig/**/*.rbs`. ```ruby -ReplCompletion.preload_rbs # Recommended. Preload using thread -ReplCompletion.load_rbs # Could take a seconds in large projects +ReplTypeCompletor.preload_rbs # Recommended. Preload using thread +ReplTypeCompletor.load_rbs # Could take a seconds in large projects ``` Now you can get completion candidates. ```ruby array = [1, 2, 3] class String; def upupup; end; end -result = ReplCompletion.analyze('array.map do str = _1.chr; str.up', binding: binding) +result = ReplTypeCompletor.analyze('array.map do str = _1.chr; str.up', binding: binding) result.completion_candidates #=> ["case", "case!", "to", "upup"] result.doc_namespace('case') #=> "String#upcase" ``` @@ -45,16 +45,16 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/repl_completion. +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/repl_type_completor. When something is wrong, these methods will provide some debug information. ```ruby -ReplCompletion.info -ReplCompletion.rbs_load_started? -ReplCompletion.rbs_loaded? -ReplCompletion.rbs_load_error -ReplCompletion.last_completion_error -ReplCompletion.analyze(code_to_complete, binding: binding) +ReplTypeCompletor.info +ReplTypeCompletor.rbs_load_started? +ReplTypeCompletor.rbs_loaded? +ReplTypeCompletor.rbs_load_error +ReplTypeCompletor.last_completion_error +ReplTypeCompletor.analyze(code_to_complete, binding: binding) ``` ## License diff --git a/Rakefile b/Rakefile index 455ad63..3649952 100644 --- a/Rakefile +++ b/Rakefile @@ -6,15 +6,15 @@ require "rake/testtask" Rake::TestTask.new(:test) do |t| t.libs << "test" t.libs << "lib" - t.test_files = FileList["test/repl_completion/test_*.rb"] + t.test_files = FileList["test/repl_type_completor/test_*.rb"] end # To make sure they have been correctly setup for Ruby CI. -desc "Run each repl_completion test file in isolation." +desc "Run each repl_type_completor test file in isolation." task :test_in_isolation do failed = false - FileList["test/repl_completion/test_*.rb"].each do |test_file| + FileList["test/repl_type_completor/test_*.rb"].each do |test_file| ENV["TEST"] = test_file begin Rake::Task["test"].execute diff --git a/bin/console b/bin/console index 5b06e4b..32410f4 100755 --- a/bin/console +++ b/bin/console @@ -2,7 +2,7 @@ # frozen_string_literal: true require "bundler/setup" -require "repl_completion" +require "repl_type_completor" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. @@ -11,6 +11,6 @@ require "repl_completion" # require "pry" # Pry.start -ReplCompletion.preload_rbs +ReplTypeCompletor.preload_rbs require "irb" IRB.start(__FILE__) diff --git a/lib/repl_completion.rb b/lib/repl_type_completor.rb similarity index 95% rename from lib/repl_completion.rb rename to lib/repl_type_completor.rb index 3a09ba2..e04d736 100644 --- a/lib/repl_completion.rb +++ b/lib/repl_type_completor.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative 'repl_completion/version' -require_relative 'repl_completion/type_analyzer' -require_relative 'repl_completion/result' +require_relative 'repl_type_completor/version' +require_relative 'repl_type_completor/type_analyzer' +require_relative 'repl_type_completor/result' -module ReplCompletion +module ReplTypeCompletor class << self attr_reader :last_completion_error @@ -54,7 +54,7 @@ def info elsif !rbs_loaded? rbs_info << ' signatures loading' end - "ReplCompletion: #{VERSION}, #{prism_info}, #{rbs_info}" + "ReplTypeCompletor: #{VERSION}, #{prism_info}, #{rbs_info}" end private diff --git a/lib/repl_completion/methods.rb b/lib/repl_type_completor/methods.rb similarity index 94% rename from lib/repl_completion/methods.rb rename to lib/repl_type_completor/methods.rb index e867549..ccd8225 100644 --- a/lib/repl_completion/methods.rb +++ b/lib/repl_type_completor/methods.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ReplCompletion +module ReplTypeCompletor module Methods OBJECT_SINGLETON_CLASS_METHOD = Object.instance_method(:singleton_class) OBJECT_INSTANCE_VARIABLES_METHOD = Object.instance_method(:instance_variables) diff --git a/lib/repl_completion/require_paths.rb b/lib/repl_type_completor/require_paths.rb similarity index 98% rename from lib/repl_completion/require_paths.rb rename to lib/repl_type_completor/require_paths.rb index c3e02a7..dfab608 100644 --- a/lib/repl_completion/require_paths.rb +++ b/lib/repl_type_completor/require_paths.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ReplCompletion +module ReplTypeCompletor module RequirePaths class << self def require_completions(target_path) diff --git a/lib/repl_completion/result.rb b/lib/repl_type_completor/result.rb similarity index 97% rename from lib/repl_completion/result.rb rename to lib/repl_type_completor/result.rb index ac9304e..1de88e5 100644 --- a/lib/repl_completion/result.rb +++ b/lib/repl_type_completor/result.rb @@ -2,7 +2,7 @@ require_relative 'require_paths' -module ReplCompletion +module ReplTypeCompletor class Result HIDDEN_METHODS = %w[Namespace TypeName] # defined by rbs, should be hidden RESERVED_WORDS = %w[ @@ -68,7 +68,7 @@ def completion_candidates end candidates.select { _1.start_with?(name) }.map { _1[name.size..] } rescue Exception => e - ReplCompletion.handle_exception(e) + ReplTypeCompletor.handle_exception(e) [] ensure $VERBOSE = verbose @@ -102,7 +102,7 @@ def doc_namespace(matched) else end rescue Exception => e - ReplCompletion.handle_exception(e) + ReplTypeCompletor.handle_exception(e) nil ensure $VERBOSE = verbose diff --git a/lib/repl_completion/scope.rb b/lib/repl_type_completor/scope.rb similarity index 99% rename from lib/repl_completion/scope.rb rename to lib/repl_type_completor/scope.rb index aae418a..cefa395 100644 --- a/lib/repl_completion/scope.rb +++ b/lib/repl_type_completor/scope.rb @@ -3,8 +3,7 @@ require 'set' require_relative 'types' -module ReplCompletion - +module ReplTypeCompletor class RootScope attr_reader :module_nesting, :self_object diff --git a/lib/repl_completion/type_analyzer.rb b/lib/repl_type_completor/type_analyzer.rb similarity index 99% rename from lib/repl_completion/type_analyzer.rb rename to lib/repl_type_completor/type_analyzer.rb index 2f8d7a3..f6b16ab 100644 --- a/lib/repl_completion/type_analyzer.rb +++ b/lib/repl_type_completor/type_analyzer.rb @@ -5,7 +5,7 @@ require_relative 'scope' require 'prism' -module ReplCompletion +module ReplTypeCompletor class TypeAnalyzer class DigTarget def initialize(parents, receiver, &block) diff --git a/lib/repl_completion/types.rb b/lib/repl_type_completor/types.rb similarity index 99% rename from lib/repl_completion/types.rb rename to lib/repl_type_completor/types.rb index 36bd3ba..c002284 100644 --- a/lib/repl_completion/types.rb +++ b/lib/repl_type_completor/types.rb @@ -2,7 +2,7 @@ require_relative 'methods' -module ReplCompletion +module ReplTypeCompletor module Types OBJECT_TO_TYPE_SAMPLE_SIZE = 50 diff --git a/lib/repl_completion/version.rb b/lib/repl_type_completor/version.rb similarity index 68% rename from lib/repl_completion/version.rb rename to lib/repl_type_completor/version.rb index ada1508..c564389 100644 --- a/lib/repl_completion/version.rb +++ b/lib/repl_type_completor/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -module ReplCompletion +module ReplTypeCompletor VERSION = "0.1.0" end diff --git a/repl_completion.gemspec b/repl_type_completor.gemspec similarity index 90% rename from repl_completion.gemspec rename to repl_type_completor.gemspec index 68e21e8..3b640ec 100644 --- a/repl_completion.gemspec +++ b/repl_type_completor.gemspec @@ -1,10 +1,10 @@ # frozen_string_literal: true -require_relative "lib/repl_completion/version" +require_relative "lib/repl_type_completor/version" Gem::Specification.new do |spec| - spec.name = "repl_completion" - spec.version = ReplCompletion::VERSION + spec.name = "repl_type_completor" + spec.version = ReplTypeCompletor::VERSION spec.authors = ["tompng"] spec.email = ["tomoyapenguin@gmail.com"] diff --git a/sig/repl_completion.rbs b/sig/repl_type_completor.rbs similarity index 95% rename from sig/repl_completion.rbs rename to sig/repl_type_completor.rbs index c70963a..fd964ad 100644 --- a/sig/repl_completion.rbs +++ b/sig/repl_type_completor.rbs @@ -1,4 +1,4 @@ -module ReplCompletion +module ReplTypeCompletor VERSION: String class Result diff --git a/test/repl_completion/test_require_paths.rb b/test/repl_completion/test_require_paths.rb deleted file mode 100644 index 6737e57..0000000 --- a/test/repl_completion/test_require_paths.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -require 'repl_completion' -require_relative './helper' - -module TestReplCompletion - class RequirePathsTest < TestCase - def test_require_paths - assert_include ReplCompletion::RequirePaths.require_completions('repl_com'), 'repl_completion' - assert_include ReplCompletion::RequirePaths.require_completions('repl_com'), 'repl_completion/version' - assert_equal ['repl_completion/version'], ReplCompletion::RequirePaths.require_completions('repl_completion/vers') - end - - def test_require_relative_paths - assert_include ReplCompletion::RequirePaths.require_relative_completions('test_re', __FILE__), 'test_require_paths' - assert_include ReplCompletion::RequirePaths.require_relative_completions('../repl_', __FILE__), '../repl_completion/test_require_paths' - project_root = File.expand_path('../../Gemfile', __dir__) - assert_not_include ReplCompletion::RequirePaths.require_relative_completions('li', __FILE__), 'lib/repl_completion' - assert_include ReplCompletion::RequirePaths.require_relative_completions('li', project_root), 'lib/repl_completion' - # Incrementally complete deep path - assert_include ReplCompletion::RequirePaths.require_relative_completions('li', project_root), 'lib/repl_completion/' - assert_not_include ReplCompletion::RequirePaths.require_relative_completions('li', project_root), 'lib/repl_completion/version' - assert_include ReplCompletion::RequirePaths.require_relative_completions('lib/', project_root), 'lib/repl_completion/version' - end - end -end diff --git a/test/repl_completion/helper.rb b/test/repl_type_completor/helper.rb similarity index 85% rename from test/repl_completion/helper.rb rename to test/repl_type_completor/helper.rb index 7776961..c8555a0 100644 --- a/test/repl_completion/helper.rb +++ b/test/repl_type_completor/helper.rb @@ -3,7 +3,7 @@ require 'test/unit' require 'core_assertions' -module TestReplCompletion +module TestReplTypeCompletor class TestCase < Test::Unit::TestCase include Test::Unit::CoreAssertions end diff --git a/test/repl_completion/test_repl_completion.rb b/test/repl_type_completor/test_repl_type_completor.rb similarity index 73% rename from test/repl_completion/test_repl_completion.rb rename to test/repl_type_completor/test_repl_type_completor.rb index 6b600ef..c80bd97 100644 --- a/test/repl_completion/test_repl_completion.rb +++ b/test/repl_type_completor/test_repl_type_completor.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true -require 'repl_completion' +require 'repl_type_completor' require_relative './helper' -module TestReplCompletion - class ReplCompletionTest < TestCase +module TestReplTypeCompletor + class ReplTypeCompletorTest < TestCase def setup - ReplCompletion.load_rbs unless ReplCompletion.rbs_loaded? + ReplTypeCompletor.load_rbs unless ReplTypeCompletor.rbs_loaded? end def empty_binding @@ -17,23 +17,23 @@ def empty_binding def assert_completion(code, binding: empty_binding, filename: nil, include: nil, exclude: nil) raise ArgumentError if include.nil? && exclude.nil? - candidates = ReplCompletion.analyze(code, binding: binding, filename: filename).completion_candidates + candidates = ReplTypeCompletor.analyze(code, binding: binding, filename: filename).completion_candidates assert ([*include] - candidates).empty?, "Expected #{candidates} to include #{include}" if include assert (candidates & [*exclude]).empty?, "Expected #{candidates} not to include #{exclude}" if exclude end def assert_doc_namespace(code, namespace, binding: empty_binding) - assert_equal namespace, ReplCompletion.analyze(code, binding: binding).doc_namespace('') + assert_equal namespace, ReplTypeCompletor.analyze(code, binding: binding).doc_namespace('') end def test_require assert_completion("require '", include: 'set') assert_completion("require 's", include: 'et') - assert_completion("require_relative 'test_", filename: __FILE__, include: 'repl_completion') - assert_completion("require_relative '../repl_", filename: __FILE__, include: 'completion/test_repl_completion') + assert_completion("require_relative 'test_", filename: __FILE__, include: 'repl_type_completor') + assert_completion("require_relative '../repl_", filename: __FILE__, include: 'type_completor/test_repl_type_completor') Dir.chdir File.join(__dir__, '..') do - assert_completion("require_relative 'repl_", filename: nil, include: 'completion/test_repl_completion') - assert_completion("require_relative 'repl_", filename: '(irb)', include: 'completion/test_repl_completion') + assert_completion("require_relative 'repl_", filename: nil, include: 'type_completor/test_repl_type_completor') + assert_completion("require_relative 'repl_", filename: '(irb)', include: 'type_completor/test_repl_type_completor') end # Incomplete double quote string is InterpolatedStringNode @@ -60,7 +60,7 @@ def test_call assert_completion('ran', include: 'd') assert_doc_namespace('1.abs', 'Integer#abs') assert_doc_namespace('Integer.sqrt', 'Integer.sqrt') - assert_doc_namespace('rand', 'TestReplCompletion::ReplCompletionTest#rand') + assert_doc_namespace('rand', 'TestReplTypeCompletor::ReplTypeCompletorTest#rand') assert_doc_namespace('Object::rand', 'Object.rand') end @@ -79,7 +79,7 @@ def test_lvar def test_const assert_completion('Ar', include: 'ray') assert_completion('::Ar', include: 'ray') - assert_completion('ReplCompletion::V', include: 'ERSION') + assert_completion('ReplTypeCompletor::V', include: 'ERSION') assert_completion('FooBar=1; F', include: 'ooBar') assert_completion('::FooBar=1; ::F', include: 'ooBar') assert_doc_namespace('Array', 'Array') @@ -87,7 +87,7 @@ def test_const assert_doc_namespace('Object::Array', 'Array') assert_completion('::', include: 'Array') assert_completion('class ::', include: 'Array') - assert_completion('module ReplCompletion; class T', include: ['ypes', 'racePoint']) + assert_completion('module ReplTypeCompletor; class T', include: ['ypes', 'racePoint']) end def test_gvar @@ -161,22 +161,22 @@ def test_deprecated_const_without_warning end def test_sig_dir - assert_doc_namespace('ReplCompletion.analyze(code, binding: binding).completion_candidates.__id__', 'Array#__id__') - assert_doc_namespace('ReplCompletion.analyze(code, binding: binding).doc_namespace.__id__', 'String#__id__') + assert_doc_namespace('ReplTypeCompletor.analyze(code, binding: binding).completion_candidates.__id__', 'Array#__id__') + assert_doc_namespace('ReplTypeCompletor.analyze(code, binding: binding).doc_namespace.__id__', 'String#__id__') end def test_none - result = ReplCompletion.analyze('()', binding: binding) + result = ReplTypeCompletor.analyze('()', binding: binding) assert_nil result end - def test_repl_completion_api - assert_nil ReplCompletion.rbs_load_error - assert_nil ReplCompletion.last_completion_error - assert_equal true, ReplCompletion.rbs_load_started? - assert_equal true, ReplCompletion.rbs_loaded? - assert_nothing_raised { ReplCompletion.preload_rbs } - assert_nothing_raised { ReplCompletion.load_rbs } + def test_repl_type_completor_api + assert_nil ReplTypeCompletor.rbs_load_error + assert_nil ReplTypeCompletor.last_completion_error + assert_equal true, ReplTypeCompletor.rbs_load_started? + assert_equal true, ReplTypeCompletor.rbs_loaded? + assert_nothing_raised { ReplTypeCompletor.preload_rbs } + assert_nothing_raised { ReplTypeCompletor.load_rbs } end def with_failing_method(klass, method_name, message) @@ -192,36 +192,36 @@ def with_failing_method(klass, method_name, message) end def test_analyze_error - with_failing_method(ReplCompletion.singleton_class, :analyze_code, 'error_in_analyze_code') do - assert_nil ReplCompletion.analyze('1.', binding: binding) + with_failing_method(ReplTypeCompletor.singleton_class, :analyze_code, 'error_in_analyze_code') do + assert_nil ReplTypeCompletor.analyze('1.', binding: binding) end - assert_equal 'error_in_analyze_code', ReplCompletion.last_completion_error&.message + assert_equal 'error_in_analyze_code', ReplTypeCompletor.last_completion_error&.message ensure - ReplCompletion.instance_variable_set(:@last_completion_error, nil) + ReplTypeCompletor.instance_variable_set(:@last_completion_error, nil) end def test_completion_candidates_error - result = ReplCompletion.analyze '1.', binding: binding - with_failing_method(ReplCompletion::Types::InstanceType, :methods, 'error_in_methods') do + result = ReplTypeCompletor.analyze '1.', binding: binding + with_failing_method(ReplTypeCompletor::Types::InstanceType, :methods, 'error_in_methods') do assert_equal [], result.completion_candidates end - assert_equal 'error_in_methods', ReplCompletion.last_completion_error&.message + assert_equal 'error_in_methods', ReplTypeCompletor.last_completion_error&.message ensure - ReplCompletion.instance_variable_set(:@last_completion_error, nil) + ReplTypeCompletor.instance_variable_set(:@last_completion_error, nil) end def test_doc_namespace_error - result = ReplCompletion.analyze '1.', binding: binding - with_failing_method(ReplCompletion::Result, :method_doc, 'error_in_method_doc') do + result = ReplTypeCompletor.analyze '1.', binding: binding + with_failing_method(ReplTypeCompletor::Result, :method_doc, 'error_in_method_doc') do assert_nil result.doc_namespace('abs') end - assert_equal 'error_in_method_doc', ReplCompletion.last_completion_error&.message + assert_equal 'error_in_method_doc', ReplTypeCompletor.last_completion_error&.message ensure - ReplCompletion.instance_variable_set(:@last_completion_error, nil) + ReplTypeCompletor.instance_variable_set(:@last_completion_error, nil) end def test_info - assert_equal "ReplCompletion: #{ReplCompletion::VERSION}, Prism: #{Prism::VERSION}, RBS: #{RBS::VERSION}", ReplCompletion.info + assert_equal "ReplTypeCompletor: #{ReplTypeCompletor::VERSION}, Prism: #{Prism::VERSION}, RBS: #{RBS::VERSION}", ReplTypeCompletor.info end end end diff --git a/test/repl_type_completor/test_require_paths.rb b/test/repl_type_completor/test_require_paths.rb new file mode 100644 index 0000000..331c75a --- /dev/null +++ b/test/repl_type_completor/test_require_paths.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'repl_type_completor' +require_relative './helper' + +module TestReplTypeCompletor + class RequirePathsTest < TestCase + def test_require_paths + assert_include ReplTypeCompletor::RequirePaths.require_completions('repl_type_com'), 'repl_type_completor' + assert_include ReplTypeCompletor::RequirePaths.require_completions('repl_type_com'), 'repl_type_completor/version' + assert_equal ['repl_type_completor/version'], ReplTypeCompletor::RequirePaths.require_completions('repl_type_completor/vers') + end + + def test_require_relative_paths + assert_include ReplTypeCompletor::RequirePaths.require_relative_completions('test_re', __FILE__), 'test_require_paths' + assert_include ReplTypeCompletor::RequirePaths.require_relative_completions('../repl_', __FILE__), '../repl_type_completor/test_require_paths' + project_root = File.expand_path('../../Gemfile', __dir__) + assert_not_include ReplTypeCompletor::RequirePaths.require_relative_completions('li', __FILE__), 'lib/repl_type_completor' + assert_include ReplTypeCompletor::RequirePaths.require_relative_completions('li', project_root), 'lib/repl_type_completor' + # Incrementally complete deep path + assert_include ReplTypeCompletor::RequirePaths.require_relative_completions('li', project_root), 'lib/repl_type_completor/' + assert_not_include ReplTypeCompletor::RequirePaths.require_relative_completions('li', project_root), 'lib/repl_type_completor/version' + assert_include ReplTypeCompletor::RequirePaths.require_relative_completions('lib/', project_root), 'lib/repl_type_completor/version' + end + end +end diff --git a/test/repl_completion/test_scope.rb b/test/repl_type_completor/test_scope.rb similarity index 61% rename from test/repl_completion/test_scope.rb rename to test/repl_type_completor/test_scope.rb index ffcae20..67e62b0 100644 --- a/test/repl_completion/test_scope.rb +++ b/test/repl_type_completor/test_scope.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true -require 'repl_completion' +require 'repl_type_completor' require_relative './helper' -module TestReplCompletion +module TestReplTypeCompletor class ScopeTest < TestCase A, B, C, D, E, F, G, H, I, J, K = ('A'..'K').map do |name| klass = Class.new klass.define_singleton_method(:inspect) { name } - ReplCompletion::Types::InstanceType.new(klass) + ReplTypeCompletor::Types::InstanceType.new(klass) end def assert_type(expected_types, type) @@ -16,29 +16,29 @@ def assert_type(expected_types, type) end def table(*local_variable_names) - local_variable_names.to_h { [_1, ReplCompletion::Types::NIL] } + local_variable_names.to_h { [_1, ReplTypeCompletor::Types::NIL] } end def base_scope - ReplCompletion::RootScope.new(binding, Object.new, []) + ReplTypeCompletor::RootScope.new(binding, Object.new, []) end def test_lvar - scope = ReplCompletion::Scope.new base_scope, table('a') + scope = ReplTypeCompletor::Scope.new base_scope, table('a') scope['a'] = A assert_equal A, scope['a'] end def test_conditional - scope = ReplCompletion::Scope.new base_scope, table('a') + scope = ReplTypeCompletor::Scope.new base_scope, table('a') scope.conditional do |sub_scope| sub_scope['a'] = A end - assert_type [A, ReplCompletion::Types::NIL], scope['a'] + assert_type [A, ReplTypeCompletor::Types::NIL], scope['a'] end def test_branch - scope = ReplCompletion::Scope.new base_scope, table('a', 'b', 'c', 'd') + scope = ReplTypeCompletor::Scope.new base_scope, table('a', 'b', 'c', 'd') scope['c'] = A scope['d'] = B scope.run_branches( @@ -48,16 +48,16 @@ def test_branch -> { _1['a'] = _1['b'] = _1['c'] = F; _1.terminate } ) assert_type [C, D, E], scope['a'] - assert_type [ReplCompletion::Types::NIL, D, E], scope['b'] + assert_type [ReplTypeCompletor::Types::NIL, D, E], scope['b'] assert_type [A, C], scope['c'] assert_type [C, D, E], scope['d'] end def test_scope_local_variables - scope1 = ReplCompletion::Scope.new base_scope, table('a', 'b') - scope2 = ReplCompletion::Scope.new scope1, table('b', 'c'), trace_lvar: false - scope3 = ReplCompletion::Scope.new scope2, table('c', 'd') - scope4 = ReplCompletion::Scope.new scope2, table('d', 'e') + scope1 = ReplTypeCompletor::Scope.new base_scope, table('a', 'b') + scope2 = ReplTypeCompletor::Scope.new scope1, table('b', 'c'), trace_lvar: false + scope3 = ReplTypeCompletor::Scope.new scope2, table('c', 'd') + scope4 = ReplTypeCompletor::Scope.new scope2, table('d', 'e') assert_empty base_scope.local_variables assert_equal %w[a b], scope1.local_variables.sort assert_equal %w[b c], scope2.local_variables.sort @@ -66,11 +66,11 @@ def test_scope_local_variables end def test_nested_scope - scope = ReplCompletion::Scope.new base_scope, table('a', 'b', 'c') + scope = ReplTypeCompletor::Scope.new base_scope, table('a', 'b', 'c') scope['a'] = A scope['b'] = A scope['c'] = A - sub_scope = ReplCompletion::Scope.new scope, { 'c' => B } + sub_scope = ReplTypeCompletor::Scope.new scope, { 'c' => B } assert_type A, sub_scope['a'] assert_type A, sub_scope['b'] @@ -88,20 +88,20 @@ def test_nested_scope end def test_break - scope = ReplCompletion::Scope.new base_scope, table('a') + scope = ReplTypeCompletor::Scope.new base_scope, table('a') scope['a'] = A - breakable_scope = ReplCompletion::Scope.new scope, { ReplCompletion::Scope::BREAK_RESULT => nil } + breakable_scope = ReplTypeCompletor::Scope.new scope, { ReplTypeCompletor::Scope::BREAK_RESULT => nil } breakable_scope.conditional do |sub| sub['a'] = B assert_type [B], sub['a'] - sub.terminate_with ReplCompletion::Scope::BREAK_RESULT, C + sub.terminate_with ReplTypeCompletor::Scope::BREAK_RESULT, C sub['a'] = C assert_type [C], sub['a'] end assert_type [A], breakable_scope['a'] - breakable_scope[ReplCompletion::Scope::BREAK_RESULT] = D + breakable_scope[ReplTypeCompletor::Scope::BREAK_RESULT] = D breakable_scope.merge_jumps - assert_type [C, D], breakable_scope[ReplCompletion::Scope::BREAK_RESULT] + assert_type [C, D], breakable_scope[ReplTypeCompletor::Scope::BREAK_RESULT] scope.update breakable_scope assert_type [A, B], scope['a'] end diff --git a/test/repl_completion/test_type_analyze.rb b/test/repl_type_completor/test_type_analyze.rb similarity index 95% rename from test/repl_completion/test_type_analyze.rb rename to test/repl_type_completor/test_type_analyze.rb index e156751..03c818e 100644 --- a/test/repl_completion/test_type_analyze.rb +++ b/test/repl_type_completor/test_type_analyze.rb @@ -1,22 +1,22 @@ # frozen_string_literal: true -require 'repl_completion' +require 'repl_type_completor' require_relative './helper' -module TestReplCompletion +module TestReplTypeCompletor class AnalyzeTest < TestCase def setup - @handle_exception_method = ReplCompletion.method(:handle_exception) - ReplCompletion.singleton_class.remove_method(:handle_exception) - def ReplCompletion.handle_exception(e) + @handle_exception_method = ReplTypeCompletor.method(:handle_exception) + ReplTypeCompletor.singleton_class.remove_method(:handle_exception) + def ReplTypeCompletor.handle_exception(e) raise e end - ReplCompletion::Types.load_rbs_builder unless ReplCompletion::Types.rbs_builder + ReplTypeCompletor::Types.load_rbs_builder unless ReplTypeCompletor::Types.rbs_builder end def teardown - ReplCompletion.singleton_class.remove_method(:handle_exception) - ReplCompletion.define_singleton_method(:handle_exception, &@handle_exception_method) + ReplTypeCompletor.singleton_class.remove_method(:handle_exception) + ReplTypeCompletor.define_singleton_method(:handle_exception, &@handle_exception_method) end def empty_binding @@ -24,7 +24,7 @@ def empty_binding end def analyze(code, binding: nil) - ReplCompletion.send(:analyze_code, code, binding || empty_binding) + ReplTypeCompletor.send(:analyze_code, code, binding || empty_binding) end def assert_analyze_type(code, type, token = nil, binding: empty_binding) @@ -439,24 +439,24 @@ def test_constant_path assert_call('class A; X=1; class B; X=""; X.', include: String, exclude: Integer) assert_call('class A; X=1; class B; X=""; end; X.', include: Integer, exclude: String) assert_call('class A; class B; X=1; end; end; class A; class B; X.', include: Integer) - assert_call('module ReplCompletion; VERSION.', include: String) - assert_call('module ReplCompletion; ReplCompletion::VERSION.', include: String) - assert_call('module ReplCompletion; VERSION=1; VERSION.', include: Integer) - assert_call('module ReplCompletion; VERSION=1; ReplCompletion::VERSION.', include: Integer) - assert_call('module ReplCompletion; module A; VERSION.', include: String) - assert_call('module ReplCompletion; module A; VERSION=1; VERSION.', include: Integer) - assert_call('module ReplCompletion; module A; VERSION=1; ReplCompletion::VERSION.', include: String) - assert_call('module ReplCompletion; module A; VERSION=1; end; VERSION.', include: String) - assert_call('module ReplCompletion; ReplCompletion=1; ReplCompletion.', include: Integer) - assert_call('module ReplCompletion; ReplCompletion=1; ::ReplCompletion::VERSION.', include: String) - module_binding = eval 'module ::ReplCompletion; binding; end' + assert_call('module ReplTypeCompletor; VERSION.', include: String) + assert_call('module ReplTypeCompletor; ReplTypeCompletor::VERSION.', include: String) + assert_call('module ReplTypeCompletor; VERSION=1; VERSION.', include: Integer) + assert_call('module ReplTypeCompletor; VERSION=1; ReplTypeCompletor::VERSION.', include: Integer) + assert_call('module ReplTypeCompletor; module A; VERSION.', include: String) + assert_call('module ReplTypeCompletor; module A; VERSION=1; VERSION.', include: Integer) + assert_call('module ReplTypeCompletor; module A; VERSION=1; ReplTypeCompletor::VERSION.', include: String) + assert_call('module ReplTypeCompletor; module A; VERSION=1; end; VERSION.', include: String) + assert_call('module ReplTypeCompletor; ReplTypeCompletor=1; ReplTypeCompletor.', include: Integer) + assert_call('module ReplTypeCompletor; ReplTypeCompletor=1; ::ReplTypeCompletor::VERSION.', include: String) + module_binding = eval 'module ::ReplTypeCompletor; binding; end' assert_call('VERSION.', include: NilClass) assert_call('VERSION.', include: String, binding: module_binding) - assert_call('ReplCompletion::VERSION.', include: String, binding: module_binding) + assert_call('ReplTypeCompletor::VERSION.', include: String, binding: module_binding) assert_call('A = 1; module M; A += 0.5; A.', include: Float) assert_call('::A = 1; module M; A += 0.5; A.', include: Float) assert_call('::A = 1; module M; A += 0.5; ::A.', include: Integer) - assert_call('ReplCompletion::A = 1; ReplCompletion::A += 0.5; ReplCompletion::A.', include: Float) + assert_call('ReplTypeCompletor::A = 1; ReplTypeCompletor::A += 0.5; ReplTypeCompletor::A.', include: Float) end def test_literal diff --git a/test/repl_completion/test_types.rb b/test/repl_type_completor/test_types.rb similarity index 55% rename from test/repl_completion/test_types.rb rename to test/repl_type_completor/test_types.rb index 9062478..642b2dd 100644 --- a/test/repl_completion/test_types.rb +++ b/test/repl_type_completor/test_types.rb @@ -1,27 +1,27 @@ # frozen_string_literal: true -require 'repl_completion' +require 'repl_type_completor' require_relative './helper' -module TestReplCompletion +module TestReplTypeCompletor class TypesTest < TestCase def test_type_inspect - true_type = ReplCompletion::Types::TRUE - false_type = ReplCompletion::Types::FALSE - nil_type = ReplCompletion::Types::NIL - string_type = ReplCompletion::Types::STRING - true_or_false = ReplCompletion::Types::UnionType[true_type, false_type] - array_type = ReplCompletion::Types::InstanceType.new Array, { Elem: true_or_false } + true_type = ReplTypeCompletor::Types::TRUE + false_type = ReplTypeCompletor::Types::FALSE + nil_type = ReplTypeCompletor::Types::NIL + string_type = ReplTypeCompletor::Types::STRING + true_or_false = ReplTypeCompletor::Types::UnionType[true_type, false_type] + array_type = ReplTypeCompletor::Types::InstanceType.new Array, { Elem: true_or_false } assert_equal 'nil', nil_type.inspect assert_equal 'true', true_type.inspect assert_equal 'false', false_type.inspect assert_equal 'String', string_type.inspect - assert_equal 'Array', ReplCompletion::Types::InstanceType.new(Array).inspect + assert_equal 'Array', ReplTypeCompletor::Types::InstanceType.new(Array).inspect assert_equal 'true | false', true_or_false.inspect assert_equal 'Array[Elem: true | false]', array_type.inspect assert_equal 'Array', array_type.inspect_without_params - assert_equal 'Proc', ReplCompletion::Types::PROC.inspect - assert_equal 'Array.itself', ReplCompletion::Types::SingletonType.new(Array).inspect + assert_equal 'Proc', ReplTypeCompletor::Types::PROC.inspect + assert_equal 'Array.itself', ReplTypeCompletor::Types::SingletonType.new(Array).inspect end def test_type_from_object @@ -30,14 +30,14 @@ def test_type_from_object def bo.hash; 42; end # Needed to use this object as a hash key arr = [1, 'a'] hash = { 'key' => :value } - int_type = ReplCompletion::Types.type_from_object 1 - obj_type = ReplCompletion::Types.type_from_object obj - arr_type = ReplCompletion::Types.type_from_object arr - hash_type = ReplCompletion::Types.type_from_object hash - bo_type = ReplCompletion::Types.type_from_object bo - bo_arr_type = ReplCompletion::Types.type_from_object [bo] - bo_key_hash_type = ReplCompletion::Types.type_from_object({ bo => 1 }) - bo_value_hash_type = ReplCompletion::Types.type_from_object({ x: bo }) + int_type = ReplTypeCompletor::Types.type_from_object 1 + obj_type = ReplTypeCompletor::Types.type_from_object obj + arr_type = ReplTypeCompletor::Types.type_from_object arr + hash_type = ReplTypeCompletor::Types.type_from_object hash + bo_type = ReplTypeCompletor::Types.type_from_object bo + bo_arr_type = ReplTypeCompletor::Types.type_from_object [bo] + bo_key_hash_type = ReplTypeCompletor::Types.type_from_object({ bo => 1 }) + bo_value_hash_type = ReplTypeCompletor::Types.type_from_object({ x: bo }) assert_equal Integer, int_type.klass # Use singleton_class to autocomplete singleton methods @@ -55,8 +55,8 @@ def bo.hash; 42; end # Needed to use this object as a hash key assert_equal 'Object', obj_type.inspect assert_equal 'Array[Elem: Integer | String]', arr_type.inspect assert_equal 'Hash[K: String, V: Symbol]', hash_type.inspect - assert_equal 'Array.itself', ReplCompletion::Types.type_from_object(Array).inspect - assert_equal 'ReplCompletion.itself', ReplCompletion::Types.type_from_object(ReplCompletion).inspect + assert_equal 'Array.itself', ReplTypeCompletor::Types.type_from_object(Array).inspect + assert_equal 'ReplTypeCompletor.itself', ReplTypeCompletor::Types.type_from_object(ReplTypeCompletor).inspect end def test_type_methods @@ -67,11 +67,11 @@ def foobar; end end String.define_method(:foobarbaz) {} targets = [:foobar, :foobaz, :foobarbaz] - type = ReplCompletion::Types.type_from_object s + type = ReplTypeCompletor::Types.type_from_object s assert_equal [:foobar, :foobarbaz], targets & type.methods assert_equal [:foobar, :foobaz, :foobarbaz], targets & type.all_methods - assert_equal [:foobarbaz], targets & ReplCompletion::Types::STRING.methods - assert_equal [:foobarbaz], targets & ReplCompletion::Types::STRING.all_methods + assert_equal [:foobarbaz], targets & ReplTypeCompletor::Types::STRING.methods + assert_equal [:foobarbaz], targets & ReplTypeCompletor::Types::STRING.all_methods ensure String.remove_method :foobarbaz end @@ -79,7 +79,7 @@ def foobar; end def test_basic_object_methods bo = BasicObject.new def bo.foobar; end - type = ReplCompletion::Types.type_from_object bo + type = ReplTypeCompletor::Types.type_from_object bo assert type.all_methods.include?(:foobar) end end