-
Notifications
You must be signed in to change notification settings - Fork 172
/
global_state.rb
247 lines (201 loc) · 8.72 KB
/
global_state.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# typed: strict
# frozen_string_literal: true
module RubyLsp
class GlobalState
extend T::Sig
sig { returns(String) }
attr_reader :test_library
sig { returns(String) }
attr_accessor :formatter
sig { returns(T::Boolean) }
attr_reader :has_type_checker
sig { returns(RubyIndexer::Index) }
attr_reader :index
sig { returns(Encoding) }
attr_reader :encoding
sig { returns(T::Boolean) }
attr_reader :supports_watching_files, :experimental_features, :supports_request_delegation
sig { returns(TypeInferrer) }
attr_reader :type_inferrer
sig { void }
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)
@addon_settings = T.let({}, T::Hash[String, T.untyped])
@supports_request_delegation = T.let(false, T::Boolean)
end
sig { params(addon_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
def settings_for_addon(addon_name)
@addon_settings[addon_name]
end
sig { params(identifier: String, instance: Requests::Support::Formatter).void }
def register_formatter(identifier, instance)
@supported_formatters[identifier] = instance
end
sig { returns(T.nilable(Requests::Support::Formatter)) }
def active_formatter
@supported_formatters[@formatter]
end
sig { returns(T::Array[Requests::Support::Formatter]) }
def active_linters
@linters.filter_map { |name| @supported_formatters[name] }
end
# Applies the options provided by the editor and returns an array of notifications to send back to the client
sig { params(options: T::Hash[Symbol, T.untyped]).returns(T::Array[Notification]) }
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
addon_settings = options.dig(:initializationOptions, :addonSettings)
if addon_settings
addon_settings.transform_keys!(&:to_s)
@addon_settings.merge!(addon_settings)
end
@supports_request_delegation = options.dig(:capabilities, :experimental, :requestDelegation) || false
notifications
end
sig { returns(String) }
def workspace_path
T.must(@workspace_uri.to_standardized_path)
end
sig { returns(String) }
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
private
sig { params(direct_dependencies: T::Array[String], all_dependencies: T::Array[String]).returns(String) }
def detect_formatter(direct_dependencies, all_dependencies)
# NOTE: Intentionally no $ at end, since we want to match rubocop-shopify, etc.
return "rubocop" if direct_dependencies.any?(/^rubocop/)
syntax_tree_is_direct_dependency = direct_dependencies.include?("syntax_tree")
return "syntax_tree" if syntax_tree_is_direct_dependency
rubocop_is_transitive_dependency = all_dependencies.include?("rubocop")
return "rubocop" if dot_rubocop_yml_present && rubocop_is_transitive_dependency
"none"
end
# Try to detect if there are linters in the project's dependencies. For auto-detection, we always only consider a
# single linter. To have multiple linters running, the user must configure them manually
sig { params(dependencies: T::Array[String], all_dependencies: T::Array[String]).returns(T::Array[String]) }
def detect_linters(dependencies, all_dependencies)
linters = []
if dependencies.any?(/^rubocop/) || (all_dependencies.include?("rubocop") && dot_rubocop_yml_present)
linters << "rubocop"
end
linters
end
sig { params(dependencies: T::Array[String]).returns(String) }
def detect_test_library(dependencies)
if dependencies.any?(/^rspec/)
"rspec"
# A Rails app may have a dependency on minitest, but we would instead want to use the Rails test runner provided
# by ruby-lsp-rails. A Rails app doesn't need to depend on the rails gem itself, individual components like
# activestorage may be added to the gemfile so that other components aren't downloaded. Check for the presence
# of bin/rails to support these cases.
elsif bin_rails_present
"rails"
# NOTE: Intentionally ends with $ to avoid mis-matching minitest-reporters, etc. in a Rails app.
elsif dependencies.any?(/^minitest$/)
"minitest"
elsif dependencies.any?(/^test-unit/)
"test-unit"
else
"unknown"
end
end
sig { params(dependencies: T::Array[String]).returns(T::Boolean) }
def detect_typechecker(dependencies)
return false if ENV["RUBY_LSP_BYPASS_TYPECHECKER"]
dependencies.any?(/^sorbet-static/)
rescue Bundler::GemfileNotFound
false
end
sig { returns(T::Boolean) }
def bin_rails_present
File.exist?(File.join(workspace_path, "bin/rails"))
end
sig { returns(T::Boolean) }
def dot_rubocop_yml_present
File.exist?(File.join(workspace_path, ".rubocop.yml"))
end
sig { returns(T::Array[String]) }
def gather_direct_dependencies
Bundler.with_original_env { Bundler.default_gemfile }
Bundler.locked_gems.dependencies.keys + gemspec_dependencies
rescue Bundler::GemfileNotFound
[]
end
sig { returns(T::Array[String]) }
def gemspec_dependencies
Bundler.locked_gems.sources
.grep(Bundler::Source::Gemspec)
.flat_map { _1.gemspec&.dependencies&.map(&:name) }
end
sig { returns(T::Array[String]) }
def gather_direct_and_indirect_dependencies
Bundler.with_original_env { Bundler.default_gemfile }
Bundler.locked_gems.specs.map(&:name)
rescue Bundler::GemfileNotFound
[]
end
end
end