Class: RubyLsp::Tapioca::Addon

Inherits:
Addon
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/tapioca/addon.rb

Instance Method Summary collapse

Constructor Details

#initializeAddon

Returns a new instance of Addon.



23
24
25
26
27
28
29
30
31
# File 'lib/ruby_lsp/tapioca/addon.rb', line 23

def initialize
  super

  @global_state = T.let(nil, T.nilable(RubyLsp::GlobalState))
  @rails_runner_client = T.let(nil, T.nilable(RubyLsp::Rails::RunnerClient))
  @index = T.let(nil, T.nilable(RubyIndexer::Index))
  @file_checksums = T.let({}, T::Hash[String, String])
  @outgoing_queue = T.let(nil, T.nilable(Thread::Queue))
end

Instance Method Details

#activate(global_state, outgoing_queue) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_lsp/tapioca/addon.rb', line 34

def activate(global_state, outgoing_queue)
  @global_state = global_state
  return unless @global_state.enabled_feature?(:tapiocaAddon)

  @index = @global_state.index
  @outgoing_queue = outgoing_queue
  Thread.new do
    # Get a handle to the Rails add-on's runtime client. The call to `rails_runner_client` will block this thread
    # until the server has finished booting, but it will not block the main LSP. This has to happen inside of a
    # thread
    addon = T.cast(::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0.3.17", "< 0.4"), ::RubyLsp::Rails::Addon)
    @rails_runner_client = addon.rails_runner_client
    @outgoing_queue << Notification.window_log_message("Activating Tapioca add-on v#{version}")
    @rails_runner_client.register_server_addon(File.expand_path("server_addon.rb", __dir__))
  rescue IncompatibleApiError
    # The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking
    # changes
    @outgoing_queue << Notification.window_log_message(
      "IncompatibleApiError: Cannot activate Tapioca LSP add-on",
      type: Constant::MessageType::WARNING,
    )
  end
end

#deactivateObject



59
60
# File 'lib/ruby_lsp/tapioca/addon.rb', line 59

def deactivate
end

#nameObject



63
64
65
# File 'lib/ruby_lsp/tapioca/addon.rb', line 63

def name
  "Tapioca"
end

#versionObject



68
69
70
# File 'lib/ruby_lsp/tapioca/addon.rb', line 68

def version
  "0.1.0"
end

#workspace_did_change_watched_files(changes) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_lsp/tapioca/addon.rb', line 73

def workspace_did_change_watched_files(changes)
  return unless T.must(@global_state).enabled_feature?(:tapiocaAddon)
  return unless @rails_runner_client # Client is not ready

  constants = changes.flat_map do |change|
    path = URI(change[:uri]).to_standardized_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.filter_map do |entry|
      entry.name if entry.class == RubyIndexer::Entry::Class || entry.class == RubyIndexer::Entry::Module
    end
  end.compact

  return if constants.empty?

  @rails_runner_client.trigger_reload
  @rails_runner_client.delegate_notification(
    server_addon_name: "Tapioca",
    request_name: "dsl",
    constants: constants,
  )
end