diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/expr_extensions.rs | 49 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 4 |
2 files changed, 26 insertions, 27 deletions
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 6da4b1309..539759450 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs | |||
@@ -305,44 +305,39 @@ impl ast::Literal { | |||
305 | .unwrap() | 305 | .unwrap() |
306 | } | 306 | } |
307 | 307 | ||
308 | fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option<SmolStr> { | ||
309 | possible_suffixes | ||
310 | .iter() | ||
311 | .find(|&suffix| text.ends_with(suffix)) | ||
312 | .map(|&suffix| SmolStr::new(suffix)) | ||
313 | } | ||
314 | |||
308 | pub fn kind(&self) -> LiteralKind { | 315 | pub fn kind(&self) -> LiteralKind { |
309 | match self.token().kind() { | 316 | const INT_SUFFIXES: [&'static str; 12] = [ |
310 | INT_NUMBER => { | 317 | "u64", "u32", "u16", "u8", "usize", "isize", "i64", "i32", "i16", "i8", "u128", "i128", |
311 | let int_suffix_list = [ | 318 | ]; |
312 | "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", | 319 | const FLOAT_SUFFIXES: [&'static str; 2] = ["f32", "f64"]; |
313 | "u16", "u8", | ||
314 | ]; | ||
315 | 320 | ||
321 | let token = self.token(); | ||
322 | |||
323 | match token.kind() { | ||
324 | INT_NUMBER => { | ||
325 | // FYI: there was a bug here previously, thus an if statement bellow is necessary. | ||
316 | // The lexer treats e.g. `1f64` as an integer literal. See | 326 | // The lexer treats e.g. `1f64` as an integer literal. See |
317 | // https://github.com/rust-analyzer/rust-analyzer/issues/1592 | 327 | // https://github.com/rust-analyzer/rust-analyzer/issues/1592 |
318 | // and the comments on the linked PR. | 328 | // and the comments on the linked PR. |
319 | let float_suffix_list = ["f32", "f64"]; | ||
320 | |||
321 | let text = self.token().text().to_string(); | ||
322 | 329 | ||
323 | let float_suffix = float_suffix_list | 330 | let text = token.text(); |
324 | .iter() | ||
325 | .find(|&s| text.ends_with(s)) | ||
326 | .map(|&suf| SmolStr::new(suf)); | ||
327 | 331 | ||
328 | if float_suffix.is_some() { | 332 | if let suffix @ Some(_) = Self::find_suffix(&text, &FLOAT_SUFFIXES) { |
329 | LiteralKind::FloatNumber { suffix: float_suffix } | 333 | LiteralKind::FloatNumber { suffix } |
330 | } else { | 334 | } else { |
331 | let suffix = int_suffix_list | 335 | LiteralKind::IntNumber { suffix: Self::find_suffix(&text, &INT_SUFFIXES) } |
332 | .iter() | ||
333 | .find(|&s| text.ends_with(s)) | ||
334 | .map(|&suf| SmolStr::new(suf)); | ||
335 | LiteralKind::IntNumber { suffix } | ||
336 | } | 336 | } |
337 | } | 337 | } |
338 | FLOAT_NUMBER => { | 338 | FLOAT_NUMBER => { |
339 | let allowed_suffix_list = ["f64", "f32"]; | 339 | let text = token.text(); |
340 | let text = self.token().text().to_string(); | 340 | LiteralKind::FloatNumber { suffix: Self::find_suffix(&text, &FLOAT_SUFFIXES) } |
341 | let suffix = allowed_suffix_list | ||
342 | .iter() | ||
343 | .find(|&s| text.ends_with(s)) | ||
344 | .map(|&suf| SmolStr::new(suf)); | ||
345 | LiteralKind::FloatNumber { suffix } | ||
346 | } | 341 | } |
347 | STRING | RAW_STRING => LiteralKind::String, | 342 | STRING | RAW_STRING => LiteralKind::String, |
348 | T![true] | T![false] => LiteralKind::Bool, | 343 | T![true] | T![false] => LiteralKind::Bool, |
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index d9666cdca..cb0aee422 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs | |||
@@ -234,6 +234,10 @@ impl ast::LetStmt { | |||
234 | Some(node) => node.kind() == T![;], | 234 | Some(node) => node.kind() == T![;], |
235 | } | 235 | } |
236 | } | 236 | } |
237 | |||
238 | pub fn eq_token(&self) -> Option<SyntaxToken> { | ||
239 | self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token()) | ||
240 | } | ||
237 | } | 241 | } |
238 | 242 | ||
239 | impl ast::ExprStmt { | 243 | impl ast::ExprStmt { |