From 2ba918775cba8d0b1166f7a14a8e114c2fd73f0d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 16 Feb 2020 18:17:35 +0100 Subject: Refactor position parsing --- crates/ra_cli/src/analysis_bench.rs | 29 +++++++++++++++++++++++++---- crates/ra_cli/src/main.rs | 18 +++--------------- 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'crates/ra_cli/src') 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 @@ use std::{ path::{Path, PathBuf}, + str::FromStr, sync::Arc, time::Instant, }; @@ -14,9 +15,29 @@ use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FilePosition, LineCol}; use crate::Result; +pub(crate) struct Position { + path: PathBuf, + line: u32, + column: u32, +} + +impl FromStr for Position { + type Err = Box; + fn from_str(s: &str) -> Result { + let (path_line, column) = rsplit_at_char(s, ':')?; + let (path, line) = rsplit_at_char(path_line, ':')?; + Ok(Position { path: path.into(), line: line.parse()?, column: column.parse()? }) + } +} + +fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { + let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; + Ok((&s[..idx], &s[idx + 1..])) +} + pub(crate) enum Op { Highlight { path: PathBuf }, - Complete { path: PathBuf, line: u32, column: u32 }, + Complete(Position), } 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<()> { let file_id = { let path = match &op { Op::Highlight { path } => path, - Op::Complete { path, .. } => path, + Op::Complete(pos) => &pos.path, }; let path = std::env::current_dir()?.join(path).canonicalize()?; roots @@ -61,11 +82,11 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { println!("\n{}", res); } } - Op::Complete { line, column, .. } => { + Op::Complete(pos) => { let offset = host .analysis() .file_line_index(file_id)? - .offset(LineCol { line, col_utf16: column }); + .offset(LineCol { line: pos.line, col_utf16: pos.column }); let file_postion = FilePosition { file_id, offset }; 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<()> { let verbose = matches.contains(["-v", "--verbose"]); let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default(); let highlight_path = matches.opt_value_from_str("--highlight")?; - let complete_path = matches.opt_value_from_str("--complete")?; + let complete_path: Option = matches.opt_value_from_str("--complete")?; if highlight_path.is_some() && complete_path.is_some() { panic!("either --highlight or --complete must be set, not both") } let op = if let Some(path) = highlight_path { let path: String = path; analysis_bench::Op::Highlight { path: path.into() } - } else if let Some(path_line_col) = complete_path { - let path_line_col: String = path_line_col; - let (path_line, column) = rsplit_at_char(path_line_col.as_str(), ':')?; - let (path, line) = rsplit_at_char(path_line, ':')?; - analysis_bench::Op::Complete { - path: path.into(), - line: line.parse()?, - column: column.parse()?, - } + } else if let Some(position) = complete_path { + analysis_bench::Op::Complete(position.parse()?) } else { panic!("either --highlight or --complete must be set") }; @@ -183,8 +176,3 @@ fn read_stdin() -> Result { std::io::stdin().read_to_string(&mut buff)?; Ok(buff) } - -fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { - let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; - Ok((&s[..idx], &s[idx + 1..])) -} -- cgit v1.2.3