]> git.lizzy.rs Git - rust.git/commitdiff
Merge #2134 #2137
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Wed, 30 Oct 2019 21:04:58 +0000 (21:04 +0000)
committerGitHub <noreply@github.com>
Wed, 30 Oct 2019 21:04:58 +0000 (21:04 +0000)
2134: More match ast r=matklad a=kjeremy

Use `match_ast!` in more places

2137: Add link to the vscode VIM extension compatibility warning. r=matklad a=krk

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/1831

Co-authored-by: kjeremy <kjeremy@gmail.com>
Co-authored-by: krk <keremkat@gmail.com>
crates/ra_hir/src/source_binder.rs
crates/ra_ide_api/src/runnables.rs
crates/ra_syntax/src/ast/traits.rs
xtask/src/main.rs

index 01f51ba5d4707e18f4132769372c849819cc1989..152bc71bd28772fb135d53dfc7e64f48d025809e 100644 (file)
@@ -12,7 +12,7 @@
 use ra_db::FileId;
 use ra_syntax::{
     ast::{self, AstNode},
-    AstPtr,
+    match_ast, AstPtr,
     SyntaxKind::*,
     SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
 };
@@ -37,24 +37,34 @@ fn try_get_resolver_for_node(
     file_id: FileId,
     node: &SyntaxNode,
 ) -> Option<Resolver> {
-    if let Some(module) = ast::Module::cast(node.clone()) {
-        let src = crate::Source { file_id: file_id.into(), ast: module };
-        Some(crate::Module::from_declaration(db, src)?.resolver(db))
-    } else if let Some(file) = ast::SourceFile::cast(node.clone()) {
-        let src =
-            crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(file) };
-        Some(crate::Module::from_definition(db, src)?.resolver(db))
-    } else if let Some(s) = ast::StructDef::cast(node.clone()) {
-        let src = crate::Source { file_id: file_id.into(), ast: s };
-        Some(Struct::from_source(db, src)?.resolver(db))
-    } else if let Some(e) = ast::EnumDef::cast(node.clone()) {
-        let src = crate::Source { file_id: file_id.into(), ast: e };
-        Some(Enum::from_source(db, src)?.resolver(db))
-    } else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
-        Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db))
-    } else {
-        // FIXME add missing cases
-        None
+    match_ast! {
+        match node {
+            ast::Module(it) => {
+                let src = crate::Source { file_id: file_id.into(), ast: it };
+                Some(crate::Module::from_declaration(db, src)?.resolver(db))
+            },
+             ast::SourceFile(it) => {
+                let src =
+                    crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(it) };
+                Some(crate::Module::from_definition(db, src)?.resolver(db))
+            },
+            ast::StructDef(it) => {
+                let src = crate::Source { file_id: file_id.into(), ast: it };
+                Some(Struct::from_source(db, src)?.resolver(db))
+            },
+            ast::EnumDef(it) => {
+                let src = crate::Source { file_id: file_id.into(), ast: it };
+                Some(Enum::from_source(db, src)?.resolver(db))
+            },
+            _ => {
+                if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
+                    Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db))
+                } else {
+                    // FIXME add missing cases
+                    None
+                }
+            },
+        }
     }
 }
 
@@ -68,16 +78,14 @@ fn def_with_body_from_child_node(
     let ctx = LocationCtx::new(db, module.id, file_id.into());
 
     node.ancestors().find_map(|node| {
-        if let Some(def) = ast::FnDef::cast(node.clone()) {
-            return Some(Function { id: ctx.to_def(&def) }.into());
-        }
-        if let Some(def) = ast::ConstDef::cast(node.clone()) {
-            return Some(Const { id: ctx.to_def(&def) }.into());
-        }
-        if let Some(def) = ast::StaticDef::cast(node) {
-            return Some(Static { id: ctx.to_def(&def) }.into());
+        match_ast! {
+            match node {
+                ast::FnDef(def)  => { Some(Function {id: ctx.to_def(&def) }.into()) },
+                ast::ConstDef(def) => { Some(Const { id: ctx.to_def(&def) }.into()) },
+                ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) },
+                _ => { None },
+            }
         }
-        None
     })
 }
 
index 910883da744272e35f2aecc5e899d89cdeee9409..1b5c8deea0f88a2b5a771ad5d6c859efb6c6aba0 100644 (file)
@@ -4,7 +4,7 @@
 use ra_db::SourceDatabase;
 use ra_syntax::{
     ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner},
-    SyntaxNode, TextRange,
+    match_ast, SyntaxNode, TextRange,
 };
 
 use crate::{db::RootDatabase, FileId};
@@ -29,12 +29,12 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
 }
 
 fn runnable(db: &RootDatabase, file_id: FileId, item: SyntaxNode) -> Option<Runnable> {
-    if let Some(fn_def) = ast::FnDef::cast(item.clone()) {
-        runnable_fn(fn_def)
-    } else if let Some(m) = ast::Module::cast(item) {
-        runnable_mod(db, file_id, m)
-    } else {
-        None
+    match_ast! {
+        match item {
+            ast::FnDef(it) => { runnable_fn(it) },
+            ast::Module(it) => { runnable_mod(db, file_id, it) },
+            _ => { None },
+        }
     }
 }
 
index 76313684eaba16f43574d690dcd2553b6111714b..c2b005886a97f58b5beea1b46eb43dbf7db7ebf3 100644 (file)
@@ -6,6 +6,7 @@
 
 use crate::{
     ast::{self, child_opt, children, AstChildren, AstNode, AstToken},
+    match_ast,
     syntax_node::{SyntaxElementChildren, SyntaxNodeChildren},
 };
 
@@ -68,11 +69,12 @@ impl Iterator for ItemOrMacroIter {
     fn next(&mut self) -> Option<ItemOrMacro> {
         loop {
             let n = self.0.next()?;
-            if let Some(item) = ast::ModuleItem::cast(n.clone()) {
-                return Some(ItemOrMacro::Item(item));
-            }
-            if let Some(call) = ast::MacroCall::cast(n) {
-                return Some(ItemOrMacro::Macro(call));
+            match_ast! {
+                match n {
+                    ast::ModuleItem(it) => { return Some(ItemOrMacro::Item(it)) },
+                    ast::MacroCall(it) => { return Some(ItemOrMacro::Macro(it)) },
+                    _ => {},
+                }
             }
         }
     }
index abcbf7129b57fdd508432d9b869e5c56c81e7724..e04e45f157529c5f14d2639345467597b7d9bc2b 100644 (file)
@@ -2,7 +2,7 @@
 //!
 //! This binary defines various auxiliary build commands, which are not
 //! expressible with just `cargo`. Notably, it provides `cargo xtask codegen`
-//! for code genetaiont and `cargo xtask install` for installation of
+//! for code generation and `cargo xtask install` for installation of
 //! rust-analyzer server and client.
 //!
 //! This binary is integrated into the `cargo` command line by using an alias in