@@ -10,7 +10,7 @@ use ide_db::{
10
10
} ;
11
11
use syntax:: {
12
12
ast:: { self , edit:: IndentLevel , HasModuleItem , HasName } ,
13
- AstNode , TextRange , TextSize ,
13
+ AstNode , TextRange ,
14
14
} ;
15
15
use text_edit:: TextEdit ;
16
16
@@ -27,14 +27,28 @@ pub(crate) fn unlinked_file(
27
27
) {
28
28
// Limit diagnostic to the first few characters in the file. This matches how VS Code
29
29
// renders it with the full span, but on other editors, and is less invasive.
30
+ let fixes = fixes ( ctx, file_id) ;
31
+ // FIXME: This is a hack for the vscode extension to notice whether there is an autofix or not before having to resolve diagnostics.
32
+ // This is to prevent project linking popups from appearing when there is an autofix. https://github.com/rust-lang/rust-analyzer/issues/14523
33
+ let message = if fixes. is_none ( ) {
34
+ "file not included in crate hierarchy"
35
+ } else {
36
+ "file not included in module tree"
37
+ } ;
38
+
30
39
let range = ctx. sema . db . parse ( file_id) . syntax_node ( ) . text_range ( ) ;
31
- // FIXME: This is wrong if one of the first three characters is not ascii: `//Ы`.
32
- let range = range. intersect ( TextRange :: up_to ( TextSize :: of ( "..." ) ) ) . unwrap_or ( range) ;
40
+ let range = FileLoader :: file_text ( ctx. sema . db , file_id)
41
+ . char_indices ( )
42
+ . take ( 3 )
43
+ . last ( )
44
+ . map ( |( i, _) | i)
45
+ . map ( |i| TextRange :: up_to ( i. try_into ( ) . unwrap ( ) ) )
46
+ . unwrap_or ( range) ;
33
47
34
48
acc. push (
35
- Diagnostic :: new ( "unlinked-file" , "file not included in module tree" , range)
49
+ Diagnostic :: new ( "unlinked-file" , message , range)
36
50
. severity ( Severity :: WeakWarning )
37
- . with_fixes ( fixes ( ctx , file_id ) ) ,
51
+ . with_fixes ( fixes) ,
38
52
) ;
39
53
}
40
54
0 commit comments