diff options
author | Aleksey Kladov <[email protected]> | 2020-02-16 17:17:35 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-16 17:17:35 +0000 |
commit | 2ba918775cba8d0b1166f7a14a8e114c2fd73f0d (patch) | |
tree | 52ba49bbe54a98b40223ed212dc7f40f44296771 /crates/ra_cli/src/analysis_bench.rs | |
parent | 98cc51580d0b8a6662f0155d8a45a8cfff469d72 (diff) |
Refactor position parsing
Diffstat (limited to 'crates/ra_cli/src/analysis_bench.rs')
-rw-r--r-- | crates/ra_cli/src/analysis_bench.rs | 29 |
1 files changed, 25 insertions, 4 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 | ||
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,29 @@ 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), |
20 | } | 41 | } |
21 | 42 | ||
22 | pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { | 43 | pub(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)); |