From 61f3a438d3a729a6be941bca1ff4c6a97a33f221 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Mon, 15 Oct 2018 17:44:23 -0400 Subject: Cargo Format Run `cargo fmt` and ignore generated files --- crates/ra_editor/src/scope/fn_scope.rs | 150 ++++++++++++++++++++------------ crates/ra_editor/src/scope/mod.rs | 3 +- crates/ra_editor/src/scope/mod_scope.rs | 47 +++++----- 3 files changed, 121 insertions(+), 79 deletions(-) (limited to 'crates/ra_editor/src/scope') diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index 99d698b60..9088e5a60 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs @@ -1,10 +1,11 @@ use std::fmt; + use rustc_hash::FxHashMap; use ra_syntax::{ - SyntaxNodeRef, SyntaxNode, SmolStr, AstNode, - ast::{self, NameOwner, LoopBodyOwner, ArgListOwner}, - algo::{generate} + algo::generate, + ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, + AstNode, SmolStr, SyntaxNode, SyntaxNodeRef, }; type ScopeId = usize; @@ -19,11 +20,12 @@ pub struct FnScopes { impl FnScopes { pub fn new(fn_def: ast::FnDef) -> FnScopes { let mut scopes = FnScopes { - self_param: fn_def.param_list() + self_param: fn_def + .param_list() .and_then(|it| it.self_param()) .map(|it| it.syntax().owned()), scopes: Vec::new(), - scope_for: FxHashMap::default() + scope_for: FxHashMap::default(), }; let root = scopes.root_scope(); scopes.add_params_bindings(root, fn_def.param_list()); @@ -35,27 +37,38 @@ impl FnScopes { pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] { &self.scopes[scope].entries } - pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { - generate(self.scope_for(node), move |&scope| self.scopes[scope].parent) + pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator + 'a { + generate(self.scope_for(node), move |&scope| { + self.scopes[scope].parent + }) } fn root_scope(&mut self) -> ScopeId { let res = self.scopes.len(); - self.scopes.push(ScopeData { parent: None, entries: vec![] }); + self.scopes.push(ScopeData { + parent: None, + entries: vec![], + }); res } fn new_scope(&mut self, parent: ScopeId) -> ScopeId { let res = self.scopes.len(); - self.scopes.push(ScopeData { parent: Some(parent), entries: vec![] }); + self.scopes.push(ScopeData { + parent: Some(parent), + entries: vec![], + }); res } fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) { - let entries = pat.syntax().descendants() + let entries = pat + .syntax() + .descendants() .filter_map(ast::BindPat::cast) .filter_map(ScopeEntry::new); self.scopes[scope].entries.extend(entries); } fn add_params_bindings(&mut self, scope: ScopeId, params: Option) { - params.into_iter() + params + .into_iter() .flat_map(|it| it.params()) .filter_map(|it| it.pat()) .for_each(|it| self.add_bindings(scope, it)); @@ -71,34 +84,33 @@ impl FnScopes { } pub struct ScopeEntry { - syntax: SyntaxNode + syntax: SyntaxNode, } impl ScopeEntry { fn new(pat: ast::BindPat) -> Option { if pat.name().is_some() { - Some(ScopeEntry { syntax: pat.syntax().owned() }) + Some(ScopeEntry { + syntax: pat.syntax().owned(), + }) } else { None } } pub fn name(&self) -> SmolStr { - self.ast().name() - .unwrap() - .text() + self.ast().name().unwrap().text() } pub fn ast(&self) -> ast::BindPat { - ast::BindPat::cast(self.syntax.borrowed()) - .unwrap() + ast::BindPat::cast(self.syntax.borrowed()).unwrap() } } impl fmt::Debug for ScopeEntry { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("ScopeEntry") - .field("name", &self.name()) - .field("syntax", &self.syntax) - .finish() + .field("name", &self.name()) + .field("syntax", &self.syntax) + .finish() } } @@ -132,16 +144,16 @@ fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: Sco fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { match expr { ast::Expr::IfExpr(e) => { - let cond_scope = e.condition().and_then(|cond| { - compute_cond_scopes(cond, scopes, scope) - }); + let cond_scope = e + .condition() + .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); if let Some(block) = e.then_branch() { compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); } if let Some(block) = e.else_branch() { compute_block_scopes(block, scopes, scope); } - }, + } ast::Expr::BlockExpr(e) => { if let Some(block) = e.block() { compute_block_scopes(block, scopes, scope); @@ -153,9 +165,9 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { } } ast::Expr::WhileExpr(e) => { - let cond_scope = e.condition().and_then(|cond| { - compute_cond_scopes(cond, scopes, scope) - }); + let cond_scope = e + .condition() + .and_then(|cond| compute_cond_scopes(cond, scopes, scope)); if let Some(block) = e.loop_body() { compute_block_scopes(block, scopes, cond_scope.unwrap_or(scope)); } @@ -201,25 +213,31 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { } } } - _ => { - expr.syntax().children() - .filter_map(ast::Expr::cast) - .for_each(|expr| compute_expr_scopes(expr, scopes, scope)) - } + _ => expr + .syntax() + .children() + .filter_map(ast::Expr::cast) + .for_each(|expr| compute_expr_scopes(expr, scopes, scope)), }; fn compute_call_scopes( receiver: Option, arg_list: Option, - scopes: &mut FnScopes, scope: ScopeId, + scopes: &mut FnScopes, + scope: ScopeId, ) { - arg_list.into_iter() + arg_list + .into_iter() .flat_map(|it| it.args()) .chain(receiver) .for_each(|expr| compute_expr_scopes(expr, scopes, scope)); } - fn compute_cond_scopes(cond: ast::Condition, scopes: &mut FnScopes, scope: ScopeId) -> Option { + fn compute_cond_scopes( + cond: ast::Condition, + scopes: &mut FnScopes, + scope: ScopeId, + ) -> Option { if let Some(expr) = cond.expr() { compute_expr_scopes(expr, scopes, scope); } @@ -236,14 +254,18 @@ fn compute_expr_scopes(expr: ast::Expr, scopes: &mut FnScopes, scope: ScopeId) { #[derive(Debug)] struct ScopeData { parent: Option, - entries: Vec + entries: Vec, } -pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> Option<&'a ScopeEntry> { +pub fn resolve_local_name<'a>( + name_ref: ast::NameRef, + scopes: &'a FnScopes, +) -> Option<&'a ScopeEntry> { use rustc_hash::FxHashSet; let mut shadowed = FxHashSet::default(); - let ret = scopes.scope_chain(name_ref.syntax()) + let ret = scopes + .scope_chain(name_ref.syntax()) .flat_map(|scope| scopes.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) .filter(|entry| entry.name() == name_ref.text()) @@ -255,8 +277,8 @@ pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> O #[cfg(test)] mod tests { use super::*; - use ra_syntax::File; use crate::{find_node_at_offset, test_utils::extract_offset}; + use ra_syntax::File; fn do_check(code: &str, expected: &[&str]) { let (off, code) = extract_offset(code); @@ -272,7 +294,8 @@ mod tests { let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap(); let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap(); let scopes = FnScopes::new(fn_def); - let actual = scopes.scope_chain(marker.syntax()) + let actual = scopes + .scope_chain(marker.syntax()) .flat_map(|scope| scopes.entries(scope)) .map(|it| it.name()) .collect::>(); @@ -281,7 +304,8 @@ mod tests { #[test] fn test_lambda_scope() { - do_check(r" + do_check( + r" fn quux(foo: i32) { let f = |bar, baz: i32| { <|> @@ -293,7 +317,8 @@ mod tests { #[test] fn test_call_scope() { - do_check(r" + do_check( + r" fn quux() { f(|x| <|> ); }", @@ -303,7 +328,8 @@ mod tests { #[test] fn test_metod_call_scope() { - do_check(r" + do_check( + r" fn quux() { z.f(|x| <|> ); }", @@ -313,7 +339,8 @@ mod tests { #[test] fn test_loop_scope() { - do_check(r" + do_check( + r" fn quux() { loop { let x = (); @@ -326,7 +353,8 @@ mod tests { #[test] fn test_match() { - do_check(r" + do_check( + r" fn quux() { match () { Some(x) => { @@ -340,7 +368,8 @@ mod tests { #[test] fn test_shadow_variable() { - do_check(r" + do_check( + r" fn foo(x: String) { let x : &str = &x<|>; }", @@ -356,14 +385,20 @@ mod tests { let scopes = FnScopes::new(fn_def); - let local_name = resolve_local_name(name_ref, &scopes).unwrap().ast().name().unwrap(); - let expected_name = find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); + let local_name = resolve_local_name(name_ref, &scopes) + .unwrap() + .ast() + .name() + .unwrap(); + let expected_name = + find_node_at_offset::(file.syntax(), expected_offset.into()).unwrap(); assert_eq!(local_name.syntax().range(), expected_name.syntax().range()); } #[test] fn test_resolve_local_name() { - do_check_local_name(r#" + do_check_local_name( + r#" fn foo(x: i32, y: u32) { { let z = x * 2; @@ -372,25 +407,30 @@ mod tests { let t = x<|> * 3; } }"#, - 21); + 21, + ); } #[test] fn test_resolve_local_name_declaration() { - do_check_local_name(r#" + do_check_local_name( + r#" fn foo(x: String) { let x : &str = &x<|>; }"#, - 21); + 21, + ); } #[test] fn test_resolve_local_name_shadow() { - do_check_local_name(r" + do_check_local_name( + r" fn foo(x: String) { let x : &str = &x; x<|> }", - 46); + 46, + ); } } diff --git a/crates/ra_editor/src/scope/mod.rs b/crates/ra_editor/src/scope/mod.rs index 7d6d530f7..cc2d49392 100644 --- a/crates/ra_editor/src/scope/mod.rs +++ b/crates/ra_editor/src/scope/mod.rs @@ -2,7 +2,6 @@ mod fn_scope; mod mod_scope; pub use self::{ - fn_scope::{FnScopes, resolve_local_name}, + fn_scope::{resolve_local_name, FnScopes}, mod_scope::ModuleScope, }; - diff --git a/crates/ra_editor/src/scope/mod_scope.rs b/crates/ra_editor/src/scope/mod_scope.rs index d2a3e7c58..8d7e408f8 100644 --- a/crates/ra_editor/src/scope/mod_scope.rs +++ b/crates/ra_editor/src/scope/mod_scope.rs @@ -1,6 +1,6 @@ use ra_syntax::{ - AstNode, SyntaxNode, SyntaxNodeRef, SmolStr, ast::{self, AstChildren}, + AstNode, SmolStr, SyntaxNode, SyntaxNodeRef, }; pub struct ModuleScope { @@ -13,7 +13,8 @@ pub struct Entry { } enum EntryKind { - Item, Import, + Item, + Import, } impl ModuleScope { @@ -34,9 +35,8 @@ impl ModuleScope { collect_imports(tree, &mut entries); } continue; - }, - ast::ModuleItem::ExternCrateItem(_) | - ast::ModuleItem::ImplItem(_) => continue, + } + ast::ModuleItem::ExternCrateItem(_) | ast::ModuleItem::ImplItem(_) => continue, }; entries.extend(entry) } @@ -52,20 +52,22 @@ impl ModuleScope { impl Entry { fn new<'a>(item: impl ast::NameOwner<'a>) -> Option { let name = item.name()?; - Some(Entry { node: name.syntax().owned(), kind: EntryKind::Item }) + Some(Entry { + node: name.syntax().owned(), + kind: EntryKind::Item, + }) } fn new_import(path: ast::Path) -> Option { let name_ref = path.segment()?.name_ref()?; - Some(Entry { node: name_ref.syntax().owned(), kind: EntryKind::Import }) + Some(Entry { + node: name_ref.syntax().owned(), + kind: EntryKind::Import, + }) } pub fn name(&self) -> SmolStr { match self.kind { - EntryKind::Item => - ast::Name::cast(self.node.borrowed()).unwrap() - .text(), - EntryKind::Import => - ast::NameRef::cast(self.node.borrowed()).unwrap() - .text(), + EntryKind::Item => ast::Name::cast(self.node.borrowed()).unwrap().text(), + EntryKind::Import => ast::NameRef::cast(self.node.borrowed()).unwrap().text(), } } pub fn syntax(&self) -> SyntaxNodeRef { @@ -75,32 +77,31 @@ impl Entry { fn collect_imports(tree: ast::UseTree, acc: &mut Vec) { if let Some(use_tree_list) = tree.use_tree_list() { - return use_tree_list.use_trees().for_each(|it| collect_imports(it, acc)); + return use_tree_list + .use_trees() + .for_each(|it| collect_imports(it, acc)); } if let Some(path) = tree.path() { acc.extend(Entry::new_import(path)); } } - #[cfg(test)] mod tests { use super::*; - use ra_syntax::{File, ast::ModuleItemOwner}; + use ra_syntax::{ast::ModuleItemOwner, File}; fn do_check(code: &str, expected: &[&str]) { let file = File::parse(&code); let scope = ModuleScope::new(file.ast().items()); - let actual = scope.entries - .iter() - .map(|it| it.name()) - .collect::>(); + let actual = scope.entries.iter().map(|it| it.name()).collect::>(); assert_eq!(expected, actual.as_slice()); } #[test] fn test_module_scope() { - do_check(" + do_check( + " struct Foo; enum Bar {} mod baz {} @@ -110,6 +111,8 @@ mod tests { t, }; type T = (); - ", &["Foo", "Bar", "baz", "quux", "z", "t", "T"]) + ", + &["Foo", "Bar", "baz", "quux", "z", "t", "T"], + ) } } -- cgit v1.2.3