Skip to content

Commit 162c6fe

Browse files
committed
streamline description preprocessing and parser logic
1 parent fc832d7 commit 162c6fe

File tree

5 files changed

+66
-90
lines changed

5 files changed

+66
-90
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/mod.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,27 @@ pub fn preprocess_description(mut description: &str, owner: Option<&LuaSemanticD
110110

111111
let mut result = String::new();
112112
let lines = description.lines();
113-
let mut in_code_block = false;
114-
let mut indent = 0;
115-
for line in lines {
116-
let trimmed_line = line.trim_start();
117-
if trimmed_line.starts_with("```") {
118-
in_code_block = !in_code_block;
119-
result.push_str(trimmed_line);
113+
let mut start_with_one_space = None;
114+
for mut line in lines {
115+
let indent_count = line.chars().take_while(|c| c.is_whitespace()).count();
116+
117+
if indent_count == line.len() {
118+
// empty line
120119
result.push('\n');
121-
if in_code_block {
122-
indent = trimmed_line.len() - trimmed_line.trim_start().len();
123-
}
124120
continue;
125121
}
126122

127-
if in_code_block {
128-
if indent > 0 && line.len() >= indent {
129-
let actual_indent = line
130-
.chars()
131-
.take(indent)
132-
.filter(|c| c.is_whitespace())
133-
.count();
134-
result.push_str(&line[actual_indent..]);
135-
} else {
136-
result.push_str(line);
123+
if start_with_one_space.is_none() {
124+
start_with_one_space = Some(indent_count == 1);
125+
}
126+
127+
if let Some(true) = start_with_one_space {
128+
if indent_count > 0 {
129+
line = &line[1..];
137130
}
138-
} else {
139-
result.push_str(trimmed_line);
140131
}
132+
133+
result.push_str(line);
141134
result.push('\n');
142135
}
143136

crates/emmylua_code_analysis/src/compilation/analyzer/doc/type_def_tags.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,6 @@ fn add_description_for_type_decl(
7070
descriptions: Vec<LuaDocDescription>,
7171
) {
7272
let mut description_text = String::new();
73-
74-
// let comment = analyzer.comment.clone();
75-
// if let Some(description) = comment.get_description() {
76-
// let description = preprocess_description(&description.get_description_text(), None);
77-
// if !description.is_empty() {
78-
// description_text.push_str(&description);
79-
// }
80-
// }
81-
8273
for description in descriptions {
8374
let description = preprocess_description(&description.get_description_text(), None);
8475
if !description.is_empty() {

crates/emmylua_parser/src/grammar/doc/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ fn parse_docs(p: &mut LuaDocParser) {
3434
}
3535
LuaTokenKind::TkNormalStart => {
3636
p.set_state(LuaDocLexerState::NormalDescription);
37+
let mut m = p.mark(LuaSyntaxKind::DocDescription);
38+
3739
p.bump();
3840

3941
if_token_bump(p, LuaTokenKind::TkWhitespace);
@@ -42,10 +44,21 @@ fn parse_docs(p: &mut LuaDocParser) {
4244
p.current_token(),
4345
LuaTokenKind::TkDocRegion | LuaTokenKind::TkDocEndRegion
4446
) {
47+
m.undo(p);
4548
p.bump();
49+
m = p.mark(LuaSyntaxKind::DocDescription);
4650
}
4751

48-
parse_normal_description(p);
52+
while let LuaTokenKind::TkDocDetail
53+
| LuaTokenKind::TkEndOfLine
54+
| LuaTokenKind::TkWhitespace
55+
| LuaTokenKind::TkDocContinue
56+
| LuaTokenKind::TkNormalStart = p.current_token()
57+
{
58+
p.bump();
59+
}
60+
61+
m.complete(p);
4962
}
5063
LuaTokenKind::TkLongCommentStart => {
5164
p.set_state(LuaDocLexerState::LongDescription);
@@ -115,18 +128,3 @@ fn if_token_bump(p: &mut LuaDocParser, token: LuaTokenKind) -> bool {
115128
false
116129
}
117130
}
118-
119-
fn parse_normal_description(p: &mut LuaDocParser) {
120-
let m = p.mark(LuaSyntaxKind::DocDescription);
121-
122-
while let LuaTokenKind::TkDocDetail
123-
| LuaTokenKind::TkEndOfLine
124-
| LuaTokenKind::TkWhitespace
125-
| LuaTokenKind::TkDocContinue
126-
| LuaTokenKind::TkNormalStart = p.current_token()
127-
{
128-
p.bump();
129-
}
130-
131-
m.complete(p);
132-
}

crates/emmylua_parser/src/grammar/doc/test.rs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ Syntax(Chunk)@0..83
3232
Token(TkEndOfLine)@0..1 "\n"
3333
Token(TkWhitespace)@1..9 " "
3434
Syntax(Comment)@9..19
35-
Token(TkNormalStart)@9..11 "--"
36-
Token(TkWhitespace)@11..12 " "
37-
Syntax(DocDescription)@12..19
35+
Syntax(DocDescription)@9..19
36+
Token(TkNormalStart)@9..11 "--"
37+
Token(TkWhitespace)@11..12 " "
3838
Token(TkDocDetail)@12..19 "comment"
3939
Token(TkEndOfLine)@19..20 "\n"
4040
Token(TkEndOfLine)@20..21 "\n"
4141
Token(TkWhitespace)@21..29 " "
4242
Syntax(Comment)@29..74
43-
Token(TkNormalStart)@29..31 "--"
44-
Token(TkWhitespace)@31..32 " "
45-
Syntax(DocDescription)@32..74
43+
Syntax(DocDescription)@29..74
44+
Token(TkNormalStart)@29..31 "--"
45+
Token(TkWhitespace)@31..32 " "
4646
Token(TkDocDetail)@32..38 "hihihi"
4747
Token(TkEndOfLine)@38..39 "\n"
4848
Token(TkWhitespace)@39..47 " "
@@ -76,8 +76,8 @@ Syntax(Chunk)@0..163
7676
Token(TkEndOfLine)@0..1 "\n"
7777
Token(TkWhitespace)@1..9 " "
7878
Syntax(Comment)@9..120
79-
Token(TkNormalStart)@9..15 "--- "
80-
Syntax(DocDescription)@15..22
79+
Syntax(DocDescription)@9..22
80+
Token(TkNormalStart)@9..15 "--- "
8181
Token(TkDocDetail)@15..22 "hiihihi"
8282
Token(TkEndOfLine)@22..23 "\n"
8383
Token(TkWhitespace)@23..31 " "
@@ -1682,14 +1682,14 @@ Syntax(Chunk)@0..63
16821682
Token(TkInt)@19..22 "123"
16831683
Token(TkWhitespace)@22..23 " "
16841684
Syntax(Comment)@23..34
1685-
Token(TkNormalStart)@23..25 "--"
1686-
Syntax(DocDescription)@25..34
1685+
Syntax(DocDescription)@23..34
1686+
Token(TkNormalStart)@23..25 "--"
16871687
Token(TkDocDetail)@25..34 "comment 1"
16881688
Token(TkEndOfLine)@34..35 "\n"
16891689
Token(TkWhitespace)@35..43 " "
16901690
Syntax(Comment)@43..54
1691-
Token(TkNormalStart)@43..45 "--"
1692-
Syntax(DocDescription)@45..54
1691+
Syntax(DocDescription)@43..54
1692+
Token(TkNormalStart)@43..45 "--"
16931693
Token(TkDocDetail)@45..54 "comment 2"
16941694
Token(TkEndOfLine)@54..55 "\n"
16951695
Token(TkWhitespace)@55..63 " "
@@ -2009,37 +2009,32 @@ Syntax(Chunk)@0..90
20092009
#[test]
20102010
fn test_region_with_comment() {
20112011
let code = r#"
2012-
-----------
20132012
--region hhhh
20142013
--comment
20152014
--endregion
20162015
"#;
20172016

20182017
let result = r#"
2019-
Syntax(Chunk)@0..89
2020-
Syntax(Block)@0..89
2018+
Syntax(Chunk)@0..69
2019+
Syntax(Block)@0..69
20212020
Token(TkEndOfLine)@0..1 "\n"
20222021
Token(TkWhitespace)@1..9 " "
2023-
Syntax(Comment)@9..80
2024-
Token(TkNormalStart)@9..12 "---"
2025-
Syntax(DocDescription)@12..31
2026-
Token(TkDocDetail)@12..20 "--------"
2027-
Token(TkEndOfLine)@20..21 "\n"
2028-
Token(TkWhitespace)@21..29 " "
2029-
Token(TkNormalStart)@29..31 "--"
2030-
Token(TkDocTrivia)@31..37 "region"
2031-
Token(TkDocTrivia)@37..42 " hhhh"
2032-
Token(TkEndOfLine)@42..43 "\n"
2033-
Token(TkWhitespace)@43..51 " "
2034-
Token(TkNormalStart)@51..53 "--"
2035-
Syntax(DocDescription)@53..71
2036-
Token(TkDocDetail)@53..60 "comment"
2037-
Token(TkEndOfLine)@60..61 "\n"
2038-
Token(TkWhitespace)@61..69 " "
2039-
Token(TkNormalStart)@69..71 "--"
2040-
Token(TkDocEndRegion)@71..80 "endregion"
2041-
Token(TkEndOfLine)@80..81 "\n"
2042-
Token(TkWhitespace)@81..89 " "
2022+
Syntax(Comment)@9..60
2023+
Token(TkNormalStart)@9..11 "--"
2024+
Token(TkDocRegion)@11..17 "region"
2025+
Token(TkWhitespace)@17..18 " "
2026+
Syntax(DocDescription)@18..51
2027+
Token(TkDocDetail)@18..22 "hhhh"
2028+
Token(TkEndOfLine)@22..23 "\n"
2029+
Token(TkWhitespace)@23..31 " "
2030+
Token(TkNormalStart)@31..33 "--"
2031+
Token(TkDocDetail)@33..40 "comment"
2032+
Token(TkEndOfLine)@40..41 "\n"
2033+
Token(TkWhitespace)@41..49 " "
2034+
Token(TkNormalStart)@49..51 "--"
2035+
Token(TkDocEndRegion)@51..60 "endregion"
2036+
Token(TkEndOfLine)@60..61 "\n"
2037+
Token(TkWhitespace)@61..69 " "
20432038
"#;
20442039

20452040
assert_ast_eq!(code, result);
@@ -2489,8 +2484,8 @@ Syntax(Chunk)@0..118
24892484
Token(TkEndOfLine)@0..1 "\n"
24902485
Token(TkWhitespace)@1..9 " "
24912486
Syntax(Comment)@9..93
2492-
Token(TkNormalStart)@9..13 "--- "
2493-
Syntax(DocDescription)@13..93
2487+
Syntax(DocDescription)@9..93
2488+
Token(TkNormalStart)@9..13 "--- "
24942489
Token(TkDocDetail)@13..27 "Note: ajfioiof"
24952490
Token(TkEndOfLine)@27..28 "\n"
24962491
Token(TkWhitespace)@28..36 " "
@@ -2514,7 +2509,6 @@ Syntax(Chunk)@0..118
25142509
Token(TkEndOfLine)@109..110 "\n"
25152510
Token(TkWhitespace)@110..118 " "
25162511
"#;
2517-
25182512
assert_ast_eq!(code, result);
25192513
}
25202514

@@ -2691,8 +2685,8 @@ Syntax(Chunk)@0..263
26912685
Token(TkEndOfLine)@0..1 "\n"
26922686
Token(TkWhitespace)@1..9 " "
26932687
Syntax(Comment)@9..90
2694-
Token(TkNormalStart)@9..13 "--- "
2695-
Syntax(DocDescription)@13..19
2688+
Syntax(DocDescription)@9..19
2689+
Token(TkNormalStart)@9..13 "--- "
26962690
Token(TkDocDetail)@13..19 "hihiih"
26972691
Token(TkEndOfLine)@19..20 "\n"
26982692
Token(TkWhitespace)@20..28 " "

crates/emmylua_parser/src/grammar/lua/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ Syntax(Chunk)@0..770
7575
Token(TkEndOfLine)@0..1 "\n"
7676
Token(TkWhitespace)@1..13 " "
7777
Syntax(Comment)@13..33
78-
Token(TkNormalStart)@13..15 "--"
79-
Token(TkWhitespace)@15..16 " "
80-
Syntax(DocDescription)@16..33
78+
Syntax(DocDescription)@13..33
79+
Token(TkNormalStart)@13..15 "--"
80+
Token(TkWhitespace)@15..16 " "
8181
Token(TkDocDetail)@16..33 "This is a comment"
8282
Token(TkEndOfLine)@33..34 "\n"
8383
Token(TkWhitespace)@34..46 " "

0 commit comments

Comments
 (0)