Skip to content

Commit

Permalink
Extract progress notifications to helpers (#2816)
Browse files Browse the repository at this point in the history
### Motivation

This PR extracts progress related notifications to helpers, to make it easier for add-ons (and the LSP itself) use progress more often.

### Implementation

Essentially just make some factory methods like we do for other notifications.

### Automated Tests

Existing tests cover it.
  • Loading branch information
vinistock authored Nov 1, 2024
1 parent 93f9669 commit fc6f542
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
37 changes: 3 additions & 34 deletions lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1111,52 +1111,21 @@ def begin_progress(id, title, percentage: 0)
params: Interface::WorkDoneProgressCreateParams.new(token: id),
))

send_message(Notification.new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressBegin.new(
kind: "begin",
title: title,
percentage: percentage,
message: "#{percentage}% completed",
),
),
))
send_message(Notification.progress_begin(id, title, percentage: percentage, message: "#{percentage}% completed"))
end

sig { params(id: String, percentage: Integer).void }
def progress(id, percentage)
return unless @global_state.client_capabilities.supports_progress

send_message(
Notification.new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressReport.new(
kind: "report",
percentage: percentage,
message: "#{percentage}% completed",
),
),
),
)
send_message(Notification.progress_report(id, percentage: percentage, message: "#{percentage}% completed"))
end

sig { params(id: String).void }
def end_progress(id)
return unless @global_state.client_capabilities.supports_progress

send_message(
Notification.new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressEnd.new(kind: "end"),
),
),
)
send_message(Notification.progress_end(id))
rescue ClosedQueueError
# If the server was killed and the message queue is already closed, there's no way to end the progress
# notification
Expand Down
55 changes: 55 additions & 0 deletions lib/ruby_lsp/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,61 @@ def telemetry(data)
params: data,
)
end

sig do
params(
id: String,
title: String,
percentage: T.nilable(Integer),
message: T.nilable(String),
).returns(Notification)
end
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

sig do
params(
id: String,
percentage: T.nilable(Integer),
message: T.nilable(String),
).returns(Notification)
end
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

sig { params(id: String).returns(Notification) }
def progress_end(id)
Notification.new(
method: "$/progress",
params: Interface::ProgressParams.new(
token: id,
value: Interface::WorkDoneProgressEnd.new(kind: "end"),
),
)
end
end

extend T::Sig
Expand Down

0 comments on commit fc6f542

Please sign in to comment.