Class: RubyLsp::Rails::Addon

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

Instance Method Summary collapse

Constructor Details

#initializeAddon

Returns a new instance of Addon.



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

def initialize
  super

  # We first initialize the client as a NullClient, so that we can start the server in a background thread. Until
  # the real client is initialized, features that depend on it will not be blocked by using the NullClient
  @client = T.let(NullClient.new, RunnerClient)
end

Instance Method Details

#activate(global_state, message_queue) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 32

def activate(global_state, message_queue)
  @global_state = T.let(global_state, T.nilable(RubyLsp::GlobalState))
  $stderr.puts("Activating Ruby LSP Rails addon v#{VERSION}")
  # Start booting the real client in a background thread. Until this completes, the client will be a NullClient
  Thread.new { @client = RunnerClient.create_client }
  register_additional_file_watchers(global_state: global_state, message_queue: message_queue)
end

#create_code_lens_listener(response_builder, uri, dispatcher) ⇒ Object



53
54
55
56
57
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 53

def create_code_lens_listener(response_builder, uri, dispatcher)
  return unless T.must(@global_state).test_library == "rails"

  CodeLens.new(response_builder, uri, dispatcher)
end

#create_definition_listener(response_builder, uri, node_context, dispatcher) ⇒ Object



88
89
90
91
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 88

def create_definition_listener(response_builder, uri, node_context, dispatcher)
  index = T.must(@global_state).index
  Definition.new(@client, response_builder, node_context, index, dispatcher)
end

#create_document_symbol_listener(response_builder, dispatcher) ⇒ Object



76
77
78
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 76

def create_document_symbol_listener(response_builder, dispatcher)
  DocumentSymbol.new(response_builder, dispatcher)
end

#create_hover_listener(response_builder, node_context, dispatcher) ⇒ Object



66
67
68
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 66

def create_hover_listener(response_builder, node_context, dispatcher)
  Hover.new(@client, response_builder, node_context, T.must(@global_state), dispatcher)
end

#deactivateObject



41
42
43
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 41

def deactivate
  @client.shutdown
end

#nameObject



129
130
131
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 129

def name
  "Ruby LSP Rails"
end

#register_additional_file_watchers(global_state:, message_queue:) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 103

def register_additional_file_watchers(global_state:, message_queue:)
  return unless global_state.supports_watching_files

  message_queue << Request.new(
    id: "ruby-lsp-rails-file-watcher",
    method: "client/registerCapability",
    params: Interface::RegistrationParams.new(
      registrations: [
        Interface::Registration.new(
          id: "workspace/didChangeWatchedFilesRails",
          method: "workspace/didChangeWatchedFiles",
          register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
            watchers: [
              Interface::FileSystemWatcher.new(
                glob_pattern: "**/*structure.sql",
                kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE,
              ),
            ],
          ),
        ),
      ],
    ),
  )
end

#workspace_did_change_watched_files(changes) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/ruby_lsp/ruby_lsp_rails/addon.rb', line 94

def workspace_did_change_watched_files(changes)
  if changes.any? do |change|
       change[:uri].end_with?("db/schema.rb") || change[:uri].end_with?("structure.sql")
     end
    @client.trigger_reload
  end
end