diff options
author | pommicket <pommicket@gmail.com> | 2022-09-30 19:12:19 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-09-30 19:12:19 -0400 |
commit | 29ab7b5f53d1a394a9b4781d6d7ebb3f47370c55 (patch) | |
tree | d20d56d697c9c4b260db54e7a5124adcd08a7f87 | |
parent | a9a882c362296f393d5b52e19abb48f81f2b0581 (diff) |
better rust syntax highlighting
-rw-r--r-- | syntax.c | 37 |
1 files changed, 35 insertions, 2 deletions
@@ -326,7 +326,9 @@ static void syntax_highlight_rust(SyntaxState *state, char32_t const *line, u32 bool in_string = (*state & SYNTAX_STATE_RUST_STRING) != 0; bool string_is_raw = (*state & SYNTAX_STATE_RUST_STRING_IS_RAW) != 0; bool in_number = false; - uint backslashes = 0; + bool in_attribute = false; + int backslashes = 0; + int bracket_depth = 0; for (u32 i = 0; i < line_len; ++i) { char32_t c = line[i]; @@ -426,10 +428,39 @@ static void syntax_highlight_rust(SyntaxState *state, char32_t const *line, u32 } } break; + case '[': + if (in_attribute && !in_string && !comment_depth) { + ++bracket_depth; + } + break; + case ']': + if (in_attribute && !in_string && !comment_depth) { + --bracket_depth; + if (bracket_depth < 0) { + in_attribute = false; + } + } + break; + case '#': + if (!in_string && !comment_depth) { + if (i && line[i-1] == 'r') { + // raw identifier -- not an attribute + break; + } + if (!has_2_chars) break; + if (line[i+1] == '[' || (line[i+1] == '!' && line[i+2] == '[')) { + in_attribute = true; + bracket_depth = 0; + } + } + break; default: { if ((i && is32_ident(line[i - 1])) || !is32_ident(c)) break; // can't be a keyword on its own. - + if (i >= 2 && line[i-2] == 'r' && line[i-1] == '#') { + // raw identifier + break; + } if (char_types && !in_string && !comment_depth && !in_number) { u32 keyword_len = syntax_keyword_len(LANG_RUST, line, i, line_len); Keyword const *keyword = syntax_keyword_lookup(syntax_all_keywords_rust, arr_count(syntax_all_keywords_rust), @@ -458,6 +489,8 @@ static void syntax_highlight_rust(SyntaxState *state, char32_t const *line, u32 type = SYNTAX_STRING; } else if (in_number) { type = SYNTAX_CONSTANT; + } else if (in_attribute) { + type = SYNTAX_PREPROCESSOR; } char_types[i] = type; } |