Skip to content

Commit 5007406

Browse files
committed
Make Notification use consistent
1 parent 3f62b8e commit 5007406

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

jekyll/add-ons.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ module RubyLsp
428428
def initialize(message_queue, response_builder, node_context, index, dispatcher)
429429
@message_queue = message_queue
430430

431+
# TODO: update example
431432
@message_queue << Notification.new(
432433
message: "$/progress",
433434
params: Interface::ProgressParams.new(

lib/ruby_lsp/base_server.rb

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def start
7070
document.is_a?(ERBDocument)
7171

7272
send_message(
73+
# TODO: add method in Notification
7374
Notification.new(
7475
method: "delegate/textDocument/virtualState",
7576
params: {

lib/ruby_lsp/server.rb

+9-34
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,9 @@ def load_addons(include_project_addons: true)
168168
errored_addons = Addon.addons.select(&:error?)
169169

170170
if errored_addons.any?
171+
message = "Error loading add-ons:\n\n#{errored_addons.map(&:formatted_errors).join("\n\n")}"
171172
send_message(
172-
Notification.new(
173-
method: "window/showMessage",
174-
params: Interface::ShowMessageParams.new(
175-
type: Constant::MessageType::WARNING,
176-
message: "Error loading add-ons:\n\n#{errored_addons.map(&:formatted_errors).join("\n\n")}",
177-
),
178-
),
173+
Notification.window_show_message(message, type: Constant::MessageType::WARNING),
179174
)
180175

181176
unless @test_mode
@@ -380,13 +375,7 @@ def text_document_did_open(message)
380375
MESSAGE
381376

382377
send_message(
383-
Notification.new(
384-
method: "window/logMessage",
385-
params: Interface::LogMessageParams.new(
386-
type: Constant::MessageType::WARNING,
387-
message: log_message,
388-
),
389-
),
378+
Notification.window_log_message(log_message, type: Constant::MessageType::WARNING),
390379
)
391380
end
392381
end
@@ -400,10 +389,7 @@ def text_document_did_close(message)
400389

401390
# Clear diagnostics for the closed file, so that they no longer appear in the problems tab
402391
send_message(
403-
Notification.new(
404-
method: "textDocument/publishDiagnostics",
405-
params: Interface::PublishDiagnosticsParams.new(uri: uri.to_s, diagnostics: []),
406-
),
392+
Notification.clear_diagnostics(uri),
407393
)
408394
end
409395
end
@@ -1171,27 +1157,16 @@ def process_indexing_configuration(indexing_options)
11711157

11721158
if File.exist?(index_path)
11731159
begin
1174-
@global_state.index.configuration.apply_config(YAML.parse_file(index_path).to_ruby)
1160+
message = "The .index.yml configuration file is deprecated. " \
1161+
"Please use editor settings to configure the index"
11751162
send_message(
1176-
Notification.new(
1177-
method: "window/showMessage",
1178-
params: Interface::ShowMessageParams.new(
1179-
type: Constant::MessageType::WARNING,
1180-
message: "The .index.yml configuration file is deprecated. " \
1181-
"Please use editor settings to configure the index",
1182-
),
1183-
),
1163+
Notification.window_show_message(message, type: Constant::MessageType::WARNING),
11841164
)
1165+
@global_state.index.configuration.apply_config(YAML.parse_file(index_path).to_ruby)
11851166
rescue Psych::SyntaxError => e
11861167
message = "Syntax error while loading configuration: #{e.message}"
11871168
send_message(
1188-
Notification.new(
1189-
method: "window/showMessage",
1190-
params: Interface::ShowMessageParams.new(
1191-
type: Constant::MessageType::WARNING,
1192-
message: message,
1193-
),
1194-
),
1169+
Notification.window_show_message(message, type: Constant::MessageType::WARNING),
11951170
)
11961171
end
11971172
return

lib/ruby_lsp/utils.rb

+21-7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ def window_log_message(message, type: Constant::MessageType::LOG)
8080
)
8181
end
8282

83+
sig { params(uri: String).returns(Notification) }
84+
def clear_diagnostics(uri)
85+
new(
86+
method: "textDocument/publishDiagnostics",
87+
params: Interface::PublishDiagnosticsParams.new(uri: uri.to_s, diagnostics: []),
88+
)
89+
end
90+
91+
def progress(id)
92+
new(
93+
method: "$/progress",
94+
params: Interface::ProgressParams.new(
95+
token: id,
96+
value: Interface::WorkDoneProgressEnd.new(kind: "end"),
97+
),
98+
)
99+
end
100+
83101
sig { params(data: T::Hash[Symbol, T.untyped]).returns(Notification) }
84102
def telemetry(data)
85103
new(
@@ -134,13 +152,7 @@ def progress_report(id, percentage: nil, message: nil)
134152

135153
sig { params(id: String).returns(Notification) }
136154
def progress_end(id)
137-
Notification.new(
138-
method: "$/progress",
139-
params: Interface::ProgressParams.new(
140-
token: id,
141-
value: Interface::WorkDoneProgressEnd.new(kind: "end"),
142-
),
143-
)
155+
Notification.progress(id)
144156
end
145157
end
146158

@@ -161,6 +173,8 @@ def initialize(id:, method:, params:)
161173
super(method: method, params: params)
162174
end
163175

176+
private_class_method :new # we want to force the use of the factory methods
177+
164178
sig { override.returns(T::Hash[Symbol, T.untyped]) }
165179
def to_hash
166180
{ id: @id, method: @method, params: T.unsafe(@params).to_hash }

0 commit comments

Comments
 (0)