diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-16 17:31:16 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-16 17:31:16 +0000 |
commit | 10f910df4bf2e3cf923b7823c2873d3b06313699 (patch) | |
tree | 4efa37ee370248da78eb2d63f1b0c9221b4d90f6 /crates | |
parent | 742b3b5744fbca1a5587e2898cd5b74d55853a47 (diff) | |
parent | ca7e9ab0da4f9a781db1857cd11951d296df24a9 (diff) |
Merge #3172
3172: 1-based columns r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_cli/src/analysis_bench.rs | 52 | ||||
-rw-r--r-- | crates/ra_cli/src/main.rs | 34 |
2 files changed, 54 insertions, 32 deletions
diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 764df6b9e..4835a68ce 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | use std::{ | 3 | use 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,30 @@ use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol}; | |||
14 | 15 | ||
15 | use crate::Result; | 16 | use crate::Result; |
16 | 17 | ||
18 | pub(crate) struct Position { | ||
19 | path: PathBuf, | ||
20 | line: u32, | ||
21 | column: u32, | ||
22 | } | ||
23 | |||
24 | impl 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 | |||
33 | fn 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 | |||
17 | pub(crate) enum Op { | 38 | pub(crate) enum Op { |
18 | Highlight { path: PathBuf }, | 39 | Highlight { path: PathBuf }, |
19 | Complete { path: PathBuf, line: u32, column: u32 }, | 40 | Complete(Position), |
41 | GotoDef(Position), | ||
20 | } | 42 | } |
21 | 43 | ||
22 | pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { | 44 | pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { |
@@ -31,7 +53,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { | |||
31 | let file_id = { | 53 | let file_id = { |
32 | let path = match &op { | 54 | let path = match &op { |
33 | Op::Highlight { path } => path, | 55 | Op::Highlight { path } => path, |
34 | Op::Complete { path, .. } => path, | 56 | Op::Complete(pos) | Op::GotoDef(pos) => &pos.path, |
35 | }; | 57 | }; |
36 | let path = std::env::current_dir()?.join(path).canonicalize()?; | 58 | let path = std::env::current_dir()?.join(path).canonicalize()?; |
37 | roots | 59 | roots |
@@ -51,7 +73,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { | |||
51 | .ok_or_else(|| format!("Can't find {:?}", path))? | 73 | .ok_or_else(|| format!("Can't find {:?}", path))? |
52 | }; | 74 | }; |
53 | 75 | ||
54 | match op { | 76 | match &op { |
55 | Op::Highlight { .. } => { | 77 | Op::Highlight { .. } => { |
56 | let res = do_work(&mut host, file_id, |analysis| { | 78 | let res = do_work(&mut host, file_id, |analysis| { |
57 | analysis.diagnostics(file_id).unwrap(); | 79 | analysis.diagnostics(file_id).unwrap(); |
@@ -61,16 +83,30 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { | |||
61 | println!("\n{}", res); | 83 | println!("\n{}", res); |
62 | } | 84 | } |
63 | } | 85 | } |
64 | Op::Complete { line, column, .. } => { | 86 | Op::Complete(pos) | Op::GotoDef(pos) => { |
87 | let is_completion = match op { | ||
88 | Op::Complete(..) => true, | ||
89 | _ => false, | ||
90 | }; | ||
91 | |||
65 | let offset = host | 92 | let offset = host |
66 | .analysis() | 93 | .analysis() |
67 | .file_line_index(file_id)? | 94 | .file_line_index(file_id)? |
68 | .offset(LineCol { line, col_utf16: column }); | 95 | .offset(LineCol { line: pos.line - 1, col_utf16: pos.column }); |
69 | let file_postion = FilePosition { file_id, offset }; | 96 | let file_postion = FilePosition { file_id, offset }; |
70 | 97 | ||
71 | let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion)); | 98 | if is_completion { |
72 | if verbose { | 99 | let res = |
73 | println!("\n{:#?}", res); | 100 | do_work(&mut host, file_id, |analysis| analysis.completions(file_postion)); |
101 | if verbose { | ||
102 | println!("\n{:#?}", res); | ||
103 | } | ||
104 | } else { | ||
105 | let res = | ||
106 | do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_postion)); | ||
107 | if verbose { | ||
108 | println!("\n{:#?}", res); | ||
109 | } | ||
74 | } | 110 | } |
75 | } | 111 | } |
76 | } | 112 | } |
diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 6a0e447b9..750cbab86 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs | |||
@@ -132,25 +132,16 @@ fn main() -> Result<()> { | |||
132 | } | 132 | } |
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: Option<String> = 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 | let goto_def_path: Option<String> = matches.opt_value_from_str("--goto-def")?; |
138 | panic!("either --highlight or --complete must be set, not both") | 138 | let op = match (highlight_path, complete_path, goto_def_path) { |
139 | } | 139 | (Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() }, |
140 | let op = if let Some(path) = highlight_path { | 140 | (None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?), |
141 | let path: String = path; | 141 | (None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?), |
142 | analysis_bench::Op::Highlight { path: path.into() } | 142 | _ => panic!( |
143 | } else if let Some(path_line_col) = complete_path { | 143 | "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" |
144 | let path_line_col: String = path_line_col; | 144 | ), |
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 { | ||
153 | panic!("either --highlight or --complete must be set") | ||
154 | }; | 145 | }; |
155 | matches.finish().or_else(handle_extra_flags)?; | 146 | matches.finish().or_else(handle_extra_flags)?; |
156 | analysis_bench::run(verbose, path.as_ref(), op)?; | 147 | analysis_bench::run(verbose, path.as_ref(), op)?; |
@@ -183,8 +174,3 @@ fn read_stdin() -> Result<String> { | |||
183 | std::io::stdin().read_to_string(&mut buff)?; | 174 | std::io::stdin().read_to_string(&mut buff)?; |
184 | Ok(buff) | 175 | Ok(buff) |
185 | } | 176 | } |
186 | |||
187 | fn 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 | } | ||