aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_cli
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-16 17:17:35 +0000
committerAleksey Kladov <[email protected]>2020-02-16 17:17:35 +0000
commit2ba918775cba8d0b1166f7a14a8e114c2fd73f0d (patch)
tree52ba49bbe54a98b40223ed212dc7f40f44296771 /crates/ra_cli
parent98cc51580d0b8a6662f0155d8a45a8cfff469d72 (diff)
Refactor position parsing
Diffstat (limited to 'crates/ra_cli')
-rw-r--r--crates/ra_cli/src/analysis_bench.rs29
-rw-r--r--crates/ra_cli/src/main.rs18
2 files changed, 28 insertions, 19 deletions
diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs
index 764df6b9e..1882a9342 100644
--- a/crates/ra_cli/src/analysis_bench.rs
+++ b/crates/ra_cli/src/analysis_bench.rs
@@ -2,6 +2,7 @@
2 2
3use std::{ 3use std::{
4 path::{Path, PathBuf}, 4 path::{Path, PathBuf},
5 str::FromStr,
5 sync::Arc, 6 sync::Arc,
6 time::Instant, 7 time::Instant,
7}; 8};
@@ -14,9 +15,29 @@ use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol};
14 15
15use crate::Result; 16use crate::Result;
16 17
18pub(crate) struct Position {
19 path: PathBuf,
20 line: u32,
21 column: u32,
22}
23
24impl FromStr for Position {
25 type Err = Box<dyn std::error::Error + Send + Sync>;
26 fn from_str(s: &str) -> Result<Self> {
27 let (path_line, column) = rsplit_at_char(s, ':')?;
28 let (path, line) = rsplit_at_char(path_line, ':')?;
29 Ok(Position { path: path.into(), line: line.parse()?, column: column.parse()? })
30 }
31}
32
33fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> {
34 let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?;
35 Ok((&s[..idx], &s[idx + 1..]))
36}
37
17pub(crate) enum Op { 38pub(crate) enum Op {
18 Highlight { path: PathBuf }, 39 Highlight { path: PathBuf },
19 Complete { path: PathBuf, line: u32, column: u32 }, 40 Complete(Position),
20} 41}
21 42
22pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { 43pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
@@ -31,7 +52,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
31 let file_id = { 52 let file_id = {
32 let path = match &op { 53 let path = match &op {
33 Op::Highlight { path } => path, 54 Op::Highlight { path } => path,
34 Op::Complete { path, .. } => path, 55 Op::Complete(pos) => &pos.path,
35 }; 56 };
36 let path = std::env::current_dir()?.join(path).canonicalize()?; 57 let path = std::env::current_dir()?.join(path).canonicalize()?;
37 roots 58 roots
@@ -61,11 +82,11 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
61 println!("\n{}", res); 82 println!("\n{}", res);
62 } 83 }
63 } 84 }
64 Op::Complete { line, column, .. } => { 85 Op::Complete(pos) => {
65 let offset = host 86 let offset = host
66 .analysis() 87 .analysis()
67 .file_line_index(file_id)? 88 .file_line_index(file_id)?
68 .offset(LineCol { line, col_utf16: column }); 89 .offset(LineCol { line: pos.line, col_utf16: pos.column });
69 let file_postion = FilePosition { file_id, offset }; 90 let file_postion = FilePosition { file_id, offset };
70 91
71 let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion)); 92 let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion));
diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs
index 6a0e447b9..749317475 100644
--- a/crates/ra_cli/src/main.rs
+++ b/crates/ra_cli/src/main.rs
@@ -133,22 +133,15 @@ fn main() -> Result<()> {
133 let verbose = matches.contains(["-v", "--verbose"]); 133 let verbose = matches.contains(["-v", "--verbose"]);
134 let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default(); 134 let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default();
135 let highlight_path = matches.opt_value_from_str("--highlight")?; 135 let highlight_path = matches.opt_value_from_str("--highlight")?;
136 let complete_path = matches.opt_value_from_str("--complete")?; 136 let complete_path: Option<String> = matches.opt_value_from_str("--complete")?;
137 if highlight_path.is_some() && complete_path.is_some() { 137 if highlight_path.is_some() && complete_path.is_some() {
138 panic!("either --highlight or --complete must be set, not both") 138 panic!("either --highlight or --complete must be set, not both")
139 } 139 }
140 let op = if let Some(path) = highlight_path { 140 let op = if let Some(path) = highlight_path {
141 let path: String = path; 141 let path: String = path;
142 analysis_bench::Op::Highlight { path: path.into() } 142 analysis_bench::Op::Highlight { path: path.into() }
143 } else if let Some(path_line_col) = complete_path { 143 } else if let Some(position) = complete_path {
144 let path_line_col: String = path_line_col; 144 analysis_bench::Op::Complete(position.parse()?)
145 let (path_line, column) = rsplit_at_char(path_line_col.as_str(), ':')?;
146 let (path, line) = rsplit_at_char(path_line, ':')?;
147 analysis_bench::Op::Complete {
148 path: path.into(),
149 line: line.parse()?,
150 column: column.parse()?,
151 }
152 } else { 145 } else {
153 panic!("either --highlight or --complete must be set") 146 panic!("either --highlight or --complete must be set")
154 }; 147 };
@@ -183,8 +176,3 @@ fn read_stdin() -> Result<String> {
183 std::io::stdin().read_to_string(&mut buff)?; 176 std::io::stdin().read_to_string(&mut buff)?;
184 Ok(buff) 177 Ok(buff)
185} 178}
186
187fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> {
188 let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?;
189 Ok((&s[..idx], &s[idx + 1..]))
190}