Skip to content

Commit

Permalink
Improved Rust syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
vallentin committed Jul 8, 2023
1 parent f7aa1d3 commit 15db646
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 24 additions & 13 deletions colorblast/src/lexers/rust.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem;

use super::{impl_iter, IntoSimpleToken, SimpleTokenIter, Token, TokenSpan};

const KEYWORDS_CONTROL_FLOW: &[&str] = &[
Expand All @@ -10,6 +12,8 @@ const PRIMITIVE_TYPES: &[&str] = &[
"u16", "u32", "u64", "u128", "unit", "usize",
];

const VARIANTS: &[&str] = &["Some", "None", "Ok", "Err"];

impl IntoSimpleToken for any_lexer::RustToken {
#[inline]
fn into_simple_token(self) -> Token {
Expand Down Expand Up @@ -82,19 +86,17 @@ impl<'code> RustLexer<'code> {
_ => {}
},
RustLexerState::InUse => {}
RustLexerState::NextIsFnName => {
self.state = RustLexerState::None;

if tok == Token::Var {
tok = Token::Var2;
return Some((tok, span));
}
}
RustLexerState::NextIsMacroName => {
self.state = RustLexerState::None;

RustLexerState::NextIsFnName
| RustLexerState::NextIsMacroName
| RustLexerState::NextIsModName => {
let state = mem::replace(&mut self.state, RustLexerState::None);
if tok == Token::Var {
tok = Token::Var4;
let tok = match state {
RustLexerState::NextIsFnName => Token::Var2,
RustLexerState::NextIsMacroName => Token::Var4,
RustLexerState::NextIsModName => Token::Var3,
_ => unreachable!(),
};
return Some((tok, span));
}
}
Expand Down Expand Up @@ -123,18 +125,26 @@ impl<'code> RustLexer<'code> {
Token::Keyword if span.as_str() == "use" => {
self.state = RustLexerState::InUse;
}
Token::Keyword if span.as_str() == "mod" => {
self.state = RustLexerState::NextIsModName;
}
Token::Keyword if KEYWORDS_CONTROL_FLOW.contains(&span.as_str()) => {
tok = Token::Keyword2;
}
Token::Var if PRIMITIVE_TYPES.contains(&span.as_str()) => {
tok = Token::Var3;
}
Token::Var if VARIANTS.contains(&span.as_str()) => {
tok = Token::Var5;
}
Token::Var | Token::Keyword => {
if (tok == Token::Keyword) && (span.as_str() == "fn") {
self.state = RustLexerState::NextIsFnName;
}

if (tok == Token::Var)
if (tok == Token::Var) && !span.as_str().contains(char::is_lowercase) {
tok = Token::Var5;
} else if (tok == Token::Var)
&& span
.as_str()
.chars()
Expand Down Expand Up @@ -189,4 +199,5 @@ enum RustLexerState {
InUse,
NextIsFnName,
NextIsMacroName,
NextIsModName,
}
2 changes: 2 additions & 0 deletions colorblast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl_enum_token!(
Var2,
Var3,
Var4,
Var5,
Keyword,
Keyword2,
Operator,
Expand All @@ -96,6 +97,7 @@ impl StylizeToken for Token {
Self::Var2 => Style::new().fg((220, 220, 170)),
Self::Var3 => Style::new().fg((78, 201, 176)),
Self::Var4 => Style::new().fg((86, 156, 214)),
Self::Var5 => Style::new().fg((79, 193, 255)),
Self::Keyword => Style::new().fg((86, 156, 214)),
Self::Keyword2 => Style::new().fg((197, 134, 192)),
Self::Operator => Style::new().fg((212, 212, 212)),
Expand Down

0 comments on commit 15db646

Please sign in to comment.