Skip to content

Commit f1a37b4

Browse files
committed
hover: signature description not removing first "#" "@"
1 parent c8dd5ee commit f1a37b4

File tree

7 files changed

+43
-13
lines changed

7 files changed

+43
-13
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use emmylua_parser::{
66
};
77

88
use crate::{
9+
compilation::analyzer::doc::preprocess_description,
910
db_index::{
1011
LuaMember, LuaMemberKey, LuaMemberOwner, LuaOperator, LuaOperatorMetaMethod,
1112
LuaSemanticDeclId, LuaType,
@@ -134,7 +135,8 @@ pub fn analyze_field(analyzer: &mut DocAnalyzer, tag: LuaDocTagField) -> Option<
134135
);
135136
}
136137

137-
if let Some(description) = description {
138+
if let Some(mut description) = description {
139+
description = preprocess_description(&description, Some(&property_owner));
138140
analyzer.db.get_property_index_mut().add_description(
139141
analyzer.file_id,
140142
property_owner.clone(),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ fn infer_multi_line_union_type(
588588
};
589589

590590
let description = if let Some(description) = field.get_description() {
591-
let description_text = preprocess_description(&description.get_description_text());
591+
let description_text =
592+
preprocess_description(&description.get_description_text(), None);
592593
if !description_text.is_empty() {
593594
Some(description_text)
594595
} else {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::AnalyzeContext;
1111
use crate::{
1212
db_index::{DbIndex, LuaTypeDeclId},
1313
profile::Profile,
14-
FileId,
14+
FileId, LuaSemanticDeclId,
1515
};
1616
use emmylua_parser::{LuaAstNode, LuaComment, LuaDocDescriptionOwner, LuaSyntaxNode};
1717
use file_generic_index::FileGenericIndex;
@@ -44,8 +44,10 @@ fn analyze_comment(analyzer: &mut DocAnalyzer) -> Option<()> {
4444
}
4545

4646
let owenr = get_owner_id(analyzer)?;
47-
let comment_description =
48-
preprocess_description(&comment.get_description()?.get_description_text());
47+
let comment_description = preprocess_description(
48+
&comment.get_description()?.get_description_text(),
49+
Some(&owenr),
50+
);
4951
analyzer.db.get_property_index_mut().add_description(
5052
analyzer.file_id,
5153
owenr,
@@ -90,9 +92,16 @@ impl<'a> DocAnalyzer<'a> {
9092
}
9193
}
9294

93-
pub fn preprocess_description(mut description: &str) -> String {
94-
if description.starts_with(['#', '@']) {
95-
description = description.trim_start_matches(|c| c == '#' || c == '@');
95+
pub fn preprocess_description(mut description: &str, owner: Option<&LuaSemanticDeclId>) -> String {
96+
let has_remove_start_char = if let Some(owner) = owner {
97+
!matches!(owner, LuaSemanticDeclId::Signature(_))
98+
} else {
99+
true
100+
};
101+
if has_remove_start_char {
102+
if description.starts_with(['#', '@']) {
103+
description = description.trim_start_matches(|c| c == '#' || c == '@');
104+
}
96105
}
97106

98107
let mut result = String::new();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ fn add_description_for_type_decl(
9494
if !is_class {
9595
let comment = analyzer.comment.clone();
9696
if let Some(description) = comment.get_description() {
97-
let description = preprocess_description(&description.get_description_text());
97+
let description = preprocess_description(&description.get_description_text(), None);
9898
if !description.is_empty() {
9999
description_text.push_str(&description);
100100
}
101101
}
102102
}
103103

104104
if let Some(description) = description {
105-
let description = preprocess_description(&description.get_description_text());
105+
let description = preprocess_description(&description.get_description_text(), None);
106106
if !description.is_empty() {
107107
if !description_text.is_empty() {
108108
description_text.push_str("\n\n");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn analyze_param(analyzer: &mut DocAnalyzer, tag: LuaDocTagParam) -> Option<
116116
}
117117

118118
let description = if let Some(des) = tag.get_description() {
119-
Some(preprocess_description(&des.get_description_text()))
119+
Some(preprocess_description(&des.get_description_text(), None))
120120
} else {
121121
None
122122
};
@@ -155,7 +155,7 @@ pub fn analyze_param(analyzer: &mut DocAnalyzer, tag: LuaDocTagParam) -> Option<
155155

156156
pub fn analyze_return(analyzer: &mut DocAnalyzer, tag: LuaDocTagReturn) -> Option<()> {
157157
let description = if let Some(des) = tag.get_description() {
158-
Some(preprocess_description(&des.get_description_text()))
158+
Some(preprocess_description(&des.get_description_text(), None))
159159
} else {
160160
None
161161
};
@@ -383,7 +383,7 @@ pub fn analyze_other(analyzer: &mut DocAnalyzer, other: LuaDocTagOther) -> Optio
383383
let owner = get_owner_id(analyzer)?;
384384
let tag_name = other.get_tag_name()?;
385385
let description = if let Some(des) = other.get_description() {
386-
let description = preprocess_description(&des.get_description_text());
386+
let description = preprocess_description(&des.get_description_text(), None);
387387
format!("@*{}* {}", tag_name, description)
388388
} else {
389389
format!("@*{}*", tag_name)

crates/emmylua_ls/src/handlers/hover/hover_builder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ impl<'a> HoverBuilder<'a> {
284284
result.push_str(&description_content);
285285
result.push_str(&expansion);
286286

287+
// 清除空白字符
288+
result = result.trim().to_string();
289+
287290
Some(Hover {
288291
contents: HoverContents::Markup(MarkupContent {
289292
kind: lsp_types::MarkupKind::Markdown,

crates/emmylua_ls/src/handlers/test/hover_test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,19 @@ mod tests {
180180
},
181181
));
182182
}
183+
184+
#[test]
185+
fn test_signature_desc() {
186+
let mut ws = ProviderVirtualWorkspace::new();
187+
assert!(ws.check_hover(
188+
r#"
189+
-- # A
190+
local function a<??>bc()
191+
end
192+
"#,
193+
VirtualHoverResult {
194+
value: "```lua\nlocal function abc()\n```\n\n---\n\n# A".to_string(),
195+
},
196+
));
197+
}
183198
}

0 commit comments

Comments
 (0)