Skip to content

Commit 0c0731c

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

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ 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+
8391
sig { params(data: T::Hash[Symbol, T.untyped]).returns(Notification) }
8492
def telemetry(data)
8593
new(
@@ -161,6 +169,8 @@ def initialize(id:, method:, params:)
161169
super(method: method, params: params)
162170
end
163171

172+
private_class_method :new # we want to force the use of the factory methods
173+
164174
sig { override.returns(T::Hash[Symbol, T.untyped]) }
165175
def to_hash
166176
{ id: @id, method: @method, params: T.unsafe(@params).to_hash }

0 commit comments

Comments
 (0)