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 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'crates/ra_cli/src/analysis_bench.rs') 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)); -- cgit v1.2.3 From 0f79ec76d6fd29d851e05c9140192e4505097185 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 16 Feb 2020 18:24:06 +0100 Subject: Support goto def in bences --- crates/ra_cli/src/analysis_bench.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'crates/ra_cli/src/analysis_bench.rs') diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 1882a9342..a7cc3a2d4 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -38,6 +38,7 @@ fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { pub(crate) enum Op { Highlight { path: PathBuf }, Complete(Position), + GotoDef(Position), } pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { @@ -52,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(pos) => &pos.path, + Op::Complete(pos) | Op::GotoDef(pos) => &pos.path, }; let path = std::env::current_dir()?.join(path).canonicalize()?; roots @@ -72,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(); @@ -82,16 +83,30 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { println!("\n{}", res); } } - Op::Complete(pos) => { + 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: 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)); - 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); + } } } } -- cgit v1.2.3 From ca7e9ab0da4f9a781db1857cd11951d296df24a9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 16 Feb 2020 18:30:48 +0100 Subject: 1-based columns --- crates/ra_cli/src/analysis_bench.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_cli/src/analysis_bench.rs') diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index a7cc3a2d4..4835a68ce 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -92,7 +92,7 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { let offset = host .analysis() .file_line_index(file_id)? - .offset(LineCol { line: pos.line, col_utf16: pos.column }); + .offset(LineCol { line: pos.line - 1, col_utf16: pos.column }); let file_postion = FilePosition { file_id, offset }; if is_completion { -- cgit v1.2.3