44use std:: {
55 fs,
66 io:: Write as _,
7+ ops:: Not ,
78 process:: { self , Stdio } ,
89} ;
910
@@ -14,7 +15,7 @@ use ide::{
1415 FilePosition , FileRange , HoverAction , HoverGotoTypeData , InlayFieldsToResolve , Query ,
1516 RangeInfo , ReferenceCategory , Runnable , RunnableKind , SingleResolve , SourceChange , TextEdit ,
1617} ;
17- use ide_db:: SymbolKind ;
18+ use ide_db:: { FxHashMap , SymbolKind } ;
1819use itertools:: Itertools ;
1920use lsp_server:: ErrorCode ;
2021use lsp_types:: {
@@ -36,6 +37,7 @@ use vfs::{AbsPath, AbsPathBuf, FileId, VfsPath};
3637
3738use crate :: {
3839 config:: { Config , RustfmtConfig , WorkspaceSymbolConfig } ,
40+ diagnostics:: convert_diagnostic,
3941 global_state:: { FetchWorkspaceRequest , GlobalState , GlobalStateSnapshot } ,
4042 hack_recover_crate_name,
4143 line_index:: LineEndings ,
@@ -473,6 +475,68 @@ pub(crate) fn handle_on_type_formatting(
473475 Ok ( Some ( change) )
474476}
475477
478+ pub ( crate ) fn handle_document_diagnostics (
479+ snap : GlobalStateSnapshot ,
480+ params : lsp_types:: DocumentDiagnosticParams ,
481+ ) -> anyhow:: Result < lsp_types:: DocumentDiagnosticReportResult > {
482+ let file_id = from_proto:: file_id ( & snap, & params. text_document . uri ) ?;
483+ let source_root = snap. analysis . source_root_id ( file_id) ?;
484+ let line_index = snap. file_line_index ( file_id) ?;
485+ let config = snap. config . diagnostics ( Some ( source_root) ) ;
486+ if !config. enabled {
487+ return Ok ( lsp_types:: DocumentDiagnosticReportResult :: Report (
488+ lsp_types:: DocumentDiagnosticReport :: Full (
489+ lsp_types:: RelatedFullDocumentDiagnosticReport {
490+ related_documents : None ,
491+ full_document_diagnostic_report : lsp_types:: FullDocumentDiagnosticReport {
492+ result_id : None ,
493+ items : vec ! [ ] ,
494+ } ,
495+ } ,
496+ ) ,
497+ ) ) ;
498+ }
499+ let supports_related = snap. config . text_document_diagnostic_related_document_support ( ) ;
500+
501+ let mut related_documents = FxHashMap :: default ( ) ;
502+ let diagnostics = snap
503+ . analysis
504+ . full_diagnostics ( & config, AssistResolveStrategy :: None , file_id) ?
505+ . into_iter ( )
506+ . filter_map ( |d| {
507+ let file = d. range . file_id ;
508+ let diagnostic = convert_diagnostic ( & line_index, d) ;
509+ if file == file_id {
510+ return Some ( diagnostic) ;
511+ }
512+ if supports_related {
513+ related_documents. entry ( file) . or_insert_with ( Vec :: new) . push ( diagnostic) ;
514+ }
515+ None
516+ } ) ;
517+ Ok ( lsp_types:: DocumentDiagnosticReportResult :: Report (
518+ lsp_types:: DocumentDiagnosticReport :: Full ( lsp_types:: RelatedFullDocumentDiagnosticReport {
519+ full_document_diagnostic_report : lsp_types:: FullDocumentDiagnosticReport {
520+ result_id : None ,
521+ items : diagnostics. collect ( ) ,
522+ } ,
523+ related_documents : related_documents. is_empty ( ) . not ( ) . then ( || {
524+ related_documents
525+ . into_iter ( )
526+ . map ( |( id, items) | {
527+ (
528+ to_proto:: url ( & snap, id) ,
529+ lsp_types:: DocumentDiagnosticReportKind :: Full (
530+ lsp_types:: FullDocumentDiagnosticReport { result_id : None , items } ,
531+ ) ,
532+ )
533+ } )
534+ . collect ( )
535+ } ) ,
536+ } ) ,
537+ ) )
538+ }
539+
476540pub ( crate ) fn handle_document_symbol (
477541 snap : GlobalStateSnapshot ,
478542 params : lsp_types:: DocumentSymbolParams ,
0 commit comments