4
4
use std:: {
5
5
fs,
6
6
io:: Write as _,
7
+ ops:: Not ,
7
8
process:: { self , Stdio } ,
8
9
} ;
9
10
@@ -14,7 +15,7 @@ use ide::{
14
15
FilePosition , FileRange , HoverAction , HoverGotoTypeData , InlayFieldsToResolve , Query ,
15
16
RangeInfo , ReferenceCategory , Runnable , RunnableKind , SingleResolve , SourceChange , TextEdit ,
16
17
} ;
17
- use ide_db:: SymbolKind ;
18
+ use ide_db:: { FxHashMap , SymbolKind } ;
18
19
use itertools:: Itertools ;
19
20
use lsp_server:: ErrorCode ;
20
21
use lsp_types:: {
@@ -36,6 +37,7 @@ use vfs::{AbsPath, AbsPathBuf, FileId, VfsPath};
36
37
37
38
use crate :: {
38
39
config:: { Config , RustfmtConfig , WorkspaceSymbolConfig } ,
40
+ diagnostics:: convert_diagnostic,
39
41
global_state:: { FetchWorkspaceRequest , GlobalState , GlobalStateSnapshot } ,
40
42
hack_recover_crate_name,
41
43
line_index:: LineEndings ,
@@ -473,6 +475,68 @@ pub(crate) fn handle_on_type_formatting(
473
475
Ok ( Some ( change) )
474
476
}
475
477
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
+
476
540
pub ( crate ) fn handle_document_symbol (
477
541
snap : GlobalStateSnapshot ,
478
542
params : lsp_types:: DocumentSymbolParams ,
0 commit comments