-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ruff server
: Important errors are now shown as popups (#10951)
## Summary Fixes #10866. Introduces the `show_err_msg!` macro which will send a message to be shown as a popup to the client via the `window/showMessage` LSP method. ## Test Plan Insert various `show_err_msg!` calls in common code paths (for example, at the beginning of `event_loop`) and confirm that these messages appear in your editor. To test that panicking works correctly, add this to the top of the `fn run` definition in `crates/ruff_server/src/server/api/requests/execute_command.rs`: ```rust panic!("This should appear"); ``` Then, try running a command like `Ruff: Format document` from the command palette (`Ctrl/Cmd+Shift+P`). You should see the following messages appear: ![Screenshot 2024-04-16 at 11 20 57 AM](https://github.com/astral-sh/ruff/assets/19577865/ae430da6-82c3-4841-a419-664ff34034e8)
- Loading branch information
1 parent
eab3c4e
commit 2882604
Showing
6 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ mod edit; | |
mod fix; | ||
mod format; | ||
mod lint; | ||
#[macro_use] | ||
mod message; | ||
mod server; | ||
mod session; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::sync::OnceLock; | ||
|
||
use lsp_types::notification::Notification; | ||
|
||
use crate::server::ClientSender; | ||
|
||
static MESSENGER: OnceLock<ClientSender> = OnceLock::new(); | ||
|
||
pub(crate) fn init_messenger(client_sender: &ClientSender) { | ||
MESSENGER | ||
.set(client_sender.clone()) | ||
.expect("messenger should only be initialized once"); | ||
|
||
// unregister any previously registered panic hook | ||
let _ = std::panic::take_hook(); | ||
|
||
// When we panic, try to notify the client. | ||
std::panic::set_hook(Box::new(move |panic_info| { | ||
if let Some(messenger) = MESSENGER.get() { | ||
let _ = messenger.send(lsp_server::Message::Notification( | ||
lsp_server::Notification { | ||
method: lsp_types::notification::ShowMessage::METHOD.into(), | ||
params: serde_json::to_value(lsp_types::ShowMessageParams { | ||
typ: lsp_types::MessageType::ERROR, | ||
message: String::from( | ||
"The Ruff language server exited with a panic. See the logs for more details." | ||
), | ||
}) | ||
.unwrap_or_default(), | ||
}, | ||
)); | ||
} | ||
|
||
let backtrace = std::backtrace::Backtrace::force_capture(); | ||
tracing::error!("{panic_info}\n{backtrace}"); | ||
})); | ||
} | ||
|
||
pub(crate) fn show_message(message: String, message_type: lsp_types::MessageType) { | ||
MESSENGER | ||
.get() | ||
.expect("messenger should be initialized") | ||
.send(lsp_server::Message::Notification( | ||
lsp_server::Notification { | ||
method: lsp_types::notification::ShowMessage::METHOD.into(), | ||
params: serde_json::to_value(lsp_types::ShowMessageParams { | ||
typ: message_type, | ||
message, | ||
}) | ||
.unwrap(), | ||
}, | ||
)) | ||
.expect("message should send"); | ||
} | ||
|
||
/// Sends an error to the client with a formatted message. The error is sent in a | ||
/// `window/showMessage` notification. | ||
macro_rules! show_err_msg { | ||
($msg:expr$(, $($arg:tt),*)?) => { | ||
crate::message::show_message(::core::format_args!($msg, $($($arg),*)?).to_string(), lsp_types::MessageType::ERROR) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters