From 1e1c188cbf1fbb80ee398977e6635b9a54a3ab7a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 5 Nov 2018 14:24:38 +0300 Subject: rename Position to FilePostion --- crates/ra_analysis/src/imp.rs | 4 ++-- crates/ra_analysis/src/lib.rs | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 704648b59..86d9cecd2 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -27,7 +27,7 @@ use crate::{ input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, symbol_index::SymbolIndex, AnalysisChange, Cancelable, CrateGraph, CrateId, Diagnostic, FileId, FileResolver, - FileSystemEdit, Position, Query, SourceChange, SourceFileEdit, + FileSystemEdit, FilePosition, Query, SourceChange, SourceFileEdit, }; #[derive(Clone, Debug)] @@ -568,7 +568,7 @@ impl SourceChange { file_system_edits: vec![], cursor_position: edit .cursor_position - .map(|offset| Position { offset, file_id }), + .map(|offset| FilePosition { offset, file_id }), } } } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index fee382151..3bbe61917 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -119,18 +119,18 @@ impl AnalysisHost { } } +#[derive(Debug)] +pub struct FilePosition { + pub file_id: FileId, + pub offset: TextUnit, +} + #[derive(Debug)] pub struct SourceChange { pub label: String, pub source_file_edits: Vec, pub file_system_edits: Vec, - pub cursor_position: Option, -} - -#[derive(Debug)] -pub struct Position { - pub file_id: FileId, - pub offset: TextUnit, + pub cursor_position: Option, } #[derive(Debug)] -- cgit v1.2.3 From 0192c116f78b3a81ef15587eecac29a2676ae00c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 5 Nov 2018 14:57:41 +0300 Subject: Use FilePosition everywhere --- crates/ra_analysis/src/completion.rs | 31 +++++++------- crates/ra_analysis/src/imp.rs | 74 +++++++++++++++------------------ crates/ra_analysis/src/lib.rs | 52 +++++++++-------------- crates/ra_analysis/src/mock_analysis.rs | 9 +--- crates/ra_analysis/tests/tests.rs | 26 ++++-------- 5 files changed, 76 insertions(+), 116 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index 766df1d96..7c3476e5c 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -14,7 +14,7 @@ use crate::{ descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource}, descriptors::DescriptorDatabase, input::FilesDatabase, - Cancelable, FileId, + Cancelable, FilePosition, }; #[derive(Debug)] @@ -29,21 +29,21 @@ pub struct CompletionItem { pub(crate) fn resolve_based_completion( db: &db::RootDatabase, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable>> { - let source_root_id = db.file_source_root(file_id); - let file = db.file_syntax(file_id); + let source_root_id = db.file_source_root(position.file_id); + let file = db.file_syntax(position.file_id); let module_tree = db.module_tree(source_root_id)?; - let module_id = match module_tree.any_module_for_source(ModuleSource::File(file_id)) { + let module_id = match module_tree.any_module_for_source(ModuleSource::File(position.file_id)) { None => return Ok(None), Some(it) => it, }; let file = { - let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); + let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string()); file.reparse(&edit) }; - let target_module_id = match find_target_module(&module_tree, module_id, &file, offset) { + let target_module_id = match find_target_module(&module_tree, module_id, &file, position.offset) + { None => return Ok(None), Some(it) => it, }; @@ -99,18 +99,17 @@ fn crate_path(name_ref: ast::NameRef) -> Option> { pub(crate) fn scope_completion( db: &db::RootDatabase, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Option> { - let original_file = db.file_syntax(file_id); + let original_file = db.file_syntax(position.file_id); // Insert a fake ident to get a valid parse tree let file = { - let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); + let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string()); original_file.reparse(&edit) }; let mut has_completions = false; let mut res = Vec::new(); - if let Some(name_ref) = find_node_at_offset::(file.syntax(), offset) { + if let Some(name_ref) = find_node_at_offset::(file.syntax(), position.offset) { has_completions = true; complete_name_ref(&file, name_ref, &mut res); // special case, `trait T { fn foo(i_am_a_name_ref) {} }` @@ -129,7 +128,7 @@ pub(crate) fn scope_completion( _ => (), } } - if let Some(name) = find_node_at_offset::(file.syntax(), offset) { + if let Some(name) = find_node_at_offset::(file.syntax(), position.offset) { if is_node::(name.syntax()) { has_completions = true; param_completions(name.syntax(), &mut res); @@ -383,7 +382,7 @@ mod tests { fn check_scope_completion(code: &str, expected_completions: &str) { let (analysis, position) = single_file_with_position(code); - let completions = scope_completion(&analysis.imp.db, position.file_id, position.offset) + let completions = scope_completion(&analysis.imp.db, position) .unwrap() .into_iter() .filter(|c| c.snippet.is_none()) @@ -393,7 +392,7 @@ mod tests { fn check_snippet_completion(code: &str, expected_completions: &str) { let (analysis, position) = single_file_with_position(code); - let completions = scope_completion(&analysis.imp.db, position.file_id, position.offset) + let completions = scope_completion(&analysis.imp.db, position) .unwrap() .into_iter() .filter(|c| c.snippet.is_some()) diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 86d9cecd2..f2482559f 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -220,16 +220,13 @@ impl AnalysisImpl { let source_root = self.db.file_source_root(file_id); self.db.module_tree(source_root) } - pub fn parent_module( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable> { - let module_tree = self.module_tree(file_id)?; - let file = self.db.file_syntax(file_id); - let module_source = match find_node_at_offset::(file.syntax(), offset) { - Some(m) if !m.has_semi() => ModuleSource::new_inline(file_id, m), - _ => ModuleSource::File(file_id), + pub fn parent_module(&self, position: FilePosition) -> Cancelable> { + let module_tree = self.module_tree(position.file_id)?; + let file = self.db.file_syntax(position.file_id); + let module_source = match find_node_at_offset::(file.syntax(), position.offset) + { + Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), + _ => ModuleSource::File(position.file_id), }; let res = module_tree @@ -269,18 +266,14 @@ impl AnalysisImpl { pub fn crate_root(&self, crate_id: CrateId) -> FileId { self.db.crate_graph().crate_roots[&crate_id] } - pub fn completions( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable>> { + pub fn completions(&self, position: FilePosition) -> Cancelable>> { let mut res = Vec::new(); let mut has_completions = false; - if let Some(scope_based) = scope_completion(&self.db, file_id, offset) { + if let Some(scope_based) = scope_completion(&self.db, position) { res.extend(scope_based); has_completions = true; } - if let Some(scope_based) = resolve_based_completion(&self.db, file_id, offset)? { + if let Some(scope_based) = resolve_based_completion(&self.db, position)? { res.extend(scope_based); has_completions = true; } @@ -289,18 +282,19 @@ impl AnalysisImpl { } pub fn approximately_resolve_symbol( &self, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable> { - let module_tree = self.module_tree(file_id)?; - let file = self.db.file_syntax(file_id); + let module_tree = self.module_tree(position.file_id)?; + let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); - if let Some(name_ref) = find_node_at_offset::(syntax, offset) { + if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { // First try to resolve the symbol locally - return if let Some((name, range)) = resolve_local_name(&self.db, file_id, name_ref) { + return if let Some((name, range)) = + resolve_local_name(&self.db, position.file_id, name_ref) + { let mut vec = vec![]; vec.push(( - file_id, + position.file_id, FileSymbol { name, node_range: range, @@ -313,10 +307,10 @@ impl AnalysisImpl { self.index_resolve(name_ref) }; } - if let Some(name) = find_node_at_offset::(syntax, offset) { + if let Some(name) = find_node_at_offset::(syntax, position.offset) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { - let file_ids = self.resolve_module(&*module_tree, file_id, module); + let file_ids = self.resolve_module(&*module_tree, position.file_id, module); let res = file_ids .into_iter() @@ -341,16 +335,17 @@ impl AnalysisImpl { Ok(vec![]) } - pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> { - let file = self.db.file_syntax(file_id); + pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { + let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); // Find the binding associated with the offset - let maybe_binding = find_node_at_offset::(syntax, offset).or_else(|| { - let name_ref = find_node_at_offset::(syntax, offset)?; - let resolved = resolve_local_name(&self.db, file_id, name_ref)?; - find_node_at_offset::(syntax, resolved.1.end()) - }); + let maybe_binding = + find_node_at_offset::(syntax, position.offset).or_else(|| { + let name_ref = find_node_at_offset::(syntax, position.offset)?; + let resolved = resolve_local_name(&self.db, position.file_id, name_ref)?; + find_node_at_offset::(syntax, resolved.1.end()) + }); let binding = match maybe_binding { None => return Vec::new(), @@ -359,11 +354,11 @@ impl AnalysisImpl { let decl = DeclarationDescriptor::new(binding); - let mut ret = vec![(file_id, decl.range)]; + let mut ret = vec![(position.file_id, decl.range)]; ret.extend( decl.find_all_refs() .into_iter() - .map(|ref_desc| (file_id, ref_desc.range)), + .map(|ref_desc| (position.file_id, ref_desc.range)), ); ret @@ -457,14 +452,13 @@ impl AnalysisImpl { pub fn resolve_callable( &self, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable)>> { - let file = self.db.file_syntax(file_id); + let file = self.db.file_syntax(position.file_id); let syntax = file.syntax(); // Find the calling expression and it's NameRef - let calling_node = match FnCallNode::with_node(syntax, offset) { + let calling_node = match FnCallNode::with_node(syntax, position.offset) { Some(node) => node, None => return Ok(None), }; @@ -499,7 +493,7 @@ impl AnalysisImpl { if let Some(ref arg_list) = calling_node.arg_list() { let start = arg_list.syntax().range().start(); - let range_search = TextRange::from_to(start, offset); + let range_search = TextRange::from_to(start, position.offset); let mut commas: usize = arg_list .syntax() .text() diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 3bbe61917..0ea9ebee7 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -119,7 +119,7 @@ impl AnalysisHost { } } -#[derive(Debug)] +#[derive(Clone, Copy, Debug)] pub struct FilePosition { pub file_id: FileId, pub offset: TextUnit, @@ -224,18 +224,18 @@ impl Analysis { let file = self.imp.file_syntax(file_id); SourceChange::from_local_edit(file_id, "join lines", ra_editor::join_lines(&file, range)) } - pub fn on_enter(&self, file_id: FileId, offset: TextUnit) -> Option { - let file = self.imp.file_syntax(file_id); - let edit = ra_editor::on_enter(&file, offset)?; - let res = SourceChange::from_local_edit(file_id, "on enter", edit); + pub fn on_enter(&self, position: FilePosition) -> Option { + let file = self.imp.file_syntax(position.file_id); + let edit = ra_editor::on_enter(&file, position.offset)?; + let res = SourceChange::from_local_edit(position.file_id, "on enter", edit); Some(res) } - pub fn on_eq_typed(&self, file_id: FileId, offset: TextUnit) -> Option { - let file = self.imp.file_syntax(file_id); + pub fn on_eq_typed(&self, position: FilePosition) -> Option { + let file = self.imp.file_syntax(position.file_id); Some(SourceChange::from_local_edit( - file_id, + position.file_id, "add semicolon", - ra_editor::on_eq_typed(&file, offset)?, + ra_editor::on_eq_typed(&file, position.offset)?, )) } pub fn file_structure(&self, file_id: FileId) -> Vec { @@ -251,24 +251,15 @@ impl Analysis { } pub fn approximately_resolve_symbol( &self, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable> { - self.imp.approximately_resolve_symbol(file_id, offset) + self.imp.approximately_resolve_symbol(position) } - pub fn find_all_refs( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable> { - Ok(self.imp.find_all_refs(file_id, offset)) + pub fn find_all_refs(&self, position: FilePosition) -> Cancelable> { + Ok(self.imp.find_all_refs(position)) } - pub fn parent_module( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable> { - self.imp.parent_module(file_id, offset) + pub fn parent_module(&self, position: FilePosition) -> Cancelable> { + self.imp.parent_module(position) } pub fn crate_for(&self, file_id: FileId) -> Cancelable> { self.imp.crate_for(file_id) @@ -284,12 +275,8 @@ impl Analysis { let file = self.imp.file_syntax(file_id); Ok(ra_editor::highlight(&file)) } - pub fn completions( - &self, - file_id: FileId, - offset: TextUnit, - ) -> Cancelable>> { - self.imp.completions(file_id, offset) + pub fn completions(&self, position: FilePosition) -> Cancelable>> { + self.imp.completions(position) } pub fn assists(&self, file_id: FileId, range: TextRange) -> Cancelable> { Ok(self.imp.assists(file_id, range)) @@ -299,10 +286,9 @@ impl Analysis { } pub fn resolve_callable( &self, - file_id: FileId, - offset: TextUnit, + position: FilePosition, ) -> Cancelable)>> { - self.imp.resolve_callable(file_id, offset) + self.imp.resolve_callable(position) } } diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index a7134a0e6..8e8f969f4 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs @@ -1,16 +1,9 @@ use std::sync::Arc; -use ra_syntax::TextUnit; use relative_path::{RelativePath, RelativePathBuf}; use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; -use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver}; - -#[derive(Debug)] -pub struct FilePosition { - pub file_id: FileId, - pub offset: TextUnit, -} +use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver, FilePosition}; /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis /// from a set of in-memory files. diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 7f7bb8e6b..c605d34f0 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -15,10 +15,7 @@ use ra_analysis::{ fn get_signature(text: &str) -> (FnDescriptor, Option) { let (analysis, position) = single_file_with_position(text); - analysis - .resolve_callable(position.file_id, position.offset) - .unwrap() - .unwrap() + analysis.resolve_callable(position).unwrap().unwrap() } #[test] @@ -32,9 +29,7 @@ fn test_resolve_module() { ", ); - let symbols = analysis - .approximately_resolve_symbol(pos.file_id, pos.offset) - .unwrap(); + let symbols = analysis.approximately_resolve_symbol(pos).unwrap(); assert_eq_dbg( r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, &symbols, @@ -49,9 +44,7 @@ fn test_resolve_module() { ", ); - let symbols = analysis - .approximately_resolve_symbol(pos.file_id, pos.offset) - .unwrap(); + let symbols = analysis.approximately_resolve_symbol(pos).unwrap(); assert_eq_dbg( r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, &symbols, @@ -92,7 +85,7 @@ fn test_resolve_parent_module() { <|>// empty ", ); - let symbols = analysis.parent_module(pos.file_id, pos.offset).unwrap(); + let symbols = analysis.parent_module(pos).unwrap(); assert_eq_dbg( r#"[(FileId(1), FileSymbol { name: "foo", node_range: [4; 7), kind: MODULE })]"#, &symbols, @@ -111,7 +104,7 @@ fn test_resolve_parent_module_for_inline() { } ", ); - let symbols = analysis.parent_module(pos.file_id, pos.offset).unwrap(); + let symbols = analysis.parent_module(pos).unwrap(); assert_eq_dbg( r#"[(FileId(1), FileSymbol { name: "bar", node_range: [18; 21), kind: MODULE })]"#, &symbols, @@ -397,9 +390,7 @@ By default this method stops actor's `Context`."# fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { let (analysis, position) = single_file_with_position(text); - analysis - .find_all_refs(position.file_id, position.offset) - .unwrap() + analysis.find_all_refs(position).unwrap() } #[test] @@ -454,10 +445,7 @@ fn test_complete_crate_path() { use crate::Sp<|> ", ); - let completions = analysis - .completions(position.file_id, position.offset) - .unwrap() - .unwrap(); + let completions = analysis.completions(position).unwrap().unwrap(); assert_eq_dbg( r#"[CompletionItem { label: "foo", lookup: None, snippet: None }, CompletionItem { label: "Spam", lookup: None, snippet: None }]"#, -- cgit v1.2.3