Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruff server refreshes diagnostics for open files when file configuration is changed #10988

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/ruff_server/src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ fn local_notification_task<'a, N: traits::SyncNotificationHandler>(
notif: server::Notification,
) -> super::Result<Task<'a>> {
let (id, params) = cast_notification::<N>(notif)?;
Ok(Task::local(move |session, notifier, _, _| {
if let Err(err) = N::run(session, notifier, params) {
Ok(Task::local(move |session, notifier, requester, _| {
if let Err(err) = N::run(session, notifier, requester, params) {
tracing::error!("An error occurred while running {id}: {err}");
show_err_msg!("Ruff encountered a problem. Check the logs for more details.");
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_server/src/server/api/notifications/cancel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -15,6 +15,7 @@ impl super::SyncNotificationHandler for Cancel {
fn run(
_session: &mut Session,
_notifier: Notifier,
_requester: &mut Requester,
_params: types::CancelParams,
) -> Result<()> {
// TODO(jane): Handle this once we have task cancellation in the scheduler.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::server::api::LSPResult;
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -16,6 +16,7 @@ impl super::SyncNotificationHandler for DidChange {
fn run(
session: &mut Session,
_notifier: Notifier,
_requester: &mut Requester,
types::DidChangeTextDocumentParams {
text_document:
types::VersionedTextDocumentIdentifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -14,6 +14,7 @@ impl super::SyncNotificationHandler for DidChangeConfiguration {
fn run(
_session: &mut Session,
_notifier: Notifier,
_requester: &mut Requester,
_params: types::DidChangeConfigurationParams,
) -> Result<()> {
// TODO(jane): get this wired up after the pre-release
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::server::api::LSPResult;
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::schedule::Task;
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -15,13 +16,21 @@ impl super::SyncNotificationHandler for DidChangeWatchedFiles {
fn run(
session: &mut Session,
_notifier: Notifier,
requester: &mut Requester,
params: types::DidChangeWatchedFilesParams,
) -> Result<()> {
for change in params.changes {
for change in &params.changes {
session
.reload_settings(&change.uri)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
}

if session.resolved_client_capabilities().workspace_refresh && !params.changes.is_empty() {
requester
.request::<types::request::WorkspaceDiagnosticRefresh>((), |()| Task::nothing())
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
}
snowsignal marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::server::api::LSPResult;
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -15,6 +15,7 @@ impl super::SyncNotificationHandler for DidChangeWorkspace {
fn run(
session: &mut Session,
_notifier: Notifier,
_requester: &mut Requester,
params: types::DidChangeWorkspaceFoldersParams,
) -> Result<()> {
for new in params.event.added {
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_server/src/server/api/notifications/did_close.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::server::api::LSPResult;
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -16,6 +16,7 @@ impl super::SyncNotificationHandler for DidClose {
fn run(
session: &mut Session,
_notifier: Notifier,
_requester: &mut Requester,
types::DidCloseTextDocumentParams {
text_document: types::TextDocumentIdentifier { uri },
}: types::DidCloseTextDocumentParams,
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_server/src/server/api/notifications/did_open.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::server::client::Notifier;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
use lsp_types as types;
Expand All @@ -15,6 +15,7 @@ impl super::SyncNotificationHandler for DidOpen {
fn run(
session: &mut Session,
_notifier: Notifier,
_requester: &mut Requester,
types::DidOpenTextDocumentParams {
text_document:
types::TextDocumentItem {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/server/api/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub(super) trait SyncNotificationHandler: NotificationHandler {
fn run(
session: &mut Session,
notifier: Notifier,
requester: &mut Requester,
params: <<Self as NotificationHandler>::NotificationType as LSPNotification>::Params,
) -> super::Result<()>;
}
Expand Down
16 changes: 16 additions & 0 deletions crates/ruff_server/src/session/capabilities.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use lsp_types::ClientCapabilities;

#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct ResolvedClientCapabilities {
pub(crate) code_action_deferred_edit_resolution: bool,
pub(crate) apply_edit: bool,
pub(crate) document_changes: bool,
pub(crate) workspace_refresh: bool,
}

impl ResolvedClientCapabilities {
Expand Down Expand Up @@ -33,11 +35,25 @@ impl ResolvedClientCapabilities {
.and_then(|workspace_edit| workspace_edit.document_changes)
.unwrap_or_default();

let workspace_refresh = true;

// TODO(jane): Once the bug involving workspace.diagnostic(s) deserialization has been fixed,
// uncomment this.
/*
let workspace_refresh = client_capabilities
.workspace
.as_ref()
.and_then(|workspace| workspace.diagnostic.as_ref())
.and_then(|diagnostic| diagnostic.refresh_support)
.unwrap_or_default();
*/

Self {
code_action_deferred_edit_resolution: code_action_data_support
&& code_action_edit_resolution,
apply_edit,
document_changes,
workspace_refresh,
}
}
}
Loading