Class: RubyLsp::TypeGuessr::Addon

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

Overview

TypeGuessr addon for Ruby LSP Provides heuristic type inference without requiring type annotations

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#runtime_adapterObject (readonly)

Returns the value of attribute runtime_adapter.



14
15
16
# File 'lib/ruby_lsp/type_guessr/addon.rb', line 14

def runtime_adapter
  @runtime_adapter
end

Instance Method Details

#activate(global_state, message_queue) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_lsp/type_guessr/addon.rb', line 20

def activate(global_state, message_queue)
  unless ::TypeGuessr::Core::Config.enabled?
    message_queue << RubyLsp::Notification.window_log_message(
      "[TypeGuessr] Disabled via config",
      type: RubyLsp::Constant::MessageType::LOG
    )
    return
  end

  @global_state = global_state
  @message_queue = message_queue
  @runtime_adapter = RuntimeAdapter.new(global_state, message_queue)
  @debug_server = nil

  # Extend Ruby LSP's hover targets to include variables and parameters
  extend_hover_targets

  # Start indexing (RBS preload, member_index, project files, gem cache)
  @runtime_adapter.start_indexing

  # Swap TypeInferrer for enhanced Go to Definition
  @runtime_adapter.swap_type_inferrer

  # Start debug server if enabled
  start_debug_server if ::TypeGuessr::Core::Config.debug_server_enabled?

  debug_status = ::TypeGuessr::Core::Config.debug? ? " (debug mode enabled)" : ""
  message_queue << RubyLsp::Notification.window_log_message(
    "[TypeGuessr] Activated#{debug_status}",
    type: RubyLsp::Constant::MessageType::LOG
  )
end

#create_hover_listener(response_builder, node_context, dispatcher) ⇒ Object



58
59
60
61
62
# File 'lib/ruby_lsp/type_guessr/addon.rb', line 58

def create_hover_listener(response_builder, node_context, dispatcher)
  return unless @runtime_adapter

  Hover.new(@runtime_adapter, response_builder, node_context, dispatcher, @global_state)
end

#deactivateObject



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

def deactivate
  @runtime_adapter&.restore_type_inferrer
  @debug_server&.stop
end

#nameObject



16
17
18
# File 'lib/ruby_lsp/type_guessr/addon.rb', line 16

def name
  "TypeGuessr"
end

#workspace_did_change_watched_files(changes) ⇒ Object

Handle file changes



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_lsp/type_guessr/addon.rb', line 65

def workspace_did_change_watched_files(changes)
  return unless @runtime_adapter

  changes.each do |change|
    uri = URI(change[:uri])
    next unless uri.path&.end_with?(".rb")

    case change[:type]
    when RubyLsp::Constant::FileChangeType::CREATED,
         RubyLsp::Constant::FileChangeType::CHANGED
      reindex_file(uri)
    when RubyLsp::Constant::FileChangeType::DELETED
      file_path = uri.to_standardized_path
      @runtime_adapter.remove_indexed_file(file_path) if file_path
    end
  end
end