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: Support publish diagnostics as a fallback when pull diagnostics aren't supported #11092

Merged
merged 2 commits into from
Apr 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ruff_server/src/server/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{server::schedule::Task, session::Session};
use lsp_server as server;

mod diagnostics;
mod notifications;
mod requests;
mod traits;
Expand Down
34 changes: 34 additions & 0 deletions crates/ruff_server/src/server/api/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{server::client::Notifier, session::DocumentSnapshot};

use super::LSPResult;

pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> Vec<lsp_types::Diagnostic> {
if snapshot.client_settings().lint() {
crate::lint::check(
snapshot.document(),
&snapshot.settings().linter,
snapshot.encoding(),
)
} else {
vec![]
}
}

pub(super) fn publish_diagnostics_for_document(
snapshot: &DocumentSnapshot,
notifier: &Notifier,
) -> crate::server::Result<()> {
let diagnostics = generate_diagnostics(snapshot);

notifier
.notify::<lsp_types::notification::PublishDiagnostics>(
lsp_types::PublishDiagnosticsParams {
uri: snapshot.url().clone(),
diagnostics,
version: Some(snapshot.document().version()),
},
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::server::api::diagnostics::publish_diagnostics_for_document;
use crate::server::api::LSPResult;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
Expand All @@ -15,7 +16,7 @@ impl super::SyncNotificationHandler for DidChange {
#[tracing::instrument(skip_all, fields(file=%uri))]
fn run(
session: &mut Session,
_notifier: Notifier,
notifier: Notifier,
_requester: &mut Requester,
types::DidChangeTextDocumentParams {
text_document:
Expand All @@ -40,6 +41,12 @@ impl super::SyncNotificationHandler for DidChange {
.make_mut()
.apply_changes(content_changes, new_version, encoding);

// Publish diagnostics if the client doesnt support pull diagnostics
if !session.resolved_client_capabilities().pull_diagnostics {
let snapshot = session.take_snapshot(&uri).unwrap();
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
publish_diagnostics_for_document(&snapshot, &notifier)?;
}

Ok(())
}
}
16 changes: 15 additions & 1 deletion crates/ruff_server/src/server/api/notifications/did_open.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::server::api::diagnostics::publish_diagnostics_for_document;
use crate::server::api::LSPResult;
use crate::server::client::{Notifier, Requester};
use crate::server::Result;
use crate::session::Session;
Expand All @@ -14,7 +16,7 @@ impl super::SyncNotificationHandler for DidOpen {
#[tracing::instrument(skip_all, fields(file=%url))]
fn run(
session: &mut Session,
_notifier: Notifier,
notifier: Notifier,
_requester: &mut Requester,
types::DidOpenTextDocumentParams {
text_document:
Expand All @@ -27,6 +29,18 @@ impl super::SyncNotificationHandler for DidOpen {
}: types::DidOpenTextDocumentParams,
) -> Result<()> {
session.open_document(url, text, version);

// Publish diagnostics if the client doesnt support pull diagnostics
if !session.resolved_client_capabilities().pull_diagnostics {
let snapshot = session
.take_snapshot(url)
.ok_or_else(|| {
anyhow::anyhow!("Unable to take snapshot for document with URL {url}")
})
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
publish_diagnostics_for_document(&snapshot, &notifier)?;
}

Ok(())
}
}
13 changes: 2 additions & 11 deletions crates/ruff_server/src/server/api/requests/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::server::api::diagnostics::generate_diagnostics;
use crate::server::{client::Notifier, Result};
use crate::session::DocumentSnapshot;
use lsp_types::{self as types, request as req};
Expand All @@ -19,23 +20,13 @@ impl super::BackgroundDocumentRequestHandler for DocumentDiagnostic {
_notifier: Notifier,
_params: types::DocumentDiagnosticParams,
) -> Result<DocumentDiagnosticReportResult> {
let diagnostics = if snapshot.client_settings().lint() {
crate::lint::check(
snapshot.document(),
&snapshot.settings().linter,
snapshot.encoding(),
)
} else {
vec![]
};

Ok(DocumentDiagnosticReportResult::Report(
types::DocumentDiagnosticReport::Full(RelatedFullDocumentDiagnosticReport {
related_documents: None,
full_document_diagnostic_report: FullDocumentDiagnosticReport {
// TODO(jane): eventually this will be important for caching diagnostic information.
result_id: None,
items: diagnostics,
items: generate_diagnostics(&snapshot),
},
}),
))
Expand Down
8 changes: 8 additions & 0 deletions crates/ruff_server/src/session/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) struct ResolvedClientCapabilities {
pub(crate) apply_edit: bool,
pub(crate) document_changes: bool,
pub(crate) workspace_refresh: bool,
pub(crate) pull_diagnostics: bool,
}

impl ResolvedClientCapabilities {
Expand Down Expand Up @@ -48,12 +49,19 @@ impl ResolvedClientCapabilities {
.unwrap_or_default();
*/

let pull_diagnostics = client_capabilities
.text_document
.as_ref()
.and_then(|text_document| text_document.diagnostic.as_ref())
.is_some();

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