-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathutils.rb
More file actions
352 lines (303 loc) · 9.01 KB
/
utils.rb
File metadata and controls
352 lines (303 loc) · 9.01 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# typed: strict
# frozen_string_literal: true
module RubyLsp
# rubocop:disable RubyLsp/UseLanguageServerAliases
Interface = LanguageServer::Protocol::Interface
Constant = LanguageServer::Protocol::Constant
# rubocop:enable RubyLsp/UseLanguageServerAliases
# Used to indicate that a request shouldn't return a response
BUNDLE_PATH = begin
Bundler.bundle_path.to_s
rescue Bundler::GemfileNotFound
nil
end #: String?
GEMFILE_NAME = begin
Bundler.with_original_env { Bundler.default_gemfile.basename.to_s }
rescue Bundler::GemfileNotFound
"Gemfile"
end #: String
GUESSED_TYPES_URL = "https://shopify.github.io/ruby-lsp/#guessed-types"
TEST_PATH_PATTERN = "**/{test,spec,features}/**/{*_test.rb,test_*.rb,*_spec.rb,*.feature}"
class << self
#: (String file_path) -> bool?
def not_in_dependencies?(file_path)
BUNDLE_PATH &&
!file_path.start_with?(
BUNDLE_PATH, #: as !nil
) &&
!file_path.start_with?(RbConfig::CONFIG["rubylibdir"])
end
end
# Request delegation for embedded languages is not yet standardized into the language server specification. Here we
# use this custom error class as a way to return a signal to the client that the request should be delegated to the
# language server for the host language. The support for delegation is custom built on the client side, so each editor
# needs to implement their own until this becomes a part of the spec
class DelegateRequestError < StandardError
# A custom error code that clients can use to handle delegate requests. This is past the range of error codes listed
# by the specification to avoid conflicting with other error types
CODE = -32900
end
class AbstractMethodInvokedError < StandardError; end
BUNDLE_COMPOSE_FAILED_CODE = -33000
# A notification to be sent to the client
# @abstract
class Message
#: String
attr_reader :method
#: Object
attr_reader :params
#: (method: String, params: Object) -> void
def initialize(method:, params:)
@method = method
@params = params
end
# @abstract
#: -> Hash[Symbol, untyped]
def to_hash
raise AbstractMethodInvokedError
end
end
class Notification < Message
class << self
#: (String message, ?type: Integer) -> Notification
def window_show_message(message, type: Constant::MessageType::INFO)
new(
method: "window/showMessage",
params: Interface::ShowMessageParams.new(type: type, message: message),
)
end
#: (String message, ?type: Integer) -> Notification
def window_log_message(message, type: Constant::MessageType::LOG)
new(
method: "window/logMessage",
params: Interface::LogMessageParams.new(type: type, message: message),
)
end
#: (Hash[Symbol, untyped] data) -> Notification
def telemetry(data)
new(
method: "telemetry/event",
params: data,
)
end
#: (String id, String title, ?percentage: Integer?, ?message: String?) -> Notification
def progress_begin(id, title, percentage: nil, message: nil)
new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressBegin.new(
kind: "begin",
title: title,
percentage: percentage,
message: message,
),
),
)
end
#: (String id, ?percentage: Integer?, ?message: String?) -> Notification
def progress_report(id, percentage: nil, message: nil)
new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressReport.new(
kind: "report",
percentage: percentage,
message: message,
),
),
)
end
#: (String id) -> Notification
def progress_end(id)
Notification.new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressEnd.new(kind: "end"),
),
)
end
#: (String uri, Array[Interface::Diagnostic] diagnostics, ?version: Integer?) -> Notification
def publish_diagnostics(uri, diagnostics, version: nil)
new(
method: "textDocument/publishDiagnostics",
params: Interface::PublishDiagnosticsParams.new(uri: uri, diagnostics: diagnostics, version: version),
)
end
end
# @override
#: -> Hash[Symbol, untyped]
def to_hash
hash = { method: @method }
if @params
hash[:params] = @params #: as untyped
.to_hash
end
hash
end
end
class Request < Message
class << self
#: (Integer id, (Interface::RelativePattern | String) pattern, ?kind: Integer, ?registration_id: String?) -> Request
def register_watched_files(
id,
pattern,
kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE,
registration_id: nil
)
new(
id: id,
method: "client/registerCapability",
params: Interface::RegistrationParams.new(
registrations: [
Interface::Registration.new(
id: registration_id || SecureRandom.uuid,
method: "workspace/didChangeWatchedFiles",
register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new(
watchers: [
Interface::FileSystemWatcher.new(glob_pattern: pattern, kind: kind),
],
),
),
],
),
)
end
end
#: (id: (Integer | String), method: String, params: Object) -> void
def initialize(id:, method:, params:)
@id = id
super(method: method, params: params)
end
# @override
#: -> Hash[Symbol, untyped]
def to_hash
hash = { id: @id, method: @method }
if @params
hash[:params] = @params #: as untyped
.to_hash
end
hash
end
end
class Error
#: String
attr_reader :message
#: Integer
attr_reader :code
#: (id: Integer, code: Integer, message: String, ?data: Hash[Symbol, untyped]?) -> void
def initialize(id:, code:, message:, data: nil)
@id = id
@code = code
@message = message
@data = data
end
#: -> Hash[Symbol, untyped]
def to_hash
{
id: @id,
error: {
code: @code,
message: @message,
data: @data,
},
}
end
end
# The final result of running a request before its IO is finalized
class Result
#: untyped
attr_reader :response
#: Integer
attr_reader :id
#: (id: Integer, response: untyped) -> void
def initialize(id:, response:)
@id = id
@response = response
end
#: -> Hash[Symbol, untyped]
def to_hash
{ id: @id, result: @response }
end
end
# A request configuration, to turn on/off features
class RequestConfig
#: (Hash[Symbol, bool] configuration) -> void
def initialize(configuration)
@configuration = configuration
end
#: (Symbol feature) -> bool?
def enabled?(feature)
@configuration[:enableAll] || @configuration[feature]
end
#: (Hash[Symbol, bool]) -> void
def merge!(hash)
@configuration.merge!(hash)
end
end
class SorbetLevel
class << self
#: -> SorbetLevel
def ignore
new("ignore")
end
end
#: (String?) -> void
def initialize(sigil)
@level = case sigil
when "ignore"
:ignore
when "false"
:false
when "true"
:true
when "strict", "strong"
:strict
else
:none
end #: Symbol
end
#: -> bool
def ignore? = @level == :ignore
#: -> bool
def false? = @level == :false
#: -> bool
def true? = @level == :true
#: -> bool
def strict? = @level == :strict
#: -> bool
def none? = @level == :none
#: -> bool
def true_or_higher? = @level == :true || @level == :strict
end
# Reads JSON RPC messages from the given IO in a loop
class MessageReader
#: (IO) -> void
def initialize(io)
@io = io
end
#: () { (Hash[Symbol, untyped]) -> void } -> void
def each_message(&block)
while (headers = @io.gets("\r\n\r\n"))
raw_message = @io.read(headers[/Content-Length: (\d+)/i, 1].to_i) #: as !nil
block.call(JSON.parse(raw_message, symbolize_names: true))
end
end
end
# Writes JSON RPC messages to the given IO
class MessageWriter
#: (IO) -> void
def initialize(io)
@io = io
end
#: (Hash[Symbol, untyped]) -> void
def write(message)
message[:jsonrpc] = "2.0"
json_message = message.to_json
@io.write("Content-Length: #{json_message.bytesize}\r\n\r\n#{json_message}")
@io.flush
end
end
end