ruff/crates/ruff_server/src/server/api/diagnostics.rs
Dhruv Manilawala 72b6c26101 Simplify LinterResult, avoid cloning ParseError (#11903)
## Summary

Follow-up to #11902

This PR simplifies the `LinterResult` struct by avoiding the generic and
not store the `ParseError`.

This is possible because the callers already have access to the
`ParseError` via the `Parsed` output. This also means that we can
simplify the return type of `check_path` and avoid the generic `T` on
`LinterResult`.

## Test Plan

`cargo insta test`
2024-06-27 13:44:11 +02:00

52 lines
1.5 KiB
Rust

use crate::{
lint::DiagnosticsMap,
server::client::Notifier,
session::{DocumentQuery, DocumentSnapshot},
};
use super::LSPResult;
pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> DiagnosticsMap {
if snapshot.client_settings().lint() {
let document = snapshot.query();
crate::lint::check(document, snapshot.encoding())
} else {
DiagnosticsMap::default()
}
}
pub(super) fn publish_diagnostics_for_document(
snapshot: &DocumentSnapshot,
notifier: &Notifier,
) -> crate::server::Result<()> {
for (uri, diagnostics) in generate_diagnostics(snapshot) {
notifier
.notify::<lsp_types::notification::PublishDiagnostics>(
lsp_types::PublishDiagnosticsParams {
uri,
diagnostics,
version: Some(snapshot.query().version()),
},
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
}
Ok(())
}
pub(super) fn clear_diagnostics_for_document(
query: &DocumentQuery,
notifier: &Notifier,
) -> crate::server::Result<()> {
notifier
.notify::<lsp_types::notification::PublishDiagnostics>(
lsp_types::PublishDiagnosticsParams {
uri: query.make_key().into_url(),
diagnostics: vec![],
version: Some(query.version()),
},
)
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
Ok(())
}