Skip to content

Commit 74a0daa

Browse files
committed
support shell and sql highlight
1 parent d330fbf commit 74a0daa

File tree

5 files changed

+1447
-2
lines changed

5 files changed

+1447
-2
lines changed

crates/emmylua_parser_desc/src/lang/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
mod json;
22
mod lua;
3+
mod shell;
4+
mod sql;
35
mod vimscript;
46

57
use emmylua_parser::{LexerState, Reader, SourceRange};
68

79
use crate::{
8-
DescItemKind, lang::json::process_json_code_block, lang::lua::process_lua_code_block,
9-
lang::vimscript::process_vimscript_code_block, util::ResultContainer,
10+
DescItemKind,
11+
lang::{
12+
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,
15+
},
16+
util::ResultContainer,
1017
};
1118

1219
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -15,6 +22,8 @@ pub enum CodeBlockLang {
1522
Lua,
1623
Vimscript,
1724
Json,
25+
Shell,
26+
Sql,
1827
Other,
1928
}
2029

@@ -25,6 +34,8 @@ impl CodeBlockLang {
2534
"" | "none" => Some(CodeBlockLang::None),
2635
"vim" | "vimscript" => Some(CodeBlockLang::Vimscript),
2736
"json" | "Json" => Some(CodeBlockLang::Json),
37+
"shell" | "Shell" => Some(CodeBlockLang::Shell),
38+
"sql" | "Sql" => Some(CodeBlockLang::Sql),
2839
_ => Some(CodeBlockLang::Other),
2940
}
3041
}
@@ -41,6 +52,8 @@ pub fn process_code<'a, C: ResultContainer>(
4152
CodeBlockLang::Lua => process_lua_code_block(c, reader, state),
4253
CodeBlockLang::Vimscript => process_vimscript_code_block(c, reader, state),
4354
CodeBlockLang::Json => process_json_code_block(c, reader, state),
55+
CodeBlockLang::Shell => process_shell_code_block(c, reader, state),
56+
CodeBlockLang::Sql => process_sql_code_block(c, reader, state),
4457
_ => {
4558
c.emit_range(range, DescItemKind::CodeBlock);
4659
return state;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
mod shell_lexer;
2+
3+
use emmylua_parser::{LexerState, Reader};
4+
5+
use crate::{CodeBlockHighlightKind, DescItemKind, util::ResultContainer};
6+
7+
use shell_lexer::{ShellLexer, ShellTokenData, ShellTokenKind};
8+
9+
pub fn process_shell_code_block<'a, C: ResultContainer>(
10+
c: &mut C,
11+
line_reader: Reader<'a>,
12+
state: LexerState,
13+
) -> LexerState {
14+
let mut lexer = ShellLexer::new_with_state(line_reader, state);
15+
let tokens = lexer.tokenize();
16+
for token in tokens {
17+
if !matches!(
18+
token.kind,
19+
ShellTokenKind::TkEof | ShellTokenKind::TkWhitespace
20+
) {
21+
let highlight_kind = to_highlight_kind(&token);
22+
if highlight_kind != CodeBlockHighlightKind::None {
23+
c.emit_range(token.range, DescItemKind::CodeBlockHl(highlight_kind));
24+
}
25+
}
26+
}
27+
28+
lexer.get_state()
29+
}
30+
31+
fn to_highlight_kind(token: &ShellTokenData) -> CodeBlockHighlightKind {
32+
match token.kind {
33+
ShellTokenKind::TkString
34+
| ShellTokenKind::TkSingleQuotedString
35+
| ShellTokenKind::TkDoubleQuotedString
36+
| ShellTokenKind::TkBacktickString
37+
| ShellTokenKind::TkHereDoc => CodeBlockHighlightKind::String,
38+
39+
ShellTokenKind::TkNumber => CodeBlockHighlightKind::Number,
40+
41+
ShellTokenKind::TkKeyword => CodeBlockHighlightKind::Keyword,
42+
43+
ShellTokenKind::TkBuiltin | ShellTokenKind::TkCommand => CodeBlockHighlightKind::Function,
44+
45+
ShellTokenKind::TkVariable | ShellTokenKind::TkDollar => CodeBlockHighlightKind::Variable,
46+
47+
ShellTokenKind::TkComment => CodeBlockHighlightKind::Comment,
48+
49+
ShellTokenKind::TkOperator
50+
| ShellTokenKind::TkPipe
51+
| ShellTokenKind::TkRedirection
52+
| ShellTokenKind::TkBackground
53+
| ShellTokenKind::TkAnd
54+
| ShellTokenKind::TkOr => CodeBlockHighlightKind::Operators,
55+
56+
ShellTokenKind::TkLeftBrace
57+
| ShellTokenKind::TkRightBrace
58+
| ShellTokenKind::TkLeftBracket
59+
| ShellTokenKind::TkRightBracket
60+
| ShellTokenKind::TkLeftParen
61+
| ShellTokenKind::TkRightParen
62+
| ShellTokenKind::TkSemicolon => CodeBlockHighlightKind::Operators,
63+
64+
_ => CodeBlockHighlightKind::None,
65+
}
66+
}

0 commit comments

Comments
 (0)