1
1
//! Benchmark operations like highlighting or goto definition.
2
2
3
- use std:: { env, path:: Path , str:: FromStr , sync:: Arc , time:: Instant } ;
3
+ use std:: { env, path:: PathBuf , str:: FromStr , sync:: Arc , time:: Instant } ;
4
4
5
- use anyhow:: { format_err, Result } ;
5
+ use anyhow:: { bail , format_err, Result } ;
6
6
use ra_db:: {
7
7
salsa:: { Database , Durability } ,
8
8
FileId ,
@@ -15,6 +15,14 @@ use crate::{
15
15
print_memory_usage,
16
16
} ;
17
17
18
+ pub struct BenchCmd {
19
+ pub path : PathBuf ,
20
+ pub what : BenchWhat ,
21
+ pub memory_usage : bool ,
22
+ pub load_output_dirs : bool ,
23
+ pub with_proc_macro : bool ,
24
+ }
25
+
18
26
pub enum BenchWhat {
19
27
Highlight { path : AbsPathBuf } ,
20
28
Complete ( Position ) ,
@@ -30,85 +38,80 @@ pub struct Position {
30
38
impl FromStr for Position {
31
39
type Err = anyhow:: Error ;
32
40
fn from_str ( s : & str ) -> Result < Self > {
33
- let ( path_line, column) = rsplit_at_char ( s, ':' ) ?;
34
- let ( path, line) = rsplit_at_char ( path_line, ':' ) ?;
35
- let path = env:: current_dir ( ) . unwrap ( ) . join ( path) ;
36
- let path = AbsPathBuf :: assert ( path) ;
37
- Ok ( Position { path, line : line. parse ( ) ?, column : column. parse ( ) ? } )
41
+ let mut split = s. rsplitn ( 3 , ':' ) ;
42
+ match ( split. next ( ) , split. next ( ) , split. next ( ) ) {
43
+ ( Some ( column) , Some ( line) , Some ( path) ) => {
44
+ let path = env:: current_dir ( ) . unwrap ( ) . join ( path) ;
45
+ let path = AbsPathBuf :: assert ( path) ;
46
+ Ok ( Position { path, line : line. parse ( ) ?, column : column. parse ( ) ? } )
47
+ }
48
+ _ => bail ! ( "position should be in file:line:column format: {:?}" , s) ,
49
+ }
38
50
}
39
51
}
40
52
41
- fn rsplit_at_char ( s : & str , c : char ) -> Result < ( & str , & str ) > {
42
- let idx = s. rfind ( c) . ok_or_else ( || format_err ! ( "no `{}` in {}" , c, s) ) ?;
43
- Ok ( ( & s[ ..idx] , & s[ idx + 1 ..] ) )
44
- }
53
+ impl BenchCmd {
54
+ pub fn run ( self , verbosity : Verbosity ) -> Result < ( ) > {
55
+ ra_prof:: init ( ) ;
45
56
46
- pub fn analysis_bench (
47
- verbosity : Verbosity ,
48
- path : & Path ,
49
- what : BenchWhat ,
50
- memory_usage : bool ,
51
- load_output_dirs : bool ,
52
- with_proc_macro : bool ,
53
- ) -> Result < ( ) > {
54
- ra_prof:: init ( ) ;
55
-
56
- let start = Instant :: now ( ) ;
57
- eprint ! ( "loading: " ) ;
58
- let ( mut host, vfs) = load_cargo ( path, load_output_dirs, with_proc_macro) ?;
59
- eprintln ! ( "{:?}\n " , start. elapsed( ) ) ;
60
-
61
- let file_id = {
62
- let path = match & what {
63
- BenchWhat :: Highlight { path } => path,
64
- BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => & pos. path ,
57
+ let start = Instant :: now ( ) ;
58
+ eprint ! ( "loading: " ) ;
59
+ let ( mut host, vfs) = load_cargo ( & self . path , self . load_output_dirs , self . with_proc_macro ) ?;
60
+ eprintln ! ( "{:?}\n " , start. elapsed( ) ) ;
61
+
62
+ let file_id = {
63
+ let path = match & self . what {
64
+ BenchWhat :: Highlight { path } => path,
65
+ BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => & pos. path ,
66
+ } ;
67
+ let path = path. clone ( ) . into ( ) ;
68
+ vfs. file_id ( & path) . ok_or_else ( || format_err ! ( "Can't find {}" , path) ) ?
65
69
} ;
66
- let path = path. clone ( ) . into ( ) ;
67
- vfs. file_id ( & path) . ok_or_else ( || format_err ! ( "Can't find {}" , path) ) ?
68
- } ;
69
-
70
- match & what {
71
- BenchWhat :: Highlight { .. } => {
72
- let res = do_work ( & mut host, file_id, |analysis| {
73
- analysis. diagnostics ( file_id, true ) . unwrap ( ) ;
74
- analysis. highlight_as_html ( file_id, false ) . unwrap ( )
75
- } ) ;
76
- if verbosity. is_verbose ( ) {
77
- println ! ( "\n {}" , res) ;
78
- }
79
- }
80
- BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => {
81
- let is_completion = matches ! ( what, BenchWhat :: Complete ( ..) ) ;
82
70
83
- let offset = host
84
- . analysis ( )
85
- . file_line_index ( file_id) ?
86
- . offset ( LineCol { line : pos. line - 1 , col_utf16 : pos. column } ) ;
87
- let file_position = FilePosition { file_id, offset } ;
88
-
89
- if is_completion {
90
- let options = CompletionConfig :: default ( ) ;
71
+ match & self . what {
72
+ BenchWhat :: Highlight { .. } => {
91
73
let res = do_work ( & mut host, file_id, |analysis| {
92
- analysis. completions ( & options, file_position)
74
+ analysis. diagnostics ( file_id, true ) . unwrap ( ) ;
75
+ analysis. highlight_as_html ( file_id, false ) . unwrap ( )
93
76
} ) ;
94
77
if verbosity. is_verbose ( ) {
95
- println ! ( "\n {:#? }" , res) ;
78
+ println ! ( "\n {}" , res) ;
96
79
}
97
- } else {
98
- let res =
99
- do_work ( & mut host, file_id, |analysis| analysis. goto_definition ( file_position) ) ;
100
- if verbosity. is_verbose ( ) {
101
- println ! ( "\n {:#?}" , res) ;
80
+ }
81
+ BenchWhat :: Complete ( pos) | BenchWhat :: GotoDef ( pos) => {
82
+ let is_completion = matches ! ( self . what, BenchWhat :: Complete ( ..) ) ;
83
+
84
+ let offset = host
85
+ . analysis ( )
86
+ . file_line_index ( file_id) ?
87
+ . offset ( LineCol { line : pos. line - 1 , col_utf16 : pos. column } ) ;
88
+ let file_position = FilePosition { file_id, offset } ;
89
+
90
+ if is_completion {
91
+ let options = CompletionConfig :: default ( ) ;
92
+ let res = do_work ( & mut host, file_id, |analysis| {
93
+ analysis. completions ( & options, file_position)
94
+ } ) ;
95
+ if verbosity. is_verbose ( ) {
96
+ println ! ( "\n {:#?}" , res) ;
97
+ }
98
+ } else {
99
+ let res = do_work ( & mut host, file_id, |analysis| {
100
+ analysis. goto_definition ( file_position)
101
+ } ) ;
102
+ if verbosity. is_verbose ( ) {
103
+ println ! ( "\n {:#?}" , res) ;
104
+ }
102
105
}
103
106
}
104
107
}
105
- }
106
108
107
- if memory_usage {
108
- print_memory_usage ( host, vfs) ;
109
- }
109
+ if self . memory_usage {
110
+ print_memory_usage ( host, vfs) ;
111
+ }
110
112
111
- Ok ( ( ) )
113
+ Ok ( ( ) )
114
+ }
112
115
}
113
116
114
117
fn do_work < F : Fn ( & Analysis ) -> T , T > ( host : & mut AnalysisHost , file_id : FileId , work : F ) -> T {
0 commit comments