From 6be50f7d5de3737464853a589673375fc0cafa97 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 31 Oct 2018 23:41:43 +0300 Subject: Reformat all --- crates/ra_analysis/src/completion.rs | 57 ++++++----- crates/ra_analysis/src/db.rs | 13 +-- crates/ra_analysis/src/descriptors/function/imp.rs | 12 +-- crates/ra_analysis/src/descriptors/function/mod.rs | 29 +++--- .../ra_analysis/src/descriptors/function/scope.rs | 7 +- crates/ra_analysis/src/descriptors/mod.rs | 39 ++++---- crates/ra_analysis/src/descriptors/module/imp.rs | 33 ++++--- crates/ra_analysis/src/descriptors/module/mod.rs | 30 +++--- crates/ra_analysis/src/descriptors/module/scope.rs | 5 +- crates/ra_analysis/src/imp.rs | 77 ++++++++------- crates/ra_analysis/src/input.rs | 9 +- crates/ra_analysis/src/lib.rs | 47 ++++----- crates/ra_analysis/src/mock_analysis.rs | 17 ++-- crates/ra_analysis/src/symbol_index.rs | 5 +- crates/ra_analysis/src/syntax_ptr.rs | 23 +++-- crates/ra_analysis/tests/tests.rs | 105 ++++++++++++++------- 16 files changed, 278 insertions(+), 230 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index 286b6c376..6bd1233f9 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -1,22 +1,22 @@ -use rustc_hash::{FxHashMap, FxHashSet}; -use ra_editor::{find_node_at_offset}; +use ra_editor::find_node_at_offset; use ra_syntax::{ - AtomEdit, File, TextUnit, AstNode, SyntaxNodeRef, algo::visit::{visitor, visitor_ctx, Visitor, VisitorCtx}, ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner}, + AstNode, AtomEdit, File, SyntaxKind::*, + SyntaxNodeRef, TextUnit, }; +use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ - FileId, Cancelable, - input::FilesDatabase, db::{self, SyntaxDatabase}, - descriptors::DescriptorDatabase, descriptors::function::FnScopes, - descriptors::module::{ModuleTree, ModuleId, ModuleScope}, + descriptors::module::{ModuleId, ModuleScope, ModuleTree}, + descriptors::DescriptorDatabase, + input::FilesDatabase, + Cancelable, FileId, }; - #[derive(Debug)] pub struct CompletionItem { /// What user sees in pop-up @@ -27,7 +27,11 @@ pub struct CompletionItem { pub snippet: Option, } -pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable>> { +pub(crate) fn resolve_based_completion( + db: &db::RootDatabase, + file_id: FileId, + offset: TextUnit, +) -> Cancelable>> { let source_root_id = db.file_source_root(file_id); let file = db.file_syntax(file_id); let module_tree = db.module_tree(source_root_id)?; @@ -56,9 +60,12 @@ pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, o Ok(Some(res)) } - - -pub(crate) fn find_target_module(module_tree: &ModuleTree, module_id: ModuleId, file: &File, offset: TextUnit) -> Option { +pub(crate) fn find_target_module( + module_tree: &ModuleTree, + module_id: ModuleId, + file: &File, + offset: TextUnit, +) -> Option { let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset)?; let mut crate_path = crate_path(name_ref)?; @@ -71,8 +78,10 @@ pub(crate) fn find_target_module(module_tree: &ModuleTree, module_id: ModuleId, } fn crate_path(name_ref: ast::NameRef) -> Option> { - let mut path = name_ref.syntax() - .parent().and_then(ast::PathSegment::cast)? + let mut path = name_ref + .syntax() + .parent() + .and_then(ast::PathSegment::cast)? .parent_path(); let mut res = Vec::new(); loop { @@ -80,8 +89,7 @@ fn crate_path(name_ref: ast::NameRef) -> Option> { match segment.kind()? { ast::PathSegmentKind::Name(name) => res.push(name), ast::PathSegmentKind::CrateKw => break, - ast::PathSegmentKind::SelfKw | ast::PathSegmentKind::SuperKw => - return None, + ast::PathSegmentKind::SelfKw | ast::PathSegmentKind::SuperKw => return None, } path = path.qualifier()?; } @@ -89,7 +97,6 @@ fn crate_path(name_ref: ast::NameRef) -> Option> { Some(res) } - pub(crate) fn scope_completion( db: &db::RootDatabase, file_id: FileId, @@ -158,11 +165,7 @@ fn complete_module_items( ); } -fn complete_name_ref( - file: &File, - name_ref: ast::NameRef, - acc: &mut Vec, -) { +fn complete_name_ref(file: &File, name_ref: ast::NameRef, acc: &mut Vec) { if !is_node::(name_ref.syntax()) { return; } @@ -273,7 +276,11 @@ fn is_in_loop_body(name_ref: ast::NameRef) -> bool { .visit::(LoopBodyOwner::loop_body) .accept(node); if let Some(Some(body)) = loop_body { - if name_ref.syntax().range().is_subrange(&body.syntax().range()) { + if name_ref + .syntax() + .range() + .is_subrange(&body.syntax().range()) + { return true; } } @@ -368,9 +375,9 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec, pub params: Vec, - pub doc: Option + pub doc: Option, } impl FnDescriptor { @@ -57,7 +52,9 @@ impl FnDescriptor { }; if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) { - let comment_range = comment_range.checked_sub(node.syntax().range().start()).unwrap(); + let comment_range = comment_range + .checked_sub(node.syntax().range().start()) + .unwrap(); let start = comment_range.start().to_usize(); let end = comment_range.end().to_usize(); @@ -94,7 +91,7 @@ impl FnDescriptor { ret_type, params, label: label.trim().to_owned(), - doc + doc, }) } @@ -105,10 +102,13 @@ impl FnDescriptor { let comment_text = node.doc_comment_text(); - let (begin, end) = node.doc_comments() + let (begin, end) = node + .doc_comments() .map(|comment| comment.syntax().range()) .map(|range| (range.start().to_usize(), range.end().to_usize())) - .fold((std::usize::MAX, std::usize::MIN), |acc, range| (min(acc.0, range.0), max(acc.1, range.1))); + .fold((std::usize::MAX, std::usize::MIN), |acc, range| { + (min(acc.0, range.0), max(acc.1, range.1)) + }); let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); @@ -134,4 +134,3 @@ impl FnDescriptor { res } } - diff --git a/crates/ra_analysis/src/descriptors/function/scope.rs b/crates/ra_analysis/src/descriptors/function/scope.rs index d9929414c..62b46ffba 100644 --- a/crates/ra_analysis/src/descriptors/function/scope.rs +++ b/crates/ra_analysis/src/descriptors/function/scope.rs @@ -51,9 +51,7 @@ impl FnScopes { &self.get(scope).entries } pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { - generate(self.scope_for(node), move |&scope| { - self.get(scope).parent - }) + generate(self.scope_for(node), move |&scope| self.get(scope).parent) } fn root_scope(&mut self) -> ScopeId { let res = ScopeId(self.scopes.len() as u32); @@ -273,13 +271,12 @@ pub fn resolve_local_name<'a>( #[cfg(test)] mod tests { + use ra_editor::find_node_at_offset; use ra_syntax::File; use test_utils::extract_offset; - use ra_editor::{find_node_at_offset}; use super::*; - fn do_check(code: &str, expected: &[&str]) { let (off, code) = extract_offset(code); let code = { diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs index e27f8314a..c28764336 100644 --- a/crates/ra_analysis/src/descriptors/mod.rs +++ b/crates/ra_analysis/src/descriptors/mod.rs @@ -1,24 +1,22 @@ -pub(crate) mod module; pub(crate) mod function; +pub(crate) mod module; use std::sync::Arc; use ra_syntax::{ - SmolStr, ast::{self, AstNode, FnDefNode}, - TextRange + SmolStr, TextRange, }; use crate::{ - FileId, Cancelable, db::SyntaxDatabase, - descriptors::module::{ModuleTree, ModuleId, ModuleScope}, - descriptors::function::{FnId, FnScopes, resolve_local_name}, + descriptors::function::{resolve_local_name, FnId, FnScopes}, + descriptors::module::{ModuleId, ModuleScope, ModuleTree}, input::SourceRootId, - syntax_ptr::{SyntaxPtrDatabase, LocalSyntaxPtr}, + syntax_ptr::{LocalSyntaxPtr, SyntaxPtrDatabase}, + Cancelable, FileId, }; - salsa::query_group! { pub(crate) trait DescriptorDatabase: SyntaxDatabase + SyntaxPtrDatabase { fn module_tree(source_root_id: SourceRootId) -> Cancelable> { @@ -49,23 +47,20 @@ salsa::query_group! { #[derive(Debug)] pub struct ReferenceDescriptor { pub range: TextRange, - pub name: String + pub name: String, } #[derive(Debug)] pub struct DeclarationDescriptor<'a> { pat: ast::BindPat<'a>, - pub range: TextRange + pub range: TextRange, } impl<'a> DeclarationDescriptor<'a> { pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { let range = pat.syntax().range(); - DeclarationDescriptor { - pat, - range - } + DeclarationDescriptor { pat, range } } pub fn find_all_refs(&self) -> Vec { @@ -73,22 +68,22 @@ impl<'a> DeclarationDescriptor<'a> { let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { Some(def) => def, - None => return Default::default() + None => return Default::default(), }; let fn_scopes = FnScopes::new(fn_def); - let refs : Vec<_> = fn_def.syntax().descendants() + let refs: Vec<_> = fn_def + .syntax() + .descendants() .filter_map(ast::NameRef::cast) - .filter(|name_ref| { - match resolve_local_name(*name_ref, &fn_scopes) { - None => false, - Some(entry) => entry.ptr() == name_ptr, - } + .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { + None => false, + Some(entry) => entry.ptr() == name_ptr, }) .map(|name_ref| ReferenceDescriptor { name: name_ref.syntax().text().to_string(), - range : name_ref.syntax().range(), + range: name_ref.syntax().range(), }) .collect(); diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs index dae3a356d..1c102f4e5 100644 --- a/crates/ra_analysis/src/descriptors/module/imp.rs +++ b/crates/ra_analysis/src/descriptors/module/imp.rs @@ -1,24 +1,25 @@ use std::sync::Arc; -use relative_path::RelativePathBuf; -use rustc_hash::{FxHashMap, FxHashSet}; use ra_syntax::{ - SmolStr, ast::{self, NameOwner}, + SmolStr, }; +use relative_path::RelativePathBuf; +use rustc_hash::{FxHashMap, FxHashSet}; use crate::{ - FileId, Cancelable, FileResolverImp, db, - input::{SourceRoot, SourceRootId}, + db, descriptors::DescriptorDatabase, + input::{SourceRoot, SourceRootId}, + Cancelable, FileId, FileResolverImp, }; -use super::{ - ModuleData, ModuleTree, ModuleId, LinkId, LinkData, Problem, ModuleScope -}; - +use super::{LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleTree, Problem}; -pub(crate) fn submodules(db: &impl DescriptorDatabase, file_id: FileId) -> Cancelable>> { +pub(crate) fn submodules( + db: &impl DescriptorDatabase, + file_id: FileId, +) -> Cancelable>> { db::check_canceled(db)?; let file = db.file_syntax(file_id); let root = file.ast(); @@ -57,13 +58,11 @@ pub(crate) fn module_tree( Ok(Arc::new(res)) } - #[derive(Clone, Hash, PartialEq, Eq, Debug)] pub struct Submodule { pub name: SmolStr, } - fn create_module_tree<'a>( db: &impl DescriptorDatabase, source_root: SourceRootId, @@ -82,7 +81,15 @@ fn create_module_tree<'a>( continue; // TODO: use explicit crate_roots here } assert!(!roots.contains_key(&file_id)); - let module_id = build_subtree(db, &source_root, &mut tree, &mut visited, &mut roots, None, file_id)?; + let module_id = build_subtree( + db, + &source_root, + &mut tree, + &mut visited, + &mut roots, + None, + file_id, + )?; roots.insert(file_id, module_id); } Ok(tree) diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index 667553f74..302e3e81c 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs @@ -1,8 +1,11 @@ pub(super) mod imp; pub(crate) mod scope; +use ra_syntax::{ + ast::{self, AstNode, NameOwner}, + SmolStr, SyntaxNode, +}; use relative_path::RelativePathBuf; -use ra_syntax::{ast::{self, NameOwner, AstNode}, SmolStr, SyntaxNode}; use crate::FileId; @@ -16,9 +19,11 @@ pub(crate) struct ModuleTree { impl ModuleTree { pub(crate) fn modules_for_file(&self, file_id: FileId) -> Vec { - self.mods.iter() + self.mods + .iter() .enumerate() - .filter(|(_idx, it)| it.file_id == file_id).map(|(idx, _)| ModuleId(idx as u32)) + .filter(|(_idx, it)| it.file_id == file_id) + .map(|(idx, _)| ModuleId(idx as u32)) .collect() } @@ -50,7 +55,7 @@ impl ModuleId { } pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option { tree.module(self).parent - } + } pub(crate) fn parent(self, tree: &ModuleTree) -> Option { let link = self.parent_link(tree)?; Some(tree.link(link).owner) @@ -69,18 +74,15 @@ impl ModuleId { curr } pub(crate) fn child(self, tree: &ModuleTree, name: &str) -> Option { - let link = tree.module(self) + let link = tree + .module(self) .children .iter() .map(|&it| tree.link(it)) .find(|it| it.name == name)?; Some(*link.points_to.first()?) } - pub(crate) fn problems( - self, - tree: &ModuleTree, - root: ast::Root, - ) -> Vec<(SyntaxNode, Problem)> { + pub(crate) fn problems(self, tree: &ModuleTree, root: ast::Root) -> Vec<(SyntaxNode, Problem)> { tree.module(self) .children .iter() @@ -98,11 +100,7 @@ impl LinkId { pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId { tree.link(self).owner } - pub(crate) fn bind_source<'a>( - self, - tree: &ModuleTree, - root: ast::Root<'a>, - ) -> ast::Module<'a> { + pub(crate) fn bind_source<'a>(self, tree: &ModuleTree, root: ast::Root<'a>) -> ast::Module<'a> { imp::modules(root) .find(|(name, _)| name == &tree.link(self).name) .unwrap() @@ -125,7 +123,6 @@ struct LinkData { problem: Option, } - impl ModuleTree { fn module(&self, id: ModuleId) -> &ModuleData { &self.mods[id.0 as usize] @@ -152,4 +149,3 @@ impl ModuleTree { id } } - diff --git a/crates/ra_analysis/src/descriptors/module/scope.rs b/crates/ra_analysis/src/descriptors/module/scope.rs index 846b8b44f..681e272c2 100644 --- a/crates/ra_analysis/src/descriptors/module/scope.rs +++ b/crates/ra_analysis/src/descriptors/module/scope.rs @@ -1,9 +1,8 @@ //! Backend for module-level scope resolution & completion - use ra_syntax::{ ast::{self, ModuleItemOwner}, - File, AstNode, SmolStr, + AstNode, File, SmolStr, }; use crate::syntax_ptr::LocalSyntaxPtr; @@ -103,7 +102,7 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec) { #[cfg(test)] mod tests { use super::*; - use ra_syntax::{File}; + use ra_syntax::File; fn do_check(code: &str, expected: &[&str]) { let file = File::parse(&code); diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 1eb8cb912..44077b507 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -13,24 +13,21 @@ use ra_syntax::{ use rayon::prelude::*; use relative_path::RelativePath; use rustc_hash::FxHashSet; -use salsa::{ParallelDatabase, Database}; +use salsa::{Database, ParallelDatabase}; use crate::{ - AnalysisChange, - db::{ - self, SyntaxDatabase, FileSyntaxQuery, - }, - input::{SourceRootId, FilesDatabase, SourceRoot, WORKSPACE}, + completion::{resolve_based_completion, scope_completion, CompletionItem}, + db::{self, FileSyntaxQuery, SyntaxDatabase}, descriptors::{ - DescriptorDatabase, DeclarationDescriptor, - module::{ModuleTree, Problem}, function::{FnDescriptor, FnId}, + module::{ModuleTree, Problem}, + DeclarationDescriptor, DescriptorDatabase, }, - completion::{scope_completion, resolve_based_completion, CompletionItem}, + input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, symbol_index::SymbolIndex, syntax_ptr::SyntaxPtrDatabase, - CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position, - Query, SourceChange, SourceFileEdit, Cancelable, + AnalysisChange, Cancelable, CrateGraph, CrateId, Diagnostic, FileId, FileResolver, + FileSystemEdit, Position, Query, SourceChange, SourceFileEdit, }; #[derive(Clone, Debug)] @@ -94,7 +91,6 @@ pub(crate) struct AnalysisHostImpl { db: db::RootDatabase, } - impl AnalysisHostImpl { pub fn new() -> AnalysisHostImpl { let db = db::RootDatabase::default(); @@ -108,7 +104,7 @@ impl AnalysisHostImpl { } pub fn analysis(&self) -> AnalysisImpl { AnalysisImpl { - db: self.db.fork() // freeze revision here + db: self.db.fork(), // freeze revision here } } pub fn apply_change(&mut self, change: AnalysisChange) { @@ -120,7 +116,8 @@ impl AnalysisHostImpl { .set(file_id, Arc::new(text)) } if !(change.files_added.is_empty() && change.files_removed.is_empty()) { - let file_resolver = change.file_resolver + let file_resolver = change + .file_resolver .expect("change resolver when changing set of files"); let mut source_root = SourceRoot::clone(&self.db.source_root(WORKSPACE)); for (file_id, text) in change.files_added { @@ -174,7 +171,8 @@ impl AnalysisHostImpl { .set((), Arc::new(libraries)); } if let Some(crate_graph) = change.crate_graph { - self.db.query(crate::input::CrateGraphQuery) + self.db + .query(crate::input::CrateGraphQuery) .set((), Arc::new(crate_graph)) } } @@ -194,18 +192,22 @@ impl AnalysisImpl { } pub fn world_symbols(&self, query: Query) -> Cancelable> { let buf: Vec> = if query.libs { - self.db.libraries().iter() + self.db + .libraries() + .iter() .map(|&lib_id| self.db.library_symbols(lib_id)) .collect() } else { let files = &self.db.source_root(WORKSPACE).files; let db = self.db.clone(); - files.par_iter() + files + .par_iter() .map_with(db, |db, &file_id| db.file_symbols(file_id)) .filter_map(|it| it.ok()) .collect() }; - self.db.query(FileSyntaxQuery) + self.db + .query(FileSyntaxQuery) .sweep(salsa::SweepStrategy::default().discard_values()); Ok(query.search(&buf)) } @@ -216,7 +218,8 @@ impl AnalysisImpl { pub fn parent_module(&self, file_id: FileId) -> Cancelable> { let module_tree = self.module_tree(file_id)?; - let res = module_tree.modules_for_file(file_id) + let res = module_tree + .modules_for_file(file_id) .into_iter() .filter_map(|module_id| { let link = module_id.parent_link(&module_tree)?; @@ -237,7 +240,8 @@ impl AnalysisImpl { pub fn crate_for(&self, file_id: FileId) -> Cancelable> { let module_tree = self.module_tree(file_id)?; let crate_graph = self.db.crate_graph(); - let res = module_tree.modules_for_file(file_id) + let res = module_tree + .modules_for_file(file_id) .into_iter() .map(|it| it.root(&module_tree)) .map(|it| it.file_id(&module_tree)) @@ -249,7 +253,11 @@ 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, + file_id: FileId, + offset: TextUnit, + ) -> Cancelable>> { let mut res = Vec::new(); let mut has_completions = false; if let Some(scope_based) = scope_completion(&self.db, file_id, offset) { @@ -260,11 +268,7 @@ impl AnalysisImpl { res.extend(scope_based); has_completions = true; } - let res = if has_completions { - Some(res) - } else { - None - }; + let res = if has_completions { Some(res) } else { None }; Ok(res) } pub fn approximately_resolve_symbol( @@ -326,12 +330,11 @@ impl AnalysisImpl { 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, 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 binding = match maybe_binding { None => return Vec::new(), @@ -341,8 +344,11 @@ impl AnalysisImpl { let decl = DeclarationDescriptor::new(binding); let mut ret = vec![(file_id, decl.range)]; - ret.extend(decl.find_all_refs().into_iter() - .map(|ref_desc| (file_id, ref_desc.range ))); + ret.extend( + decl.find_all_refs() + .into_iter() + .map(|ref_desc| (file_id, ref_desc.range)), + ); ret } @@ -526,7 +532,8 @@ impl AnalysisImpl { Some(id) => id, None => return Vec::new(), }; - module_id.child(module_tree, name.as_str()) + module_id + .child(module_tree, name.as_str()) .map(|it| it.file_id(module_tree)) .into_iter() .collect() diff --git a/crates/ra_analysis/src/input.rs b/crates/ra_analysis/src/input.rs index b89b45133..ba8a17fd5 100644 --- a/crates/ra_analysis/src/input.rs +++ b/crates/ra_analysis/src/input.rs @@ -1,12 +1,9 @@ -use std::{ - sync::Arc, - fmt, -}; +use std::{fmt, sync::Arc}; -use salsa; -use rustc_hash::FxHashSet; use relative_path::RelativePath; use rustc_hash::FxHashMap; +use rustc_hash::FxHashSet; +use salsa; use crate::{symbol_index::SymbolIndex, FileResolverImp}; diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index e75411ec9..32aa7a1fd 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -6,23 +6,20 @@ extern crate relative_path; extern crate rustc_hash; extern crate salsa; -mod input; +mod completion; mod db; mod descriptors; mod imp; +mod input; +pub mod mock_analysis; mod symbol_index; -mod completion; mod syntax_ptr; -pub mod mock_analysis; -use std::{ - fmt, - sync::Arc, -}; +use std::{fmt, sync::Arc}; use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; -use relative_path::RelativePathBuf; use rayon::prelude::*; +use relative_path::RelativePathBuf; use crate::{ imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, @@ -30,13 +27,12 @@ use crate::{ }; pub use crate::{ - descriptors::function::FnDescriptor, completion::CompletionItem, - input::{FileId, FileResolver, CrateGraph, CrateId}, + descriptors::function::FnDescriptor, + input::{CrateGraph, CrateId, FileId, FileResolver}, }; pub use ra_editor::{ - FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, - RunnableKind, StructureNode, + FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, }; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -50,8 +46,7 @@ impl std::fmt::Display for Canceled { } } -impl std::error::Error for Canceled { -} +impl std::error::Error for Canceled {} #[derive(Default)] pub struct AnalysisChange { @@ -76,7 +71,6 @@ impl fmt::Debug for AnalysisChange { } } - impl AnalysisChange { pub fn new() -> AnalysisChange { AnalysisChange::default() @@ -251,12 +245,15 @@ impl Analysis { pub fn approximately_resolve_symbol( &self, file_id: FileId, - offset: TextUnit + offset: TextUnit, ) -> Cancelable> { - self.imp - .approximately_resolve_symbol(file_id, offset) + self.imp.approximately_resolve_symbol(file_id, offset) } - pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, ) -> Cancelable> { + pub fn find_all_refs( + &self, + file_id: FileId, + offset: TextUnit, + ) -> Cancelable> { Ok(self.imp.find_all_refs(file_id, offset)) } pub fn parent_module(&self, file_id: FileId) -> Cancelable> { @@ -276,7 +273,11 @@ 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>> { + pub fn completions( + &self, + file_id: FileId, + offset: TextUnit, + ) -> Cancelable>> { self.imp.completions(file_id, offset) } pub fn assists(&self, file_id: FileId, range: TextRange) -> Cancelable> { @@ -307,7 +308,11 @@ impl LibraryData { let file = File::parse(text); (*file_id, file) })); - LibraryData { files, file_resolver: FileResolverImp::new(file_resolver), symbol_index } + LibraryData { + files, + file_resolver: FileResolverImp::new(file_resolver), + symbol_index, + } } } diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index f72911192..76100f548 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs @@ -1,13 +1,10 @@ - use std::sync::Arc; -use relative_path::{RelativePath, RelativePathBuf}; use ra_syntax::TextUnit; +use relative_path::{RelativePath, RelativePathBuf}; use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; -use crate::{ - AnalysisChange, Analysis, AnalysisHost, FileId, FileResolver, -}; +use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver}; #[derive(Debug)] pub struct FilePosition { @@ -51,7 +48,10 @@ impl MockAnalysis { let mut res = MockAnalysis::new(); for entry in parse_fixture(fixture) { if entry.text.contains(CURSOR_MARKER) { - assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); + assert!( + position.is_none(), + "only one marker (<|>) per fixture is allowed" + ); position = Some(res.add_file_with_position(&entry.meta, &entry.text)); } else { res.add_file(&entry.meta, &entry.text); @@ -73,7 +73,10 @@ impl MockAnalysis { FilePosition { file_id, offset } } pub fn id_of(&self, path: &str) -> FileId { - let (idx, _) = self.files.iter().enumerate() + let (idx, _) = self + .files + .iter() + .enumerate() .find(|(_, (p, _text))| path == p) .expect("no file in this mock"); FileId(idx as u32 + 1) diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index 5f302cbda..b57ad5d33 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -57,10 +57,7 @@ impl SymbolIndex { } impl Query { - pub(crate) fn search( - self, - indices: &[Arc], - ) -> Vec<(FileId, FileSymbol)> { + pub(crate) fn search(self, indices: &[Arc]) -> Vec<(FileId, FileSymbol)> { let mut op = fst::map::OpBuilder::new(); for file_symbols in indices.iter() { let automaton = fst::automaton::Subsequence::new(&self.lowercased); diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs index c3c904633..612f75cd9 100644 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ b/crates/ra_analysis/src/syntax_ptr.rs @@ -1,12 +1,12 @@ use std::marker::PhantomData; use ra_syntax::{ - File, TextRange, SyntaxKind, SyntaxNode, SyntaxNodeRef, ast::{self, AstNode}, + File, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange, }; -use crate::FileId; use crate::db::SyntaxDatabase; +use crate::FileId; salsa::query_group! { pub(crate) trait SyntaxPtrDatabase: SyntaxDatabase { @@ -52,12 +52,10 @@ trait ToAst { impl<'a> ToAst for &'a OwnedAst> { type Ast = ast::FnDef<'a>; fn to_ast(self) -> ast::FnDef<'a> { - ast::FnDef::cast(self.syntax.borrowed()) - .unwrap() + ast::FnDef::cast(self.syntax.borrowed()).unwrap() } } - /// A pionter to a syntax node inside a file. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(crate) struct LocalSyntaxPtr { @@ -79,22 +77,29 @@ impl LocalSyntaxPtr { if curr.range() == self.range && curr.kind() == self.kind { return curr.owned(); } - curr = curr.children() + curr = curr + .children() .find(|it| self.range.is_subrange(&it.range())) .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) } } pub(crate) fn into_global(self, file_id: FileId) -> SyntaxPtr { - SyntaxPtr { file_id, local: self} + SyntaxPtr { + file_id, + local: self, + } } } - #[test] fn test_local_syntax_ptr() { let file = File::parse("struct Foo { f: u32, }"); - let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap(); + let field = file + .syntax() + .descendants() + .find_map(ast::NamedFieldDef::cast) + .unwrap(); let ptr = LocalSyntaxPtr::new(field.syntax()); let field_syntax = ptr.resolve(&file); assert_eq!(field.syntax(), field_syntax); diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 22d27cdbe..c2754c8e4 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -5,42 +5,53 @@ extern crate relative_path; extern crate rustc_hash; extern crate test_utils; -use ra_syntax::{TextRange}; -use test_utils::{assert_eq_dbg}; +use ra_syntax::TextRange; +use test_utils::assert_eq_dbg; use ra_analysis::{ + mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, AnalysisChange, CrateGraph, FileId, FnDescriptor, - mock_analysis::{MockAnalysis, single_file, single_file_with_position, analysis_and_position}, }; 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.file_id, position.offset) + .unwrap() + .unwrap() } #[test] fn test_resolve_module() { - let (analysis, pos) = analysis_and_position(" + let (analysis, pos) = analysis_and_position( + " //- /lib.rs mod <|>foo; //- /foo.rs // empty - "); + ", + ); - let symbols = analysis.approximately_resolve_symbol(pos.file_id, pos.offset).unwrap(); + let symbols = analysis + .approximately_resolve_symbol(pos.file_id, pos.offset) + .unwrap(); assert_eq_dbg( r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, &symbols, ); - let (analysis, pos) = analysis_and_position(" + let (analysis, pos) = analysis_and_position( + " //- /lib.rs mod <|>foo; //- /foo/mod.rs // empty - "); + ", + ); - let symbols = analysis.approximately_resolve_symbol(pos.file_id, pos.offset).unwrap(); + let symbols = analysis + .approximately_resolve_symbol(pos.file_id, pos.offset) + .unwrap(); assert_eq_dbg( r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#, &symbols, @@ -73,12 +84,14 @@ fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() { #[test] fn test_resolve_parent_module() { - let (analysis, pos) = analysis_and_position(" + let (analysis, pos) = analysis_and_position( + " //- /lib.rs mod foo; //- /foo.rs <|>// empty - "); + ", + ); let symbols = analysis.parent_module(pos.file_id).unwrap(); assert_eq_dbg( r#"[(FileId(1), FileSymbol { name: "foo", node_range: [0; 8), kind: MODULE })]"#, @@ -88,12 +101,14 @@ fn test_resolve_parent_module() { #[test] fn test_resolve_crate_root() { - let mock = MockAnalysis::with_files(" + let mock = MockAnalysis::with_files( + " //- /lib.rs mod foo; //- /foo.rs // emtpy <|> - "); + ", + ); let root_file = mock.id_of("/lib.rs"); let mod_file = mock.id_of("/foo.rs"); let mut host = mock.analysis_host(); @@ -245,8 +260,10 @@ pub fn do() { assert_eq!(desc.ret_type, Some("-> i32".to_string())); assert_eq!(param, Some(0)); assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string()); - assert_eq!(desc.doc, Some( -r#"Adds one to the number given. + assert_eq!( + desc.doc, + Some( + r#"Adds one to the number given. # Examples @@ -254,7 +271,10 @@ r#"Adds one to the number given. let five = 5; assert_eq!(6, my_crate::add_one(5)); -```"#.into())); +```"# + .into() + ) + ); } #[test] @@ -280,15 +300,18 @@ impl addr { pub fn do_it() { addr {}; addr::add_one(<|>); -}"#); +}"#, + ); assert_eq!(desc.name, "add_one".to_string()); assert_eq!(desc.params, vec!["x".to_string()]); assert_eq!(desc.ret_type, Some("-> i32".to_string())); assert_eq!(param, Some(0)); assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string()); - assert_eq!(desc.doc, Some( -r#"Adds one to the number given. + assert_eq!( + desc.doc, + Some( + r#"Adds one to the number given. # Examples @@ -296,7 +319,10 @@ r#"Adds one to the number given. let five = 5; assert_eq!(6, my_crate::add_one(5)); -```"#.into())); +```"# + .into() + ) + ); } #[test] @@ -329,22 +355,32 @@ pub fn foo() { r.finished(<|>); } -"#); +"#, + ); assert_eq!(desc.name, "finished".to_string()); - assert_eq!(desc.params, vec!["&mut self".to_string(), "ctx".to_string()]); + assert_eq!( + desc.params, + vec!["&mut self".to_string(), "ctx".to_string()] + ); assert_eq!(desc.ret_type, None); assert_eq!(param, Some(1)); - assert_eq!(desc.doc, Some( -r#"Method is called when writer finishes. - -By default this method stops actor's `Context`."#.into())); + assert_eq!( + desc.doc, + Some( + r#"Method is called when writer finishes. + +By default this method stops actor's `Context`."# + .into() + ) + ); } - 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.file_id, position.offset) + .unwrap() } #[test] @@ -390,14 +426,19 @@ fn test_find_all_refs_for_fn_param() { #[test] fn test_complete_crate_path() { - let (analysis, position) = analysis_and_position(" + let (analysis, position) = analysis_and_position( + " //- /lib.rs mod foo; struct Spam; //- /foo.rs use crate::Sp<|> - "); - let completions = analysis.completions(position.file_id, position.offset).unwrap().unwrap(); + ", + ); + let completions = analysis + .completions(position.file_id, position.offset) + .unwrap() + .unwrap(); assert_eq_dbg( r#"[CompletionItem { label: "foo", lookup: None, snippet: None }, CompletionItem { label: "Spam", lookup: None, snippet: None }]"#, -- cgit v1.2.3