aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
committerSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
commitb21d9337d9200e2cfdc90b386591c72c302dc03e (patch)
treef81f5c08f821115cee26fa4d3ceaae88c7807fd5 /crates/ra_syntax/src
parent18a0937585b836ec5ed054b9ae48e0156ab6d9ef (diff)
parentce07a2daa9e53aa86a769f8641b14c2878444fbc (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo.rs12
-rw-r--r--crates/ra_syntax/src/ast/edit.rs4
-rw-r--r--crates/ra_syntax/src/ast/generated.rs34
-rw-r--r--crates/ra_syntax/src/ast/make.rs3
-rw-r--r--crates/ra_syntax/src/grammar.ron16
-rw-r--r--crates/ra_syntax/src/ptr.rs14
-rw-r--r--crates/ra_syntax/src/syntax_node.rs2
7 files changed, 68 insertions, 17 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index 1c075082a..e4061e994 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -140,13 +140,13 @@ pub fn insert_children(
140 }); 140 });
141 141
142 let new_children = match &position { 142 let new_children = match &position {
143 InsertPosition::First => to_insert.chain(old_children).collect::<Box<[_]>>(), 143 InsertPosition::First => to_insert.chain(old_children).collect::<Vec<_>>(),
144 InsertPosition::Last => old_children.chain(to_insert).collect::<Box<[_]>>(), 144 InsertPosition::Last => old_children.chain(to_insert).collect::<Vec<_>>(),
145 InsertPosition::Before(anchor) | InsertPosition::After(anchor) => { 145 InsertPosition::Before(anchor) | InsertPosition::After(anchor) => {
146 let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 }; 146 let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 };
147 let split_at = position_of_child(parent, anchor.clone()) + take_anchor; 147 let split_at = position_of_child(parent, anchor.clone()) + take_anchor;
148 let before = old_children.by_ref().take(split_at).collect::<Vec<_>>(); 148 let before = old_children.by_ref().take(split_at).collect::<Vec<_>>();
149 before.into_iter().chain(to_insert).chain(old_children).collect::<Box<[_]>>() 149 before.into_iter().chain(to_insert).chain(old_children).collect::<Vec<_>>()
150 } 150 }
151 }; 151 };
152 152
@@ -174,7 +174,7 @@ pub fn replace_children(
174 .into_iter() 174 .into_iter()
175 .chain(to_insert.map(to_green_element)) 175 .chain(to_insert.map(to_green_element))
176 .chain(old_children.skip(end + 1 - start)) 176 .chain(old_children.skip(end + 1 - start))
177 .collect::<Box<[_]>>(); 177 .collect::<Vec<_>>();
178 with_children(parent, new_children) 178 with_children(parent, new_children)
179} 179}
180 180
@@ -187,7 +187,7 @@ pub fn replace_descendants(
187 map: &FxHashMap<SyntaxElement, SyntaxElement>, 187 map: &FxHashMap<SyntaxElement, SyntaxElement>,
188) -> SyntaxNode { 188) -> SyntaxNode {
189 // FIXME: this could be made much faster. 189 // FIXME: this could be made much faster.
190 let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Box<[_]>>(); 190 let new_children = parent.children_with_tokens().map(|it| go(map, it)).collect::<Vec<_>>();
191 return with_children(parent, new_children); 191 return with_children(parent, new_children);
192 192
193 fn go( 193 fn go(
@@ -211,7 +211,7 @@ pub fn replace_descendants(
211 211
212fn with_children( 212fn with_children(
213 parent: &SyntaxNode, 213 parent: &SyntaxNode,
214 new_children: Box<[NodeOrToken<rowan::GreenNode, rowan::GreenToken>]>, 214 new_children: Vec<NodeOrToken<rowan::GreenNode, rowan::GreenToken>>,
215) -> SyntaxNode { 215) -> SyntaxNode {
216 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>(); 216 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>();
217 let new_node = 217 let new_node =
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 95bf9db14..ae5d63927 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -104,7 +104,7 @@ impl ast::ItemList {
104 } 104 }
105 }; 105 };
106 106
107 let indent = leading_indent(self.syntax()).unwrap_or("".into()); 107 let indent = leading_indent(self.syntax()).unwrap_or_default();
108 let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); 108 let ws = tokens::WsBuilder::new(&format!("\n{}", indent));
109 let to_insert = iter::once(ws.ws().into()); 109 let to_insert = iter::once(ws.ws().into());
110 match existing_ws { 110 match existing_ws {
@@ -133,7 +133,7 @@ impl ast::RecordFieldList {
133 let space = if is_multiline { 133 let space = if is_multiline {
134 ws = tokens::WsBuilder::new(&format!( 134 ws = tokens::WsBuilder::new(&format!(
135 "\n{} ", 135 "\n{} ",
136 leading_indent(self.syntax()).unwrap_or("".into()) 136 leading_indent(self.syntax()).unwrap_or_default()
137 )); 137 ));
138 ws.ws() 138 ws.ws()
139 } else { 139 } else {
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index c06076e3d..9f9d6e63c 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -312,6 +312,7 @@ impl AstNode for Block {
312 } 312 }
313} 313}
314impl ast::AttrsOwner for Block {} 314impl ast::AttrsOwner for Block {}
315impl ast::ModuleItemOwner for Block {}
315impl Block { 316impl Block {
316 pub fn statements(&self) -> AstChildren<Stmt> { 317 pub fn statements(&self) -> AstChildren<Stmt> {
317 AstChildren::new(&self.syntax) 318 AstChildren::new(&self.syntax)
@@ -550,6 +551,36 @@ impl ConstDef {
550 } 551 }
551} 552}
552#[derive(Debug, Clone, PartialEq, Eq, Hash)] 553#[derive(Debug, Clone, PartialEq, Eq, Hash)]
554pub struct ConstParam {
555 pub(crate) syntax: SyntaxNode,
556}
557impl AstNode for ConstParam {
558 fn can_cast(kind: SyntaxKind) -> bool {
559 match kind {
560 CONST_PARAM => true,
561 _ => false,
562 }
563 }
564 fn cast(syntax: SyntaxNode) -> Option<Self> {
565 if Self::can_cast(syntax.kind()) {
566 Some(Self { syntax })
567 } else {
568 None
569 }
570 }
571 fn syntax(&self) -> &SyntaxNode {
572 &self.syntax
573 }
574}
575impl ast::NameOwner for ConstParam {}
576impl ast::AttrsOwner for ConstParam {}
577impl ast::TypeAscriptionOwner for ConstParam {}
578impl ConstParam {
579 pub fn default_val(&self) -> Option<Expr> {
580 AstChildren::new(&self.syntax).next()
581 }
582}
583#[derive(Debug, Clone, PartialEq, Eq, Hash)]
553pub struct ContinueExpr { 584pub struct ContinueExpr {
554 pub(crate) syntax: SyntaxNode, 585 pub(crate) syntax: SyntaxNode,
555} 586}
@@ -1425,6 +1456,9 @@ impl LambdaExpr {
1425 pub fn param_list(&self) -> Option<ParamList> { 1456 pub fn param_list(&self) -> Option<ParamList> {
1426 AstChildren::new(&self.syntax).next() 1457 AstChildren::new(&self.syntax).next()
1427 } 1458 }
1459 pub fn ret_type(&self) -> Option<RetType> {
1460 AstChildren::new(&self.syntax).next()
1461 }
1428 pub fn body(&self) -> Option<Expr> { 1462 pub fn body(&self) -> Option<Expr> {
1429 AstChildren::new(&self.syntax).next() 1463 AstChildren::new(&self.syntax).next()
1430 } 1464 }
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index 40db570da..04a5408fe 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -168,8 +168,7 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
168 168
169fn ast_from_text<N: AstNode>(text: &str) -> N { 169fn ast_from_text<N: AstNode>(text: &str) -> N {
170 let parse = SourceFile::parse(text); 170 let parse = SourceFile::parse(text);
171 let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); 171 parse.tree().syntax().descendants().find_map(N::cast).unwrap()
172 res
173} 172}
174 173
175pub mod tokens { 174pub mod tokens {
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index d1be40abe..08aafb610 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -94,7 +94,8 @@ Grammar(
94 "return", 94 "return",
95 "try", 95 "try",
96 "box", 96 "box",
97 "await" 97 "await",
98 "macro"
98 ], 99 ],
99 contextual_keywords: [ 100 contextual_keywords: [
100 "auto", 101 "auto",
@@ -140,6 +141,7 @@ Grammar(
140 "TYPE_ALIAS_DEF", 141 "TYPE_ALIAS_DEF",
141 "MACRO_CALL", 142 "MACRO_CALL",
142 "TOKEN_TREE", 143 "TOKEN_TREE",
144 "MACRO_DEF",
143 145
144 "PAREN_TYPE", 146 "PAREN_TYPE",
145 "TUPLE_TYPE", 147 "TUPLE_TYPE",
@@ -243,6 +245,7 @@ Grammar(
243 "TYPE_PARAM_LIST", 245 "TYPE_PARAM_LIST",
244 "LIFETIME_PARAM", 246 "LIFETIME_PARAM",
245 "TYPE_PARAM", 247 "TYPE_PARAM",
248 "CONST_PARAM",
246 "TYPE_ARG_LIST", 249 "TYPE_ARG_LIST",
247 "LIFETIME_ARG", 250 "LIFETIME_ARG",
248 "TYPE_ARG", 251 "TYPE_ARG",
@@ -426,7 +429,7 @@ Grammar(
426 "PathExpr": (options: ["Path"]), 429 "PathExpr": (options: ["Path"]),
427 "LambdaExpr": ( 430 "LambdaExpr": (
428 options: [ 431 options: [
429 "ParamList", 432 "ParamList", "RetType",
430 ["body", "Expr"], 433 ["body", "Expr"],
431 ] 434 ]
432 ), 435 ),
@@ -602,6 +605,10 @@ Grammar(
602 options: [("default_type", "TypeRef")], 605 options: [("default_type", "TypeRef")],
603 traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"], 606 traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"],
604 ), 607 ),
608 "ConstParam": (
609 options: [("default_val", "Expr")],
610 traits: ["NameOwner", "AttrsOwner", "TypeAscriptionOwner"],
611 ),
605 "LifetimeParam": ( 612 "LifetimeParam": (
606 traits: ["AttrsOwner"], 613 traits: ["AttrsOwner"],
607 ), 614 ),
@@ -653,6 +660,7 @@ Grammar(
653 ], 660 ],
654 traits: [ 661 traits: [
655 "AttrsOwner", 662 "AttrsOwner",
663 "ModuleItemOwner",
656 ] 664 ]
657 ), 665 ),
658 "ParamList": ( 666 "ParamList": (
@@ -664,14 +672,14 @@ Grammar(
664 "SelfParam": ( 672 "SelfParam": (
665 traits: [ 673 traits: [
666 "TypeAscriptionOwner", 674 "TypeAscriptionOwner",
667 "AttrsOwner", 675 "AttrsOwner",
668 ] 676 ]
669 ), 677 ),
670 "Param": ( 678 "Param": (
671 options: [ "Pat" ], 679 options: [ "Pat" ],
672 traits: [ 680 traits: [
673 "TypeAscriptionOwner", 681 "TypeAscriptionOwner",
674 "AttrsOwner", 682 "AttrsOwner",
675 ] 683 ]
676 ), 684 ),
677 "UseItem": ( 685 "UseItem": (
diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs
index e049fce61..db6230aab 100644
--- a/crates/ra_syntax/src/ptr.rs
+++ b/crates/ra_syntax/src/ptr.rs
@@ -1,6 +1,10 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use std::{iter::successors, marker::PhantomData}; 3use std::{
4 hash::{Hash, Hasher},
5 iter::successors,
6 marker::PhantomData,
7};
4 8
5use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange}; 9use crate::{AstNode, SyntaxKind, SyntaxNode, TextRange};
6 10
@@ -43,7 +47,7 @@ impl SyntaxNodePtr {
43} 47}
44 48
45/// Like `SyntaxNodePtr`, but remembers the type of node 49/// Like `SyntaxNodePtr`, but remembers the type of node
46#[derive(Debug, Hash)] 50#[derive(Debug)]
47pub struct AstPtr<N: AstNode> { 51pub struct AstPtr<N: AstNode> {
48 raw: SyntaxNodePtr, 52 raw: SyntaxNodePtr,
49 _ty: PhantomData<fn() -> N>, 53 _ty: PhantomData<fn() -> N>,
@@ -64,6 +68,12 @@ impl<N: AstNode> PartialEq for AstPtr<N> {
64 } 68 }
65} 69}
66 70
71impl<N: AstNode> Hash for AstPtr<N> {
72 fn hash<H: Hasher>(&self, state: &mut H) {
73 self.raw.hash(state)
74 }
75}
76
67impl<N: AstNode> AstPtr<N> { 77impl<N: AstNode> AstPtr<N> {
68 pub fn new(node: &N) -> AstPtr<N> { 78 pub fn new(node: &N) -> AstPtr<N> {
69 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } 79 AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index b2f5b8c64..041c6ea8d 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -40,7 +40,7 @@ pub use rowan::{Direction, NodeOrToken};
40 40
41pub struct SyntaxTreeBuilder { 41pub struct SyntaxTreeBuilder {
42 errors: Vec<SyntaxError>, 42 errors: Vec<SyntaxError>,
43 inner: GreenNodeBuilder, 43 inner: GreenNodeBuilder<'static>,
44} 44}
45 45
46impl Default for SyntaxTreeBuilder { 46impl Default for SyntaxTreeBuilder {