1+ use crate :: trap;
12use itertools:: Itertools ;
23use ra_ap_base_db:: { EditionedFileId , FileText , RootQueryDb , SourceDatabase } ;
34use ra_ap_hir:: Semantics ;
@@ -23,16 +24,47 @@ use std::rc::Rc;
2324use tracing:: { debug, error, info, trace, warn} ;
2425use triomphe:: Arc ;
2526
27+ #[ derive( Clone , Default ) ]
28+ pub struct RustAnalyzerNoSemantics {
29+ pub severity : trap:: DiagnosticSeverity ,
30+ pub reason : & ' static str ,
31+ }
32+
33+ impl RustAnalyzerNoSemantics {
34+ pub fn warning ( reason : & ' static str ) -> Self {
35+ RustAnalyzerNoSemantics {
36+ severity : trap:: DiagnosticSeverity :: Warning ,
37+ reason,
38+ }
39+ }
40+ pub fn info ( reason : & ' static str ) -> Self {
41+ RustAnalyzerNoSemantics {
42+ severity : trap:: DiagnosticSeverity :: Info ,
43+ reason,
44+ }
45+ }
46+ }
47+
2648pub enum RustAnalyzer < ' a > {
2749 WithSemantics {
2850 vfs : & ' a Vfs ,
2951 semantics : & ' a Semantics < ' a , RootDatabase > ,
3052 } ,
3153 WithoutSemantics {
32- reason : & ' a str ,
54+ severity : trap:: DiagnosticSeverity ,
55+ reason : & ' static str ,
3356 } ,
3457}
3558
59+ impl From < RustAnalyzerNoSemantics > for RustAnalyzer < ' static > {
60+ fn from ( value : RustAnalyzerNoSemantics ) -> Self {
61+ RustAnalyzer :: WithoutSemantics {
62+ severity : value. severity ,
63+ reason : value. reason ,
64+ }
65+ }
66+ }
67+
3668pub struct FileSemanticInformation < ' a > {
3769 pub file_id : EditionedFileId ,
3870 pub semantics : & ' a Semantics < ' a , RootDatabase > ,
@@ -42,7 +74,7 @@ pub struct ParseResult<'a> {
4274 pub ast : SourceFile ,
4375 pub text : Arc < str > ,
4476 pub errors : Vec < SyntaxError > ,
45- pub semantics_info : Result < FileSemanticInformation < ' a > , & ' a str > ,
77+ pub semantics_info : Result < FileSemanticInformation < ' a > , RustAnalyzerNoSemantics > ,
4678}
4779
4880impl < ' a > RustAnalyzer < ' a > {
@@ -52,7 +84,7 @@ impl<'a> RustAnalyzer<'a> {
5284 load_config : & LoadCargoConfig ,
5385 ) -> Option < ( RootDatabase , Vfs ) > {
5486 let progress = |t| trace ! ( "progress: {t}" ) ;
55- let manifest = project. manifest_path ( ) ;
87+ let manifest: & ManifestPath = project. manifest_path ( ) ;
5688 match load_workspace_at ( manifest. as_ref ( ) , config, load_config, & progress) {
5789 Ok ( ( db, vfs, _macro_server) ) => Some ( ( db, vfs) ) ,
5890 Err ( err) => {
@@ -67,16 +99,25 @@ impl<'a> RustAnalyzer<'a> {
6799 fn get_file_data (
68100 & self ,
69101 path : & Path ,
70- ) -> Result < ( & Semantics < ' _ , RootDatabase > , EditionedFileId , FileText ) , & str > {
102+ ) -> Result < ( & Semantics < ' _ , RootDatabase > , EditionedFileId , FileText ) , RustAnalyzerNoSemantics >
103+ {
71104 match self {
72- RustAnalyzer :: WithoutSemantics { reason } => Err ( reason) ,
105+ RustAnalyzer :: WithoutSemantics { severity, reason } => Err ( RustAnalyzerNoSemantics {
106+ severity : * severity,
107+ reason,
108+ } ) ,
73109 RustAnalyzer :: WithSemantics { vfs, semantics } => {
74- let file_id = path_to_file_id ( path, vfs) . ok_or ( "file not found in project" ) ?;
75- let input = std:: panic:: catch_unwind ( || semantics. db . file_text ( file_id) )
76- . or ( Err ( "no text available for the file in the project" ) ) ?;
77- let editioned_file_id = semantics
78- . attach_first_edition ( file_id)
79- . ok_or ( "failed to determine rust edition" ) ?;
110+ let file_id = path_to_file_id ( path, vfs) . ok_or (
111+ RustAnalyzerNoSemantics :: warning ( "file not found in project" ) ,
112+ ) ?;
113+ let input = std:: panic:: catch_unwind ( || semantics. db . file_text ( file_id) ) . or (
114+ Err ( RustAnalyzerNoSemantics :: warning (
115+ "no text available for the file in the project" ,
116+ ) ) ,
117+ ) ?;
118+ let editioned_file_id = semantics. attach_first_edition ( file_id) . ok_or (
119+ RustAnalyzerNoSemantics :: warning ( "failed to determine rust edition" ) ,
120+ ) ?;
80121 Ok ( ( semantics, editioned_file_id, input) )
81122 }
82123 }
0 commit comments