Class: RubyLsp::GlobalState

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlobalState

Returns a new instance of GlobalState.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_lsp/global_state.rb', line 30

def initialize
  @workspace_uri = T.let(URI::Generic.from_path(path: Dir.pwd), URI::Generic)
  @encoding = T.let(Encoding::UTF_8, Encoding)

  @formatter = T.let("auto", String)
  @linters = T.let([], T::Array[String])
  @test_library = T.let("minitest", String)
  @has_type_checker = T.let(true, T::Boolean)
  @index = T.let(RubyIndexer::Index.new, RubyIndexer::Index)
  @supported_formatters = T.let({}, T::Hash[String, Requests::Support::Formatter])
  @supports_watching_files = T.let(false, T::Boolean)
  @experimental_features = T.let(false, T::Boolean)
  @type_inferrer = T.let(TypeInferrer.new(@index, @experimental_features), TypeInferrer)
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



21
22
23
# File 'lib/ruby_lsp/global_state.rb', line 21

def encoding
  @encoding
end

#experimental_featuresObject (readonly)

Returns the value of attribute experimental_features.



24
25
26
# File 'lib/ruby_lsp/global_state.rb', line 24

def experimental_features
  @experimental_features
end

#formatterObject

Returns the value of attribute formatter.



12
13
14
# File 'lib/ruby_lsp/global_state.rb', line 12

def formatter
  @formatter
end

#has_type_checkerObject (readonly)

Returns the value of attribute has_type_checker.



15
16
17
# File 'lib/ruby_lsp/global_state.rb', line 15

def has_type_checker
  @has_type_checker
end

#indexObject (readonly)

Returns the value of attribute index.



18
19
20
# File 'lib/ruby_lsp/global_state.rb', line 18

def index
  @index
end

#supports_watching_filesObject (readonly)

Returns the value of attribute supports_watching_files.



24
25
26
# File 'lib/ruby_lsp/global_state.rb', line 24

def supports_watching_files
  @supports_watching_files
end

#test_libraryObject (readonly)

Returns the value of attribute test_library.



9
10
11
# File 'lib/ruby_lsp/global_state.rb', line 9

def test_library
  @test_library
end

#type_inferrerObject (readonly)

Returns the value of attribute type_inferrer.



27
28
29
# File 'lib/ruby_lsp/global_state.rb', line 27

def type_inferrer
  @type_inferrer
end

Instance Method Details

#active_formatterObject



51
52
53
# File 'lib/ruby_lsp/global_state.rb', line 51

def active_formatter
  @supported_formatters[@formatter]
end

#active_lintersObject



56
57
58
# File 'lib/ruby_lsp/global_state.rb', line 56

def active_linters
  @linters.filter_map { |name| @supported_formatters[name] }
end

#apply_options(options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_lsp/global_state.rb', line 62

def apply_options(options)
  notifications = []
  direct_dependencies = gather_direct_dependencies
  all_dependencies = gather_direct_and_indirect_dependencies
  workspace_uri = options.dig(:workspaceFolders, 0, :uri)
  @workspace_uri = URI(workspace_uri) if workspace_uri

  specified_formatter = options.dig(:initializationOptions, :formatter)

  if specified_formatter
    @formatter = specified_formatter

    if specified_formatter != "auto"
      notifications << Notification.window_log_message("Using formatter specified by user: #{@formatter}")
    end
  end

  if @formatter == "auto"
    @formatter = detect_formatter(direct_dependencies, all_dependencies)
    notifications << Notification.window_log_message("Auto detected formatter: #{@formatter}")
  end

  specified_linters = options.dig(:initializationOptions, :linters)
  @linters = specified_linters || detect_linters(direct_dependencies, all_dependencies)

  notifications << if specified_linters
    Notification.window_log_message("Using linters specified by user: #{@linters.join(", ")}")
  else
    Notification.window_log_message("Auto detected linters: #{@linters.join(", ")}")
  end

  @test_library = detect_test_library(direct_dependencies)
  notifications << Notification.window_log_message("Detected test library: #{@test_library}")

  @has_type_checker = detect_typechecker(all_dependencies)
  if @has_type_checker
    notifications << Notification.window_log_message(
      "Ruby LSP detected this is a Sorbet project and will defer to the Sorbet LSP for some functionality",
    )
  end

  encodings = options.dig(:capabilities, :general, :positionEncodings)
  @encoding = if !encodings || encodings.empty?
    Encoding::UTF_16LE
  elsif encodings.include?(Constant::PositionEncodingKind::UTF8)
    Encoding::UTF_8
  elsif encodings.include?(Constant::PositionEncodingKind::UTF16)
    Encoding::UTF_16LE
  else
    Encoding::UTF_32
  end

  file_watching_caps = options.dig(:capabilities, :workspace, :didChangeWatchedFiles)
  if file_watching_caps&.dig(:dynamicRegistration) && file_watching_caps&.dig(:relativePatternSupport)
    @supports_watching_files = true
  end

  @experimental_features = options.dig(:initializationOptions, :experimentalFeaturesEnabled) || false
  @type_inferrer.experimental_features = @experimental_features

  notifications
end

#encoding_nameObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/ruby_lsp/global_state.rb', line 131

def encoding_name
  case @encoding
  when Encoding::UTF_8
    Constant::PositionEncodingKind::UTF8
  when Encoding::UTF_16LE
    Constant::PositionEncodingKind::UTF16
  else
    Constant::PositionEncodingKind::UTF32
  end
end

#register_formatter(identifier, instance) ⇒ Object



46
47
48
# File 'lib/ruby_lsp/global_state.rb', line 46

def register_formatter(identifier, instance)
  @supported_formatters[identifier] = instance
end

#workspace_pathObject



126
127
128
# File 'lib/ruby_lsp/global_state.rb', line 126

def workspace_path
  T.must(@workspace_uri.to_standardized_path)
end