Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support unconvention compilers #2150

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,32 @@ def version

sig { params(changes: T::Array[{ uri: String, type: Integer }]).void }
def workspace_did_change_watched_files(changes)
T.must(@outgoing_queue) << Notification.window_log_message("***: did_change")
return unless T.must(@global_state).enabled_feature?(:tapiocaAddon)
return unless @rails_runner_client # Client is not ready

T.must(@outgoing_queue) << Notification.window_log_message("***: collecting")

constants = changes.flat_map do |change|
path = URI(change[:uri]).to_standardized_path
T.must(@outgoing_queue) << Notification.window_log_message("***: path: #{path}")
next if path.end_with?("_test.rb", "_spec.rb")
next unless file_updated?(change, path)

entries = T.must(@index).entries_for(change[:uri])
next unless entries
entries = T.must(@index).entries_for(change[:uri]) || []
# unless entries
# T.must(@outgoing_queue) << Notification.window_log_message("***: nexting")
# next
# end
T.must(@outgoing_queue) << Notification.window_log_message("***: nexting")

entries.filter_map do |entry|
entry.name if entry.class == RubyIndexer::Entry::Class || entry.class == RubyIndexer::Entry::Module
end
end + [path]
end.compact

T.must(@outgoing_queue) << Notification.window_log_message("***: #{constants}")

return if constants.empty?

@rails_runner_client.trigger_reload
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/commands/abstract_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def load_application
def create_pipeline
Tapioca::Dsl::Pipeline.new(
requested_constants:
constantize(@requested_constants) + constantize(constants_from_requested_paths, ignore_missing: true),
constantize(@requested_constants) + constantize(constants_from_requested_paths, ignore_missing: true), # constants_from_requested_paths
requested_paths: @requested_paths,
requested_compilers: constantize_compilers(@only),
excluded_compilers: constantize_compilers(@exclude),
Expand Down
5 changes: 5 additions & 0 deletions lib/tapioca/dsl/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def handles?(constant)
processable_constants.include?(constant)
end

sig { params(path: String).returns(T::Boolean) }
def handles_path?(path)
path.end_with?(".rb")
end

sig { abstract.returns(T::Enumerable[Module]) }
def gather_constants; end

Expand Down
6 changes: 6 additions & 0 deletions lib/tapioca/dsl/compilers/active_record_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def gather_constants

[ActiveSupport::TestCase]
end

sig { params(path: String).returns(T::Boolean) }
def handles_path?(path)
# TODO: `spec` too?
File.fnmatch("test/fixtures/**/*.yml{,.erb}", path, File::FNM_PATHNAME | File::FNM_EXTGLOB)
end
end

private
Expand Down
14 changes: 14 additions & 0 deletions lib/tapioca/dsl/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ def run(&blk)
.sort_by! { |c| T.must(Runtime::Reflection.name_of(c)) }

# It's OK if there are no constants to process if we received a valid file/path.
# binding.break

requested_paths.each do |requested_path|
@active_compilers.each do |compiler|
next unless compiler.handles_path?(requested_path.to_s)

# TODO: generalize for other types
# constants_to_process << compiler.gather_constants
constants_to_process += compiler.gather_constants
end
end
# binding.break
if constants_to_process.empty? && requested_paths.none? { |p| File.exist?(p) }
report_error(<<~ERROR)
No classes/modules can be matched for RBI generation.
Expand All @@ -84,6 +96,7 @@ def run(&blk)
abort_if_pending_migrations!
end

# binding.break
result = Executor.new(
constants_to_process,
number_of_workers: @number_of_workers,
Expand Down Expand Up @@ -205,6 +218,7 @@ def rbi_for_constant(constant)
next unless compiler_class.handles?(constant)

compiler = compiler_class.new(self, file.root, constant, @compiler_options)
# binding.break
compiler.decorate
rescue
$stderr.puts("Error: `#{compiler_class.name}` failed to generate RBI for `#{constant}`")
Expand Down
10 changes: 10 additions & 0 deletions spec/tapioca/dsl/compilers/active_record_fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ def posts(fixture_name = nil, *other_fixtures); end
assert_equal(expected, rbi_for("ActiveSupport::TestCase"))
end
end

describe "#handles_path?" do
it "matches the fixtures path" do
assert(ActiveRecordFixtures.handles_path?("test/fixtures/posts.yml"))
assert(ActiveRecordFixtures.handles_path?("test/fixtures/posts.yml.erb"))
assert(ActiveRecordFixtures.handles_path?("test/fixtures/more/posts.yml"))
refute(ActiveRecordFixtures.handles_path?("test/fixtures/posts.rb"))
refute(ActiveRecordFixtures.handles_path?("test/elsewhere/posts.yml"))
end
end
end

private
Expand Down
Loading