1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-23 03:57:06 +03:00

Fix highlighting of or

This commit is contained in:
oxalica 2022-09-20 16:25:45 +08:00
parent a16525ec85
commit d31baa16cb
5 changed files with 22 additions and 13 deletions

View File

@ -102,7 +102,6 @@ pub(crate) fn highlight(
T![...] => HlTag::Punct(HlPunct::Ellipsis),
T![if] | T![then] | T![else] => HlTag::Keyword(HlKeyword::Conditional),
// FIXME: `or` is always a keyword currently.
T![or] => HlTag::Keyword(HlKeyword::Operator),
T![assert] | T![in] | T![inherit] | T![let] | T![rec] | T![with] => {
HlTag::Keyword(HlKeyword::Other)
@ -188,8 +187,12 @@ mod tests {
#[test]
fn keyword() {
check("$0if 1 then 2 else 3", expect!["Keyword(Conditional)"]);
check("a.b $0or c", expect!["Keyword(Operator)"]);
check("$0let in 1", expect!["Keyword(Other)"]);
check("a.b $0or c", expect!["Keyword(Operator)"]);
// Contextual keywords.
check("let or = 1; in a $0or", expect!["NameRef(LetIn)"]);
check("{ $0or = 1; }", expect!["NameDef(PlainAttrset)"]);
}
#[test]

View File

@ -110,6 +110,12 @@ impl<'i> Parser<'i> {
self.builder.token(kind.into(), &self.src[range]);
}
/// Same with `bump`, but override the kind.
fn bump_with_kind(&mut self, kind: SyntaxKind) {
let (_, range) = self.tokens.pop().unwrap();
self.builder.token(kind.into(), &self.src[range]);
}
/// Peek the next token, including whitespaces.
fn peek_full(&mut self) -> Option<(SyntaxKind, TextRange)> {
self.steps += 1;
@ -417,7 +423,7 @@ impl<'i> Parser<'i> {
} else if self.peek_non_ws() == Some(T![or]) {
self.start_node_at(cp, APPLY);
self.start_node(REF);
self.bump(); // or
self.bump_with_kind(SyntaxKind::IDENT);
self.finish_node();
self.finish_node();
}
@ -678,7 +684,7 @@ impl<'i> Parser<'i> {
match self.peek_non_ws() {
Some(IDENT | T![or]) => {
self.start_node(NAME);
self.bump();
self.bump_with_kind(SyntaxKind::IDENT);
self.finish_node();
}
Some(T!["${"]) => self.dynamic(),

View File

@ -15,14 +15,14 @@ SOURCE_FILE@0..21
SPACE@8..9 " "
APPLY@9..11
REF@9..11
KW_OR@9..11 "or"
IDENT@9..11 "or"
SPACE@11..12 " "
PLUS@12..13 "+"
SPACE@13..14 " "
APPLY@14..21
APPLY@14..16
REF@14..16
KW_OR@14..16 "or"
IDENT@14..16 "or"
SPACE@16..17 " "
REF@17..20
IDENT@17..20 "foo"

View File

@ -9,7 +9,7 @@ SOURCE_FILE@0..24
IDENT@1..2 "a"
SPACE@2..3 " "
REF@3..5
KW_OR@3..5 "or"
IDENT@3..5 "or"
SPACE@5..6 " "
KW_OR@6..8 "or"
R_PAREN@8..9 ")"
@ -22,7 +22,7 @@ SOURCE_FILE@0..24
IDENT@12..13 "a"
SPACE@13..14 " "
REF@14..16
KW_OR@14..16 "or"
IDENT@14..16 "or"
SPACE@16..17 " "
KW_OR@17..19 "or"
SPACE@19..20 " "

View File

@ -5,7 +5,7 @@ SOURCE_FILE@0..50
ATTR_PATH_VALUE@4..11
ATTR_PATH@4..7
NAME@4..6
KW_OR@4..6 "or"
IDENT@4..6 "or"
SPACE@6..7 " "
EQ@7..8 "="
SPACE@8..9 " "
@ -27,7 +27,7 @@ SOURCE_FILE@0..50
DOT@20..21 "."
ATTR_PATH@21..24
NAME@21..23
KW_OR@21..23 "or"
IDENT@21..23 "or"
SPACE@23..24 " "
KW_OR@24..26 "or"
APPLY@26..31
@ -36,14 +36,14 @@ SOURCE_FILE@0..50
INT@27..28 "1"
SPACE@28..29 " "
REF@29..31
KW_OR@29..31 "or"
IDENT@29..31 "or"
SPACE@31..32 " "
APPLY@32..36
LITERAL@32..33
INT@32..33 "2"
SPACE@33..34 " "
REF@34..36
KW_OR@34..36 "or"
IDENT@34..36 "or"
SPACE@36..37 " "
R_BRACK@37..38 "]"
SPACE@38..39 " "
@ -59,7 +59,7 @@ SOURCE_FILE@0..50
IDENT@43..44 "a"
SPACE@44..45 " "
REF@45..47
KW_OR@45..47 "or"
IDENT@45..47 "or"
SPACE@47..48 " "
REF@48..49
IDENT@48..49 "b"