Skip to content

Commit 43d8142

Browse files
bors[bot]matklad
andauthored
Merge #5614
5614: Use split_once polyfill r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 08d18e0 + cd9f863 commit 43d8142

File tree

9 files changed

+334
-352
lines changed

9 files changed

+334
-352
lines changed

crates/ra_project_model/src/cfg_flag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::str::FromStr;
55

66
use ra_cfg::CfgOptions;
7-
use stdx::split_delim;
7+
use stdx::split_once;
88

99
#[derive(Clone, Eq, PartialEq, Debug)]
1010
pub enum CfgFlag {
@@ -15,7 +15,7 @@ pub enum CfgFlag {
1515
impl FromStr for CfgFlag {
1616
type Err = String;
1717
fn from_str(s: &str) -> Result<Self, Self::Err> {
18-
let res = match split_delim(s, '=') {
18+
let res = match split_once(s, '=') {
1919
Some((key, value)) => {
2020
if !(value.starts_with('"') && value.ends_with('"')) {
2121
return Err(format!("Invalid cfg ({:?}), value should be in quotes", s));

crates/rust-analyzer/src/bin/args.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{env, fmt::Write, path::PathBuf};
88
use anyhow::{bail, Result};
99
use pico_args::Arguments;
1010
use ra_ssr::{SsrPattern, SsrRule};
11-
use rust_analyzer::cli::{BenchWhat, Position, Verbosity};
11+
use rust_analyzer::cli::{AnalysisStatsCmd, BenchCmd, BenchWhat, Position, Verbosity};
1212
use vfs::AbsPathBuf;
1313

1414
pub(crate) struct Args {
@@ -24,23 +24,8 @@ pub(crate) enum Command {
2424
Highlight {
2525
rainbow: bool,
2626
},
27-
Stats {
28-
randomize: bool,
29-
parallel: bool,
30-
memory_usage: bool,
31-
only: Option<String>,
32-
with_deps: bool,
33-
path: PathBuf,
34-
load_output_dirs: bool,
35-
with_proc_macro: bool,
36-
},
37-
Bench {
38-
memory_usage: bool,
39-
path: PathBuf,
40-
what: BenchWhat,
41-
load_output_dirs: bool,
42-
with_proc_macro: bool,
43-
},
27+
AnalysisStats(AnalysisStatsCmd),
28+
Bench(BenchCmd),
4429
Diagnostics {
4530
path: PathBuf,
4631
load_output_dirs: bool,
@@ -199,7 +184,7 @@ ARGS:
199184
trailing.pop().unwrap().into()
200185
};
201186

202-
Command::Stats {
187+
Command::AnalysisStats(AnalysisStatsCmd {
203188
randomize,
204189
parallel,
205190
memory_usage,
@@ -208,7 +193,7 @@ ARGS:
208193
path,
209194
load_output_dirs,
210195
with_proc_macro,
211-
}
196+
})
212197
}
213198
"analysis-bench" => {
214199
if matches.contains(["-h", "--help"]) {
@@ -256,7 +241,13 @@ ARGS:
256241
let memory_usage = matches.contains("--memory-usage");
257242
let load_output_dirs = matches.contains("--load-output-dirs");
258243
let with_proc_macro = matches.contains("--with-proc-macro");
259-
Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro }
244+
Command::Bench(BenchCmd {
245+
memory_usage,
246+
path,
247+
what,
248+
load_output_dirs,
249+
with_proc_macro,
250+
})
260251
}
261252
"diagnostics" => {
262253
if matches.contains(["-h", "--help"]) {

crates/rust-analyzer/src/bin/main.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,8 @@ fn main() -> Result<()> {
3333
args::Command::Parse { no_dump } => cli::parse(no_dump)?,
3434
args::Command::Symbols => cli::symbols()?,
3535
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
36-
args::Command::Stats {
37-
randomize,
38-
parallel,
39-
memory_usage,
40-
only,
41-
with_deps,
42-
path,
43-
load_output_dirs,
44-
with_proc_macro,
45-
} => cli::analysis_stats(
46-
args.verbosity,
47-
memory_usage,
48-
path.as_ref(),
49-
only.as_ref().map(String::as_ref),
50-
with_deps,
51-
randomize,
52-
parallel,
53-
load_output_dirs,
54-
with_proc_macro,
55-
)?,
56-
args::Command::Bench { memory_usage, path, what, load_output_dirs, with_proc_macro } => {
57-
cli::analysis_bench(
58-
args.verbosity,
59-
path.as_ref(),
60-
what,
61-
memory_usage,
62-
load_output_dirs,
63-
with_proc_macro,
64-
)?
65-
}
36+
args::Command::AnalysisStats(cmd) => cmd.run(args.verbosity)?,
37+
args::Command::Bench(cmd) => cmd.run(args.verbosity)?,
6638
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
6739
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
6840
}

crates/rust-analyzer/src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use ra_ide::Analysis;
1414
use ra_prof::profile;
1515
use ra_syntax::{AstNode, SourceFile};
1616

17-
pub use analysis_bench::{analysis_bench, BenchWhat, Position};
18-
pub use analysis_stats::analysis_stats;
17+
pub use analysis_bench::{BenchCmd, BenchWhat, Position};
18+
pub use analysis_stats::AnalysisStatsCmd;
1919
pub use diagnostics::diagnostics;
2020
pub use load_cargo::load_cargo;
2121
pub use ssr::{apply_ssr_rules, search_for_patterns};

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Benchmark operations like highlighting or goto definition.
22
3-
use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant};
3+
use std::{env, path::PathBuf, str::FromStr, sync::Arc, time::Instant};
44

5-
use anyhow::{format_err, Result};
5+
use anyhow::{bail, format_err, Result};
66
use ra_db::{
77
salsa::{Database, Durability},
88
FileId,
@@ -15,6 +15,14 @@ use crate::{
1515
print_memory_usage,
1616
};
1717

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+
1826
pub enum BenchWhat {
1927
Highlight { path: AbsPathBuf },
2028
Complete(Position),
@@ -30,85 +38,80 @@ pub struct Position {
3038
impl FromStr for Position {
3139
type Err = anyhow::Error;
3240
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+
}
3850
}
3951
}
4052

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();
4556

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))?
6569
};
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(..));
8270

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 { .. } => {
9173
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()
9376
});
9477
if verbosity.is_verbose() {
95-
println!("\n{:#?}", res);
78+
println!("\n{}", res);
9679
}
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+
}
102105
}
103106
}
104107
}
105-
}
106108

107-
if memory_usage {
108-
print_memory_usage(host, vfs);
109-
}
109+
if self.memory_usage {
110+
print_memory_usage(host, vfs);
111+
}
110112

111-
Ok(())
113+
Ok(())
114+
}
112115
}
113116

114117
fn do_work<F: Fn(&Analysis) -> T, T>(host: &mut AnalysisHost, file_id: FileId, work: F) -> T {

0 commit comments

Comments
 (0)