1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-22 02:55:39 +03:00

Rename confusing SyntaxKind::is_*

This commit is contained in:
oxalica 2023-03-07 00:58:56 +08:00
parent bb9a748cce
commit fc91c3f6d6
6 changed files with 30 additions and 15 deletions

View File

@ -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(),

View File

@ -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*/{ }"]);
}
}

View File

@ -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<SyntaxToken> {
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])
}
},

View File

@ -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))
}
}

View File

@ -139,7 +139,7 @@ pub fn best_token_at_offset(node: &SyntaxNode, offset: TextSize) -> Option<Synta
| SyntaxKind::PATH_FRAGMENT
| SyntaxKind::STRING_FRAGMENT => 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,

View File

@ -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();
}
}