Skip to content

Commit 8d610ec

Browse files
committed
Support emphasis in emmy annotator
1 parent e1a17ad commit 8d610ec

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

crates/emmylua_ls/src/handlers/emmy_annotator/build_annotator.rs

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::collections::HashSet;
22

3-
use emmylua_code_analysis::{DbIndex, LuaDeclId, LuaDocument, SemanticModel};
3+
use super::{EmmyAnnotator, EmmyAnnotatorType};
4+
use crate::util::parse_desc;
5+
use emmylua_code_analysis::{DbIndex, LuaDeclId, LuaDocument, SemanticModel, WorkspaceId};
46
use emmylua_parser::{
5-
LuaAst, LuaAstNode, LuaAstToken, LuaForRangeStat, LuaForStat, LuaLocalFuncStat, LuaLocalStat,
6-
LuaNameExpr, LuaParamList,
7+
LuaAst, LuaAstNode, LuaAstToken, LuaDocDescription, LuaForRangeStat, LuaForStat,
8+
LuaLocalFuncStat, LuaLocalStat, LuaNameExpr, LuaParamList,
79
};
10+
use emmylua_parser_desc::DescItemKind;
811
use rowan::TextRange;
912

10-
use super::{EmmyAnnotator, EmmyAnnotatorType};
11-
1213
pub fn build_annotators(semantic: &SemanticModel) -> Vec<EmmyAnnotator> {
1314
let mut result = vec![];
1415
let document = semantic.get_document();
@@ -59,6 +60,20 @@ pub fn build_annotators(semantic: &SemanticModel) -> Vec<EmmyAnnotator> {
5960
LuaAst::LuaNameExpr(name_expr) => {
6061
build_name_expr_annotator(&document, &mut use_range_set, &mut result, name_expr);
6162
}
63+
LuaAst::LuaDocDescription(description) => {
64+
if semantic
65+
.get_emmyrc()
66+
.semantic_tokens
67+
.render_documentation_markup
68+
{
69+
build_description_annotator(
70+
&semantic,
71+
&mut use_range_set,
72+
&mut result,
73+
description,
74+
);
75+
}
76+
}
6277
_ => {}
6378
}
6479
}
@@ -291,3 +306,53 @@ fn build_local_func_stat_annotator(
291306

292307
Some(())
293308
}
309+
310+
fn build_description_annotator(
311+
semantic_model: &SemanticModel,
312+
use_range_set: &mut HashSet<TextRange>,
313+
result: &mut Vec<EmmyAnnotator>,
314+
description: LuaDocDescription,
315+
) -> Option<()> {
316+
let document = semantic_model.get_document();
317+
let text = document.get_text();
318+
let items = parse_desc(
319+
semantic_model
320+
.get_module()
321+
.map(|m| m.workspace_id)
322+
.unwrap_or(WorkspaceId::MAIN),
323+
semantic_model.get_emmyrc(),
324+
text,
325+
description,
326+
None,
327+
);
328+
329+
let mut strong = EmmyAnnotator {
330+
typ: EmmyAnnotatorType::DocStrong,
331+
ranges: vec![],
332+
};
333+
let mut em = EmmyAnnotator {
334+
typ: EmmyAnnotatorType::DocEm,
335+
ranges: vec![],
336+
};
337+
338+
for item in items {
339+
match item.kind {
340+
DescItemKind::Em => {
341+
use_range_set.insert(item.range.clone());
342+
em.ranges.push(document.to_lsp_range(item.range.clone())?);
343+
}
344+
DescItemKind::Strong => {
345+
use_range_set.insert(item.range.clone());
346+
strong
347+
.ranges
348+
.push(document.to_lsp_range(item.range.clone())?);
349+
}
350+
_ => {}
351+
}
352+
}
353+
354+
result.push(em);
355+
result.push(strong);
356+
357+
Some(())
358+
}

crates/emmylua_ls/src/handlers/emmy_annotator/emmy_annotator_request.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub enum EmmyAnnotatorType {
3030
ReadOnlyLocal = 2,
3131
MutLocal = 3,
3232
MutParam = 4,
33+
DocEm = 5,
34+
DocStrong = 6,
3335
}
3436

3537
impl From<EmmyAnnotatorType> for u8 {
@@ -46,6 +48,8 @@ impl From<u8> for EmmyAnnotatorType {
4648
2 => EmmyAnnotatorType::ReadOnlyLocal,
4749
3 => EmmyAnnotatorType::MutLocal,
4850
4 => EmmyAnnotatorType::MutParam,
51+
5 => EmmyAnnotatorType::DocEm,
52+
6 => EmmyAnnotatorType::DocStrong,
4953
_ => EmmyAnnotatorType::ReadOnlyLocal,
5054
}
5155
}

0 commit comments

Comments
 (0)