Skip to content

Commit 1b22ca7

Browse files
committed
support protobuf highlight
1 parent ae176b2 commit 1b22ca7

File tree

3 files changed

+531
-2
lines changed

3 files changed

+531
-2
lines changed

crates/emmylua_parser_desc/src/lang/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod json;
22
mod lua;
3+
mod protobuf;
34
mod shell;
45
mod sql;
56
mod vimscript;
@@ -10,8 +11,8 @@ use crate::{
1011
DescItemKind,
1112
lang::{
1213
json::process_json_code_block, lua::process_lua_code_block,
13-
shell::process_shell_code_block, sql::process_sql_code_block,
14-
vimscript::process_vimscript_code_block,
14+
protobuf::process_protobuf_code_block, shell::process_shell_code_block,
15+
sql::process_sql_code_block, vimscript::process_vimscript_code_block,
1516
},
1617
util::ResultContainer,
1718
};
@@ -24,6 +25,7 @@ pub enum CodeBlockLang {
2425
Json,
2526
Shell,
2627
Sql,
28+
Protobuf,
2729
Other,
2830
}
2931

@@ -36,6 +38,7 @@ impl CodeBlockLang {
3638
"json" | "Json" => Some(CodeBlockLang::Json),
3739
"shell" | "Shell" => Some(CodeBlockLang::Shell),
3840
"sql" | "Sql" => Some(CodeBlockLang::Sql),
41+
"Protobuf" | "protobuf" => Some(CodeBlockLang::Protobuf),
3942
_ => Some(CodeBlockLang::Other),
4043
}
4144
}
@@ -54,6 +57,7 @@ pub fn process_code<'a, C: ResultContainer>(
5457
CodeBlockLang::Json => process_json_code_block(c, reader, state),
5558
CodeBlockLang::Shell => process_shell_code_block(c, reader, state),
5659
CodeBlockLang::Sql => process_sql_code_block(c, reader, state),
60+
CodeBlockLang::Protobuf => process_protobuf_code_block(c, reader, state),
5761
_ => {
5862
c.emit_range(range, DescItemKind::CodeBlock);
5963
return state;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
mod protobuf_lexer;
2+
3+
use emmylua_parser::{LexerState, Reader};
4+
5+
use crate::{
6+
CodeBlockHighlightKind, DescItemKind,
7+
lang::protobuf::protobuf_lexer::{ProtobufLexer, ProtobufTokenData, ProtobufTokenKind},
8+
util::ResultContainer,
9+
};
10+
11+
pub fn process_protobuf_code_block<'a, C: ResultContainer>(
12+
c: &mut C,
13+
line_reader: Reader<'a>,
14+
state: LexerState,
15+
) -> LexerState {
16+
let mut lexer = ProtobufLexer::new_with_state(line_reader, state);
17+
let tokens = lexer.tokenize();
18+
for token in tokens {
19+
if !matches!(
20+
token.kind,
21+
ProtobufTokenKind::TkEof | ProtobufTokenKind::TkWhitespace
22+
) {
23+
let highlight_kind = to_highlight_kind(&token);
24+
if highlight_kind != CodeBlockHighlightKind::None {
25+
c.emit_range(token.range, DescItemKind::CodeBlockHl(highlight_kind));
26+
}
27+
}
28+
}
29+
30+
lexer.get_state()
31+
}
32+
33+
fn to_highlight_kind(token: &ProtobufTokenData) -> CodeBlockHighlightKind {
34+
match token.kind {
35+
ProtobufTokenKind::TkKeyword => CodeBlockHighlightKind::Keyword,
36+
ProtobufTokenKind::TkType => CodeBlockHighlightKind::Class,
37+
ProtobufTokenKind::TkString => CodeBlockHighlightKind::String,
38+
ProtobufTokenKind::TkNumber | ProtobufTokenKind::TkFloat => CodeBlockHighlightKind::Number,
39+
ProtobufTokenKind::TkLineComment | ProtobufTokenKind::TkBlockComment => {
40+
CodeBlockHighlightKind::Comment
41+
}
42+
ProtobufTokenKind::TkIdentifier => CodeBlockHighlightKind::Variable,
43+
ProtobufTokenKind::TkSemicolon
44+
| ProtobufTokenKind::TkComma
45+
| ProtobufTokenKind::TkDot
46+
| ProtobufTokenKind::TkEquals
47+
| ProtobufTokenKind::TkLeftParen
48+
| ProtobufTokenKind::TkRightParen
49+
| ProtobufTokenKind::TkLeftBrace
50+
| ProtobufTokenKind::TkRightBrace
51+
| ProtobufTokenKind::TkLeftBracket
52+
| ProtobufTokenKind::TkRightBracket
53+
| ProtobufTokenKind::TkLeftAngle
54+
| ProtobufTokenKind::TkRightAngle
55+
| ProtobufTokenKind::TkColon
56+
| ProtobufTokenKind::TkSlash
57+
| ProtobufTokenKind::TkMinus
58+
| ProtobufTokenKind::TkPlus => CodeBlockHighlightKind::Operators,
59+
_ => CodeBlockHighlightKind::None,
60+
}
61+
}

0 commit comments

Comments
 (0)