Class: RubyLsp::Ree::Addon

Inherits:
Addon
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_ree/addon.rb

Instance Method Summary collapse

Instance Method Details

#activate(global_state, message_queue) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 16

def activate(global_state, message_queue)
  @global_state = global_state
  @settings = global_state.settings_for_addon(name) || {}

  @message_queue = message_queue
  @template_applicator = RubyLsp::Ree::ReeTemplateApplicator.new
  
  global_state.register_formatter("ree_formatter", RubyLsp::Ree::ReeFormatter.new(
    @message_queue, 
    @settings[:formatter], 
    @global_state.index
  ))

  register_additional_file_watchers(global_state, message_queue)
end

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



44
45
46
47
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 44

def create_completion_listener(response_builder, node_context, dispatcher, uri)
  index = @global_state.index
  RubyLsp::Ree::CompletionListener.new(response_builder, node_context, index, dispatcher, uri)
end

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



39
40
41
42
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 39

def create_definition_listener(response_builder, uri, node_context, dispatcher)
  index = @global_state.index
  RubyLsp::Ree::DefinitionListener.new(response_builder, node_context, index, dispatcher, uri)
end

#create_hover_listener(response_builder, node_context, dispatcher) ⇒ Object



49
50
51
52
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 49

def create_hover_listener(response_builder, node_context, dispatcher)
  index = @global_state.index
  RubyLsp::Ree::HoverListener.new(response_builder, node_context, index, dispatcher)
end

#deactivateObject



32
33
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 32

def deactivate
end

#nameObject



35
36
37
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 35

def name
  "Ree Addon"
end

#register_additional_file_watchers(global_state, message_queue) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 54

def register_additional_file_watchers(global_state, message_queue)
  # Clients are not required to implement this capability
  return unless global_state.supports_watching_files
  
  message_queue << Request.new(
    id: "ruby-lsp-ree-file-create-watcher",
    method: "client/registerCapability",
    params: Interface::RegistrationParams.new(
      registrations: [
        Interface::Registration.new(
          id: "workspace/didCreateWatchedFilesRee",
          method: "workspace/didChangeWatchedFiles",
          register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
            watchers: [
              Interface::FileSystemWatcher.new(
                glob_pattern: "**/*.rb",
                kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE,
              ),
            ],
          ),
        ),
      ],
    ),
  )
end

#workspace_did_change_watched_files(changes) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_lsp/ruby_lsp_ree/addon.rb', line 80

def workspace_did_change_watched_files(changes)
  if changes.size == 1 && changes[0][:type] == Constant::FileChangeType::CREATED
    $stderr.puts("file created #{changes[0][:uri]}")

    return unless @template_applicator.template_dir_exists?

    @template_applicator.apply(changes[0])
  elsif changes.size == 2 && changes.any?{ _1[:type] == Constant::FileChangeType::CREATED } && changes.any?{ _1[:type] == Constant::FileChangeType::DELETED }
    $stderr.puts("file renamed #{changes[0][:uri]} #{changes[1][:uri]}")
  
    RubyLsp::Ree::ReeRenameHandler.call(changes)
  end
end