From fc91c3f6d634423bd0da7efb33294687a413fc5a Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 7 Mar 2023 00:58:56 +0800 Subject: [PATCH] Rename confusing `SyntaxKind::is_*` --- crates/ide/src/ide/assists/pack_bindings.rs | 2 +- .../src/ide/assists/remove_empty_let_in.rs | 7 ++++- crates/syntax/src/ast.rs | 4 +-- crates/syntax/src/kind.rs | 26 +++++++++++++------ crates/syntax/src/lib.rs | 2 +- crates/syntax/src/parser.rs | 4 +-- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/crates/ide/src/ide/assists/pack_bindings.rs b/crates/ide/src/ide/assists/pack_bindings.rs index 3a63859..659cec3 100644 --- a/crates/ide/src/ide/assists/pack_bindings.rs +++ b/crates/ide/src/ide/assists/pack_bindings.rs @@ -87,7 +87,7 @@ pub(super) fn pack_bindings(ctx: &mut AssistsCtx<'_>) -> Option<()> { let trivia_start = std::iter::successors(path_value.syntax().first_token(), |tok| tok.prev_token()) .skip(1) - .take_while(|tok| tok.kind().is_whitespace()) + .take_while(|tok| tok.kind().is_trivia()) .last() .map_or_else( || path_value.syntax().text_range().start(), diff --git a/crates/ide/src/ide/assists/remove_empty_let_in.rs b/crates/ide/src/ide/assists/remove_empty_let_in.rs index e830647..f1553c0 100644 --- a/crates/ide/src/ide/assists/remove_empty_let_in.rs +++ b/crates/ide/src/ide/assists/remove_empty_let_in.rs @@ -23,7 +23,7 @@ pub(super) fn remove_empty_let_in(ctx: &mut AssistsCtx<'_>) -> Option<()> { let last_token = node .in_token()? .next_token() - .filter(|tok| tok.kind().is_whitespace()) + .filter(|tok| tok.kind().is_space()) .or(node.in_token())?; let range = node @@ -59,4 +59,9 @@ mod tests { check_no("let foo = 42;$0 in foo"); check_no("{ foo = let bar = 42;$0 in bar; }"); } + + #[test] + fn keep_comment() { + check("let in$0/*hello*/{ }", expect!["/*hello*/{ }"]); + } } diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index 620de39..f7e988a 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs @@ -297,14 +297,14 @@ asts! { self.0 .children_with_tokens() .filter_map(|it| it.into_token()) - .find(|it| !it.kind().is_whitespace()) + .find(|it| !it.kind().is_trivia()) .filter(|tok| tok.kind() == T![let]) } pub fn rec_token(&self) -> Option { self.0 .children_with_tokens() .filter_map(|it| it.into_token()) - .find(|it| !it.kind().is_whitespace()) + .find(|it| !it.kind().is_trivia()) .filter(|tok| tok.kind() == T![rec]) } }, diff --git a/crates/syntax/src/kind.rs b/crates/syntax/src/kind.rs index 2cd84d8..f8c31d9 100644 --- a/crates/syntax/src/kind.rs +++ b/crates/syntax/src/kind.rs @@ -82,8 +82,8 @@ def! { KW_THEN = [then], KW_WITH = [with] @KEYWORD_LAST, - // Symbols len=1. - AT = [@] @SYMBOL_FIRST, + // Punctuations len=1. + AT = [@] @PUNCT_FIRST, BANG = [!], COLON = [:], COMMA = [,], @@ -105,7 +105,7 @@ def! { SLASH = [/], STAR = [*], - // Symbols len=2. + // Punctuations len=2. AND2 = [&&], DOLLAR_L_CURLY = ["${"], EQ2 = [==], @@ -118,8 +118,8 @@ def! { QUOTE2 = ["''"], SLASH2 = ["//"], - // Symbols len=3. - DOT3 = [...] @SYMBOL_LAST, + // Punctuations len=3. + DOT3 = [...] @PUNCT_LAST, // Literals and identifiers. FLOAT, @@ -171,19 +171,29 @@ def! { } impl SyntaxKind { + /// Returns whether this is a SPACE. #[inline(always)] - pub fn is_whitespace(self) -> bool { + pub fn is_space(self) -> bool { + matches!(self, Self::SPACE) + } + + /// Returns whether this is a COMMENT or SPACE. + #[inline(always)] + pub fn is_trivia(self) -> bool { matches!(self, Self::COMMENT | Self::SPACE) } + /// Returns whether this is a keyword. + /// Contextual keywords are not considered keywords outside expected contexts. #[inline(always)] pub fn is_keyword(self) -> bool { (Self::KEYWORD_FIRST as u8..=Self::KEYWORD_LAST as u8).contains(&(self as u8)) } + /// Returns whether this is a punctuation, including operators and delimiters. #[inline(always)] - pub fn is_symbol(self) -> bool { - (Self::SYMBOL_FIRST as u8..=Self::SYMBOL_LAST as u8).contains(&(self as u8)) + pub fn is_punct(self) -> bool { + (Self::PUNCT_FIRST as u8..=Self::PUNCT_LAST as u8).contains(&(self as u8)) } } diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 6817735..3bb02e5 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -139,7 +139,7 @@ pub fn best_token_at_offset(node: &SyntaxNode, offset: TextSize) -> Option 2, SyntaxKind::STRING_ESCAPE => 3, - k if k.is_symbol() => 4, + k if k.is_punct() => 4, k if k.is_keyword() => 5, // IDENT, INT, and etc. _ => 6, diff --git a/crates/syntax/src/parser.rs b/crates/syntax/src/parser.rs index b6b80b5..b925fdc 100644 --- a/crates/syntax/src/parser.rs +++ b/crates/syntax/src/parser.rs @@ -148,12 +148,12 @@ impl<'i> Parser<'i> { .iter() .rev() .map(|&(k, _)| k) - .filter(|k| !k.is_whitespace()) + .filter(|k| !k.is_trivia()) } /// Consumes all following whitespaces if any. fn ws(&mut self) { - while matches!(self.peek(), Some(k) if k.is_whitespace()) { + while matches!(self.peek(), Some(k) if k.is_trivia()) { self.bump(); } }