aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:23:34 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:23:34 +0000
commit1e0948a509e8f6ec7cbb5e2ef77669325fee0637 (patch)
tree81b546c105388e1fc3154c90a42a8cc5fb930d0b
parent3f4be819125ce4a22edd86721fa56b5caba99c2e (diff)
parentfa6e0b0d38d2a030b959be91232927b9c096272b (diff)
Merge #453
453: itroduce trait for ast tokens r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_analysis/src/imp.rs2
-rw-r--r--crates/ra_analysis/src/symbol_index.rs2
-rw-r--r--crates/ra_editor/src/assists/add_impl.rs2
-rw-r--r--crates/ra_hir/src/name.rs4
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_syntax/src/ast.rs52
-rw-r--r--crates/ra_syntax/src/ast/generated.rs7
-rw-r--r--crates/ra_syntax/src/grammar.ron14
-rw-r--r--crates/ra_syntax/src/validation/byte.rs2
-rw-r--r--crates/ra_syntax/src/validation/byte_string.rs2
-rw-r--r--crates/ra_syntax/src/validation/char.rs2
-rw-r--r--crates/ra_syntax/src/validation/string.rs2
12 files changed, 34 insertions, 59 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 8ac430e41..98554dd4c 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -115,7 +115,7 @@ impl db::RootDatabase {
115 let name = ast_module.name().unwrap(); 115 let name = ast_module.name().unwrap();
116 Ok(vec![NavigationTarget { 116 Ok(vec![NavigationTarget {
117 file_id, 117 file_id,
118 name: name.text(), 118 name: name.text().clone(),
119 range: name.syntax().range(), 119 range: name.syntax().range(),
120 kind: MODULE, 120 kind: MODULE,
121 ptr: None, 121 ptr: None,
diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs
index ed1796756..8dd15b40e 100644
--- a/crates/ra_analysis/src/symbol_index.rs
+++ b/crates/ra_analysis/src/symbol_index.rs
@@ -205,7 +205,7 @@ pub(crate) struct FileSymbol {
205 205
206fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> { 206fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> {
207 fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> { 207 fn decl<N: NameOwner>(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> {
208 let name = node.name()?.text(); 208 let name = node.name()?.text().clone();
209 let ptr = LocalSyntaxPtr::new(node.syntax()); 209 let ptr = LocalSyntaxPtr::new(node.syntax());
210 Some((name, ptr)) 210 Some((name, ptr))
211 } 211 }
diff --git a/crates/ra_editor/src/assists/add_impl.rs b/crates/ra_editor/src/assists/add_impl.rs
index 9353e2717..2eda7cae2 100644
--- a/crates/ra_editor/src/assists/add_impl.rs
+++ b/crates/ra_editor/src/assists/add_impl.rs
@@ -1,6 +1,6 @@
1use join_to_string::join; 1use join_to_string::join;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, AstNode, NameOwner, TypeParamsOwner}, 3 ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner},
4 TextUnit, 4 TextUnit,
5}; 5};
6 6
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
index dee1c9c5c..3e6ce8b95 100644
--- a/crates/ra_hir/src/name.rs
+++ b/crates/ra_hir/src/name.rs
@@ -76,13 +76,13 @@ pub(crate) trait AsName {
76 76
77impl AsName for ast::NameRef { 77impl AsName for ast::NameRef {
78 fn as_name(&self) -> Name { 78 fn as_name(&self) -> Name {
79 Name::new(self.text()) 79 Name::new(self.text().clone())
80 } 80 }
81} 81}
82 82
83impl AsName for ast::Name { 83impl AsName for ast::Name {
84 fn as_name(&self) -> Name { 84 fn as_name(&self) -> Name {
85 Name::new(self.text()) 85 Name::new(self.text().clone())
86 } 86 }
87} 87}
88 88
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 82675c0e4..59a803761 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -177,7 +177,7 @@ pub fn macro_symbols(
177 if let Some(name) = trait_def.name() { 177 if let Some(name) = trait_def.name() {
178 let dst_range = name.syntax().range(); 178 let dst_range = name.syntax().range();
179 if let Some(src_range) = exp.map_range_back(dst_range) { 179 if let Some(src_range) = exp.map_range_back(dst_range) {
180 res.push((name.text(), src_range + off)) 180 res.push((name.text().clone(), src_range + off))
181 } 181 }
182 } 182 }
183 } 183 }
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 0e303ee98..96879ae5a 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -23,6 +23,12 @@ pub trait AstNode: rowan::TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>
23 fn to_owned(&self) -> TreePtr<Self>; 23 fn to_owned(&self) -> TreePtr<Self>;
24} 24}
25 25
26pub trait AstToken: AstNode {
27 fn text(&self) -> &SmolStr {
28 self.syntax().leaf_text().unwrap()
29 }
30}
31
26pub trait NameOwner: AstNode { 32pub trait NameOwner: AstNode {
27 fn name(&self) -> Option<&Name> { 33 fn name(&self) -> Option<&Name> {
28 child_opt(self) 34 child_opt(self)
@@ -155,41 +161,7 @@ impl Attr {
155 } 161 }
156} 162}
157 163
158impl Lifetime {
159 pub fn text(&self) -> SmolStr {
160 self.syntax().leaf_text().unwrap().clone()
161 }
162}
163
164impl Char {
165 pub fn text(&self) -> &SmolStr {
166 &self.syntax().leaf_text().unwrap()
167 }
168}
169
170impl Byte {
171 pub fn text(&self) -> &SmolStr {
172 &self.syntax().leaf_text().unwrap()
173 }
174}
175
176impl ByteString {
177 pub fn text(&self) -> &SmolStr {
178 &self.syntax().leaf_text().unwrap()
179 }
180}
181
182impl String {
183 pub fn text(&self) -> &SmolStr {
184 &self.syntax().leaf_text().unwrap()
185 }
186}
187
188impl Comment { 164impl Comment {
189 pub fn text(&self) -> &SmolStr {
190 self.syntax().leaf_text().unwrap()
191 }
192
193 pub fn flavor(&self) -> CommentFlavor { 165 pub fn flavor(&self) -> CommentFlavor {
194 let text = self.text(); 166 let text = self.text();
195 if text.starts_with("///") { 167 if text.starts_with("///") {
@@ -248,10 +220,6 @@ impl CommentFlavor {
248} 220}
249 221
250impl Whitespace { 222impl Whitespace {
251 pub fn text(&self) -> &SmolStr {
252 &self.syntax().leaf_text().unwrap()
253 }
254
255 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> { 223 pub fn count_newlines_lazy(&self) -> impl Iterator<Item = &()> {
256 self.text().chars().filter(|&c| c == '\n').map(|_| &()) 224 self.text().chars().filter(|&c| c == '\n').map(|_| &())
257 } 225 }
@@ -262,16 +230,16 @@ impl Whitespace {
262} 230}
263 231
264impl Name { 232impl Name {
265 pub fn text(&self) -> SmolStr { 233 pub fn text(&self) -> &SmolStr {
266 let ident = self.syntax().first_child().unwrap(); 234 let ident = self.syntax().first_child().unwrap();
267 ident.leaf_text().unwrap().clone() 235 ident.leaf_text().unwrap()
268 } 236 }
269} 237}
270 238
271impl NameRef { 239impl NameRef {
272 pub fn text(&self) -> SmolStr { 240 pub fn text(&self) -> &SmolStr {
273 let ident = self.syntax().first_child().unwrap(); 241 let ident = self.syntax().first_child().unwrap();
274 ident.leaf_text().unwrap().clone() 242 ident.leaf_text().unwrap()
275 } 243 }
276} 244}
277 245
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 5e96ab142..547e3c003 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -288,6 +288,7 @@ impl AstNode for Byte {
288} 288}
289 289
290 290
291impl ast::AstToken for Byte {}
291impl Byte {} 292impl Byte {}
292 293
293// ByteString 294// ByteString
@@ -312,6 +313,7 @@ impl AstNode for ByteString {
312} 313}
313 314
314 315
316impl ast::AstToken for ByteString {}
315impl ByteString {} 317impl ByteString {}
316 318
317// CallExpr 319// CallExpr
@@ -397,6 +399,7 @@ impl AstNode for Char {
397} 399}
398 400
399 401
402impl ast::AstToken for Char {}
400impl Char {} 403impl Char {}
401 404
402// Comment 405// Comment
@@ -421,6 +424,7 @@ impl AstNode for Comment {
421} 424}
422 425
423 426
427impl ast::AstToken for Comment {}
424impl Comment {} 428impl Comment {}
425 429
426// Condition 430// Condition
@@ -1270,6 +1274,7 @@ impl AstNode for Lifetime {
1270} 1274}
1271 1275
1272 1276
1277impl ast::AstToken for Lifetime {}
1273impl Lifetime {} 1278impl Lifetime {}
1274 1279
1275// LifetimeParam 1280// LifetimeParam
@@ -2766,6 +2771,7 @@ impl AstNode for String {
2766} 2771}
2767 2772
2768 2773
2774impl ast::AstToken for String {}
2769impl String {} 2775impl String {}
2770 2776
2771// StructDef 2777// StructDef
@@ -3391,5 +3397,6 @@ impl AstNode for Whitespace {
3391} 3397}
3392 3398
3393 3399
3400impl ast::AstToken for Whitespace {}
3394impl Whitespace {} 3401impl Whitespace {}
3395 3402
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index d7505ea06..bddd96a5c 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -424,10 +424,10 @@ Grammar(
424 "PrefixExpr": (options: ["Expr"]), 424 "PrefixExpr": (options: ["Expr"]),
425 "RangeExpr": (), 425 "RangeExpr": (),
426 "BinExpr": (), 426 "BinExpr": (),
427 "String": (), 427 "String": ( traits: ["AstToken"] ),
428 "Byte": (), 428 "Byte": ( traits: ["AstToken"] ),
429 "ByteString": (), 429 "ByteString": ( traits: ["AstToken"] ),
430 "Char": (), 430 "Char": ( traits: ["AstToken"] ),
431 "Literal": (), 431 "Literal": (),
432 432
433 "Expr": ( 433 "Expr": (
@@ -505,7 +505,7 @@ Grammar(
505 ), 505 ),
506 "TypeParam": ( traits: ["NameOwner"] ), 506 "TypeParam": ( traits: ["NameOwner"] ),
507 "LifetimeParam": ( options: [ "Lifetime" ] ), 507 "LifetimeParam": ( options: [ "Lifetime" ] ),
508 "Lifetime": (), 508 "Lifetime": ( traits: ["AstToken"] ),
509 "WhereClause": (), 509 "WhereClause": (),
510 "ExprStmt": ( 510 "ExprStmt": (
511 options: [ ["expr", "Expr"] ] 511 options: [ ["expr", "Expr"] ]
@@ -562,7 +562,7 @@ Grammar(
562 "PathSegment": ( 562 "PathSegment": (
563 options: [ "NameRef" ] 563 options: [ "NameRef" ]
564 ), 564 ),
565 "Comment": (), 565 "Comment": ( traits: ["AstToken"] ),
566 "Whitespace": (), 566 "Whitespace": ( traits: ["AstToken"] ),
567 }, 567 },
568) 568)
diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs
index 4deb302a7..9ab4b18a3 100644
--- a/crates/ra_syntax/src/validation/byte.rs
+++ b/crates/ra_syntax/src/validation/byte.rs
@@ -1,7 +1,7 @@
1//! Validation of byte literals 1//! Validation of byte literals
2 2
3use crate::{ 3use crate::{
4 ast::{self, AstNode}, 4 ast::{self, AstNode, AstToken},
5 string_lexing::{self, StringComponentKind}, 5 string_lexing::{self, StringComponentKind},
6 TextRange, 6 TextRange,
7 validation::char, 7 validation::char,
diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs
index 670c43a09..cd41a0a68 100644
--- a/crates/ra_syntax/src/validation/byte_string.rs
+++ b/crates/ra_syntax/src/validation/byte_string.rs
@@ -1,5 +1,5 @@
1use crate::{ 1use crate::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode, AstToken},
3 string_lexing::{self, StringComponentKind}, 3 string_lexing::{self, StringComponentKind},
4 yellow::{ 4 yellow::{
5 SyntaxError, 5 SyntaxError,
diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs
index 9cbd43fba..169c88f56 100644
--- a/crates/ra_syntax/src/validation/char.rs
+++ b/crates/ra_syntax/src/validation/char.rs
@@ -5,7 +5,7 @@ use std::u32;
5use arrayvec::ArrayString; 5use arrayvec::ArrayString;
6 6
7use crate::{ 7use crate::{
8 ast::{self, AstNode}, 8 ast::{self, AstNode, AstToken},
9 string_lexing::{self, StringComponentKind}, 9 string_lexing::{self, StringComponentKind},
10 TextRange, 10 TextRange,
11 yellow::{ 11 yellow::{
diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs
index 7b2a68d12..cb86b765f 100644
--- a/crates/ra_syntax/src/validation/string.rs
+++ b/crates/ra_syntax/src/validation/string.rs
@@ -1,5 +1,5 @@
1use crate::{ 1use crate::{
2 ast::{self, AstNode}, 2 ast::{self, AstNode, AstToken},
3 string_lexing, 3 string_lexing,
4 yellow::{ 4 yellow::{
5 SyntaxError, 5 SyntaxError,