-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Initial support for workspace diagnostics #18939
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
108 changes: 108 additions & 0 deletions
108
crates/ty_server/src/server/api/requests/workspace_diagnostic.rs
This file contains hidden or 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,108 @@ | ||
| use lsp_types::request::WorkspaceDiagnosticRequest; | ||
| use lsp_types::{ | ||
| FullDocumentDiagnosticReport, Url, WorkspaceDiagnosticParams, WorkspaceDiagnosticReport, | ||
| WorkspaceDiagnosticReportResult, WorkspaceDocumentDiagnosticReport, | ||
| WorkspaceFullDocumentDiagnosticReport, | ||
| }; | ||
| use rustc_hash::FxHashMap; | ||
| use ty_project::CheckMode; | ||
|
|
||
| use crate::server::Result; | ||
| use crate::server::api::diagnostics::to_lsp_diagnostic; | ||
| use crate::server::api::traits::{ | ||
| BackgroundRequestHandler, RequestHandler, RetriableRequestHandler, | ||
| }; | ||
| use crate::session::WorkspaceSnapshot; | ||
| use crate::session::client::Client; | ||
| use crate::system::file_to_url; | ||
|
|
||
| pub(crate) struct WorkspaceDiagnosticRequestHandler; | ||
|
|
||
| impl RequestHandler for WorkspaceDiagnosticRequestHandler { | ||
| type RequestType = WorkspaceDiagnosticRequest; | ||
| } | ||
|
|
||
| impl BackgroundRequestHandler for WorkspaceDiagnosticRequestHandler { | ||
| fn run( | ||
| snapshot: WorkspaceSnapshot, | ||
| _client: &Client, | ||
| _params: WorkspaceDiagnosticParams, | ||
| ) -> Result<WorkspaceDiagnosticReportResult> { | ||
| let index = snapshot.index(); | ||
|
|
||
| if !index.global_settings().diagnostic_mode().is_workspace() { | ||
| tracing::debug!("Workspace diagnostics is disabled; returning empty report"); | ||
| return Ok(WorkspaceDiagnosticReportResult::Report( | ||
| WorkspaceDiagnosticReport { items: vec![] }, | ||
| )); | ||
| } | ||
|
|
||
| let mut items = Vec::new(); | ||
|
|
||
| for db in snapshot.projects() { | ||
| let diagnostics = db.check_with_mode(CheckMode::AllFiles); | ||
|
|
||
| // Group diagnostics by URL | ||
| let mut diagnostics_by_url: FxHashMap<Url, Vec<_>> = FxHashMap::default(); | ||
|
|
||
| for diagnostic in diagnostics { | ||
| if let Some(span) = diagnostic.primary_span() { | ||
| let file = span.expect_ty_file(); | ||
| let Some(url) = file_to_url(db, file) else { | ||
| tracing::debug!("Failed to convert file to URL at {}", file.path(db)); | ||
| continue; | ||
| }; | ||
| diagnostics_by_url.entry(url).or_default().push(diagnostic); | ||
| } | ||
| } | ||
|
|
||
| items.reserve(diagnostics_by_url.len()); | ||
|
|
||
| // Convert to workspace diagnostic report format | ||
| for (url, file_diagnostics) in diagnostics_by_url { | ||
| let version = index | ||
| .key_from_url(url.clone()) | ||
| .ok() | ||
| .and_then(|key| index.make_document_ref(&key)) | ||
| .map(|doc| i64::from(doc.version())); | ||
|
|
||
| // Convert diagnostics to LSP format | ||
| let lsp_diagnostics = file_diagnostics | ||
| .into_iter() | ||
| .map(|diagnostic| { | ||
| to_lsp_diagnostic(db, &diagnostic, snapshot.position_encoding()) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| items.push(WorkspaceDocumentDiagnosticReport::Full( | ||
| WorkspaceFullDocumentDiagnosticReport { | ||
| uri: url, | ||
| version, | ||
| full_document_diagnostic_report: FullDocumentDiagnosticReport { | ||
| // TODO: We don't implement result ID caching yet | ||
| result_id: None, | ||
| items: lsp_diagnostics, | ||
| }, | ||
| }, | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| Ok(WorkspaceDiagnosticReportResult::Report( | ||
| WorkspaceDiagnosticReport { items }, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| impl RetriableRequestHandler for WorkspaceDiagnosticRequestHandler { | ||
| fn salsa_cancellation_error() -> lsp_server::ResponseError { | ||
| lsp_server::ResponseError { | ||
| code: lsp_server::ErrorCode::ServerCancelled as i32, | ||
| message: "server cancelled the request".to_owned(), | ||
| data: serde_json::to_value(lsp_types::DiagnosticServerCancellationData { | ||
| retrigger_request: true, | ||
| }) | ||
| .ok(), | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,14 +1,21 @@ | ||
| use super::options::DiagnosticMode; | ||
|
|
||
| /// Resolved client settings for a specific document. These settings are meant to be | ||
| /// used directly by the server, and are *not* a 1:1 representation with how the client | ||
| /// sends them. | ||
| #[derive(Clone, Debug)] | ||
| #[cfg_attr(test, derive(PartialEq, Eq))] | ||
| pub(crate) struct ClientSettings { | ||
| pub(super) disable_language_services: bool, | ||
| pub(super) diagnostic_mode: DiagnosticMode, | ||
| } | ||
|
|
||
| impl ClientSettings { | ||
| pub(crate) fn is_language_services_disabled(&self) -> bool { | ||
| self.disable_language_services | ||
| } | ||
|
|
||
| pub(crate) fn diagnostic_mode(&self) -> DiagnosticMode { | ||
| self.diagnostic_mode | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this doesn't need to be done when in workspace diagnostic mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because in workspace mode all diagnostics should be visible regardless of whether a document is open or close in an editor. So, we shouldn't clear the diagnostics when a user closes a file in the editor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhhh makes sense of course.