3172: 1-based columns r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-02-16 17:31:16 +00:00 committed by GitHub
commit 10f910df4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 32 deletions

View File

@ -2,6 +2,7 @@
use std::{
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
time::Instant,
};
@ -14,9 +15,30 @@ 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),
GotoDef(Position),
}
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<()> {
let file_id = {
let path = match &op {
Op::Highlight { path } => path,
Op::Complete { path, .. } => path,
Op::Complete(pos) | Op::GotoDef(pos) => &pos.path,
};
let path = std::env::current_dir()?.join(path).canonicalize()?;
roots
@ -51,7 +73,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
.ok_or_else(|| format!("Can't find {:?}", path))?
};
match op {
match &op {
Op::Highlight { .. } => {
let res = do_work(&mut host, file_id, |analysis| {
analysis.diagnostics(file_id).unwrap();
@ -61,16 +83,30 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
println!("\n{}", res);
}
}
Op::Complete { line, column, .. } => {
Op::Complete(pos) | Op::GotoDef(pos) => {
let is_completion = match op {
Op::Complete(..) => true,
_ => false,
};
let offset = host
.analysis()
.file_line_index(file_id)?
.offset(LineCol { line, col_utf16: column });
.offset(LineCol { line: pos.line - 1, col_utf16: pos.column });
let file_postion = FilePosition { file_id, offset };
let res = do_work(&mut host, file_id, |analysis| analysis.completions(file_postion));
if verbose {
println!("\n{:#?}", res);
if is_completion {
let res =
do_work(&mut host, file_id, |analysis| analysis.completions(file_postion));
if verbose {
println!("\n{:#?}", res);
}
} else {
let res =
do_work(&mut host, file_id, |analysis| analysis.goto_definition(file_postion));
if verbose {
println!("\n{:#?}", res);
}
}
}
}

View File

@ -132,25 +132,16 @@ 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")?;
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 {
panic!("either --highlight or --complete must be set")
let highlight_path: Option<String> = matches.opt_value_from_str("--highlight")?;
let complete_path: Option<String> = matches.opt_value_from_str("--complete")?;
let goto_def_path: Option<String> = matches.opt_value_from_str("--goto-def")?;
let op = match (highlight_path, complete_path, goto_def_path) {
(Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() },
(None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?),
(None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?),
_ => panic!(
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
),
};
matches.finish().or_else(handle_extra_flags)?;
analysis_bench::run(verbose, path.as_ref(), op)?;
@ -183,8 +174,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..]))
}