From 08ea2271e8050165d0aaf4c994ed3dd746aff3ba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 12:06:38 +0200 Subject: Rename TypeRef -> Type The TypeRef name comes from IntelliJ days, where you often have both type *syntax* as well as *semantical* representation of types in scope. And naming both Type is confusing. In rust-analyzer however, we use ast types as `ast::Type`, and have many more semantic counterparts to ast types, so avoiding name clash here is just confusing. --- crates/ra_syntax/src/ast.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_syntax/src/ast.rs') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index fd426ece9..8a0e3d27b 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -287,7 +287,7 @@ where assert!(pred.for_token().is_none()); assert!(pred.generic_param_list().is_none()); - assert_eq!("T", pred.type_ref().unwrap().syntax().text().to_string()); + assert_eq!("T", pred.ty().unwrap().syntax().text().to_string()); assert_bound("Clone", bounds.next()); assert_bound("Copy", bounds.next()); assert_bound("Debug", bounds.next()); @@ -304,20 +304,20 @@ where let pred = predicates.next().unwrap(); let mut bounds = pred.type_bound_list().unwrap().bounds(); - assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string()); + assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string()); assert_bound("'a", bounds.next()); let pred = predicates.next().unwrap(); let mut bounds = pred.type_bound_list().unwrap().bounds(); - assert_eq!("Iterator::Item", pred.type_ref().unwrap().syntax().text().to_string()); + assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string()); assert_bound("Debug", bounds.next()); assert_bound("'a", bounds.next()); let pred = predicates.next().unwrap(); let mut bounds = pred.type_bound_list().unwrap().bounds(); - assert_eq!("::Item", pred.type_ref().unwrap().syntax().text().to_string()); + assert_eq!("::Item", pred.ty().unwrap().syntax().text().to_string()); assert_bound("Debug", bounds.next()); assert_bound("'a", bounds.next()); @@ -326,6 +326,6 @@ where assert!(pred.for_token().is_some()); assert_eq!("<'a>", pred.generic_param_list().unwrap().syntax().text().to_string()); - assert_eq!("F", pred.type_ref().unwrap().syntax().text().to_string()); + assert_eq!("F", pred.ty().unwrap().syntax().text().to_string()); assert_bound("Fn(&'a str)", bounds.next()); } -- cgit v1.2.3 From a7ca6583fbce6f1bddce7b31ad5bb1fc0665b616 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Jul 2020 15:40:48 +0200 Subject: Handwrite Stmt --- crates/ra_syntax/src/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast.rs') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 8a0e3d27b..d536bb1e7 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -17,7 +17,7 @@ use crate::{ pub use self::{ expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, - generated::{nodes::*, tokens::*}, + generated::*, node_ext::{ AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, StructKind, TypeBoundKind, VisibilityKind, -- cgit v1.2.3 From a1c187eef3ba08076aedb5154929f7eda8d1b424 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:26:51 +0200 Subject: Rename ra_syntax -> syntax --- crates/ra_syntax/src/ast.rs | 331 -------------------------------------------- 1 file changed, 331 deletions(-) delete mode 100644 crates/ra_syntax/src/ast.rs (limited to 'crates/ra_syntax/src/ast.rs') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs deleted file mode 100644 index d536bb1e7..000000000 --- a/crates/ra_syntax/src/ast.rs +++ /dev/null @@ -1,331 +0,0 @@ -//! Abstract Syntax Tree, layered on top of untyped `SyntaxNode`s - -mod generated; -mod traits; -mod token_ext; -mod node_ext; -mod expr_ext; -pub mod edit; -pub mod make; - -use std::marker::PhantomData; - -use crate::{ - syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken}, - SmolStr, SyntaxKind, -}; - -pub use self::{ - expr_ext::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, - generated::*, - node_ext::{ - AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, - StructKind, TypeBoundKind, VisibilityKind, - }, - token_ext::*, - traits::*, -}; - -/// The main trait to go from untyped `SyntaxNode` to a typed ast. The -/// conversion itself has zero runtime cost: ast and syntax nodes have exactly -/// the same representation: a pointer to the tree root and a pointer to the -/// node itself. -pub trait AstNode { - fn can_cast(kind: SyntaxKind) -> bool - where - Self: Sized; - - fn cast(syntax: SyntaxNode) -> Option - where - Self: Sized; - - fn syntax(&self) -> &SyntaxNode; -} - -/// Like `AstNode`, but wraps tokens rather than interior nodes. -pub trait AstToken { - fn can_cast(token: SyntaxKind) -> bool - where - Self: Sized; - - fn cast(syntax: SyntaxToken) -> Option - where - Self: Sized; - - fn syntax(&self) -> &SyntaxToken; - - fn text(&self) -> &SmolStr { - self.syntax().text() - } -} - -/// An iterator over `SyntaxNode` children of a particular AST type. -#[derive(Debug, Clone)] -pub struct AstChildren { - inner: SyntaxNodeChildren, - ph: PhantomData, -} - -impl AstChildren { - fn new(parent: &SyntaxNode) -> Self { - AstChildren { inner: parent.children(), ph: PhantomData } - } -} - -impl Iterator for AstChildren { - type Item = N; - fn next(&mut self) -> Option { - self.inner.find_map(N::cast) - } -} - -mod support { - use super::{AstChildren, AstNode, SyntaxKind, SyntaxNode, SyntaxToken}; - - pub(super) fn child(parent: &SyntaxNode) -> Option { - parent.children().find_map(N::cast) - } - - pub(super) fn children(parent: &SyntaxNode) -> AstChildren { - AstChildren::new(parent) - } - - pub(super) fn token(parent: &SyntaxNode, kind: SyntaxKind) -> Option { - parent.children_with_tokens().filter_map(|it| it.into_token()).find(|it| it.kind() == kind) - } -} - -#[test] -fn assert_ast_is_object_safe() { - fn _f(_: &dyn AstNode, _: &dyn NameOwner) {} -} - -#[test] -fn test_doc_comment_none() { - let file = SourceFile::parse( - r#" - // non-doc - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert!(module.doc_comment_text().is_none()); -} - -#[test] -fn test_doc_comment_of_items() { - let file = SourceFile::parse( - r#" - //! doc - // non-doc - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("doc", module.doc_comment_text().unwrap()); -} - -#[test] -fn test_doc_comment_of_statics() { - let file = SourceFile::parse( - r#" - /// Number of levels - static LEVELS: i32 = 0; - "#, - ) - .ok() - .unwrap(); - let st = file.syntax().descendants().find_map(Static::cast).unwrap(); - assert_eq!("Number of levels", st.doc_comment_text().unwrap()); -} - -#[test] -fn test_doc_comment_preserves_indents() { - let file = SourceFile::parse( - r#" - /// doc1 - /// ``` - /// fn foo() { - /// // ... - /// } - /// ``` - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("doc1\n```\nfn foo() {\n // ...\n}\n```", module.doc_comment_text().unwrap()); -} - -#[test] -fn test_doc_comment_preserves_newlines() { - let file = SourceFile::parse( - r#" - /// this - /// is - /// mod - /// foo - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("this\nis\nmod\nfoo", module.doc_comment_text().unwrap()); -} - -#[test] -fn test_doc_comment_single_line_block_strips_suffix() { - let file = SourceFile::parse( - r#" - /** this is mod foo*/ - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("this is mod foo", module.doc_comment_text().unwrap()); -} - -#[test] -fn test_doc_comment_single_line_block_strips_suffix_whitespace() { - let file = SourceFile::parse( - r#" - /** this is mod foo */ - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("this is mod foo ", module.doc_comment_text().unwrap()); -} - -#[test] -fn test_doc_comment_multi_line_block_strips_suffix() { - let file = SourceFile::parse( - r#" - /** - this - is - mod foo - */ - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!( - " this\n is\n mod foo\n ", - module.doc_comment_text().unwrap() - ); -} - -#[test] -fn test_comments_preserve_trailing_whitespace() { - let file = SourceFile::parse( - "\n/// Representation of a Realm. \n/// In the specification these are called Realm Records.\nstruct Realm {}", - ) - .ok() - .unwrap(); - let def = file.syntax().descendants().find_map(Struct::cast).unwrap(); - assert_eq!( - "Representation of a Realm. \nIn the specification these are called Realm Records.", - def.doc_comment_text().unwrap() - ); -} - -#[test] -fn test_four_slash_line_comment() { - let file = SourceFile::parse( - r#" - //// too many slashes to be a doc comment - /// doc comment - mod foo {} - "#, - ) - .ok() - .unwrap(); - let module = file.syntax().descendants().find_map(Module::cast).unwrap(); - assert_eq!("doc comment", module.doc_comment_text().unwrap()); -} - -#[test] -fn test_where_predicates() { - fn assert_bound(text: &str, bound: Option) { - assert_eq!(text, bound.unwrap().syntax().text().to_string()); - } - - let file = SourceFile::parse( - r#" -fn foo() -where - T: Clone + Copy + Debug + 'static, - 'a: 'b + 'c, - Iterator::Item: 'a + Debug, - Iterator::Item: Debug + 'a, - ::Item: Debug + 'a, - for<'a> F: Fn(&'a str) -{} - "#, - ) - .ok() - .unwrap(); - let where_clause = file.syntax().descendants().find_map(WhereClause::cast).unwrap(); - - let mut predicates = where_clause.predicates(); - - let pred = predicates.next().unwrap(); - let mut bounds = pred.type_bound_list().unwrap().bounds(); - - assert!(pred.for_token().is_none()); - assert!(pred.generic_param_list().is_none()); - assert_eq!("T", pred.ty().unwrap().syntax().text().to_string()); - assert_bound("Clone", bounds.next()); - assert_bound("Copy", bounds.next()); - assert_bound("Debug", bounds.next()); - assert_bound("'static", bounds.next()); - - let pred = predicates.next().unwrap(); - let mut bounds = pred.type_bound_list().unwrap().bounds(); - - assert_eq!("'a", pred.lifetime_token().unwrap().text()); - - assert_bound("'b", bounds.next()); - assert_bound("'c", bounds.next()); - - let pred = predicates.next().unwrap(); - let mut bounds = pred.type_bound_list().unwrap().bounds(); - - assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string()); - assert_bound("'a", bounds.next()); - - let pred = predicates.next().unwrap(); - let mut bounds = pred.type_bound_list().unwrap().bounds(); - - assert_eq!("Iterator::Item", pred.ty().unwrap().syntax().text().to_string()); - assert_bound("Debug", bounds.next()); - assert_bound("'a", bounds.next()); - - let pred = predicates.next().unwrap(); - let mut bounds = pred.type_bound_list().unwrap().bounds(); - - assert_eq!("::Item", pred.ty().unwrap().syntax().text().to_string()); - assert_bound("Debug", bounds.next()); - assert_bound("'a", bounds.next()); - - let pred = predicates.next().unwrap(); - let mut bounds = pred.type_bound_list().unwrap().bounds(); - - assert!(pred.for_token().is_some()); - assert_eq!("<'a>", pred.generic_param_list().unwrap().syntax().text().to_string()); - assert_eq!("F", pred.ty().unwrap().syntax().text().to_string()); - assert_bound("Fn(&'a str)", bounds.next()); -} -- cgit v1.2.3