Refactor position parsing

This commit is contained in:
Aleksey Kladov 2020-02-16 18:17:35 +01:00
parent 98cc51580d
commit 2ba918775c
2 changed files with 28 additions and 19 deletions

View file

@ -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<dyn std::error::Error + Send + Sync>;
fn from_str(s: &str) -> Result<Self> {
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));

View file

@ -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<String> = 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<String> {
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..]))
}