Skip to content

Commit 377f4ea

Browse files
committed
add description to type decl
1 parent a3e6cae commit 377f4ea

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
`CHG` Refactor Union type
2323

24+
`NEW` Add description to type
25+
2426
# 0.5.1
2527

2628
`FIX` Fix issue `emmylua_ls` might not exit in unix.

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

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use std::collections::HashMap;
22

33
use emmylua_parser::{
4-
LuaAssignStat, LuaAst, LuaAstNode, LuaAstToken, LuaCommentOwner, LuaDocDescriptionOwner,
5-
LuaDocGenericDeclList, LuaDocTagAlias, LuaDocTagClass, LuaDocTagEnum, LuaDocTagGeneric,
6-
LuaFuncStat, LuaLocalName, LuaLocalStat, LuaNameExpr, LuaSyntaxId, LuaSyntaxKind, LuaTokenKind,
7-
LuaVarExpr,
4+
LuaAssignStat, LuaAst, LuaAstNode, LuaAstToken, LuaCommentOwner, LuaDocDescription,
5+
LuaDocDescriptionOwner, LuaDocGenericDeclList, LuaDocTagAlias, LuaDocTagClass, LuaDocTagEnum,
6+
LuaDocTagGeneric, LuaFuncStat, LuaLocalName, LuaLocalStat, LuaNameExpr, LuaSyntaxId,
7+
LuaSyntaxKind, LuaTokenKind, LuaVarExpr,
88
};
99
use rowan::TextRange;
1010

11-
use crate::db_index::{
12-
LuaDeclId, LuaDeclTypeKind, LuaMemberId, LuaPropertyOwnerId, LuaSignatureId, LuaType,
11+
use crate::{
12+
db_index::{
13+
LuaDeclId, LuaDeclTypeKind, LuaMemberId, LuaPropertyOwnerId, LuaSignatureId, LuaType,
14+
},
15+
LuaTypeDeclId,
1316
};
1417

1518
use super::{
@@ -62,21 +65,45 @@ pub fn analyze_class(analyzer: &mut DocAnalyzer, tag: LuaDocTagClass) -> Option<
6265
}
6366
}
6467

65-
if let Some(description) = tag.get_description() {
66-
let description_text = preprocess_description(&description.get_description_text());
67-
if !description_text.is_empty() {
68-
analyzer.db.get_property_index_mut().add_description(
69-
file_id,
70-
LuaPropertyOwnerId::TypeDecl(class_decl_id.clone()),
71-
description_text,
72-
);
73-
}
74-
}
68+
add_description_for_type_decl(analyzer, &class_decl_id, tag.get_description());
7569

7670
bind_def_type(analyzer, LuaType::Def(class_decl_id.clone()));
7771
Some(())
7872
}
7973

74+
fn add_description_for_type_decl(
75+
analyzer: &mut DocAnalyzer,
76+
type_decl_id: &LuaTypeDeclId,
77+
description: Option<LuaDocDescription>,
78+
) {
79+
let mut description_text = String::new();
80+
81+
let comment = analyzer.comment.clone();
82+
if let Some(description) = comment.get_description() {
83+
let description = preprocess_description(&description.get_description_text());
84+
if !description.is_empty() {
85+
description_text.push_str(&description);
86+
}
87+
}
88+
89+
if let Some(description) = description {
90+
let description = preprocess_description(&description.get_description_text());
91+
if !description.is_empty() {
92+
if !description_text.is_empty() {
93+
description_text.push_str("\n\n");
94+
}
95+
96+
description_text.push_str(&description);
97+
}
98+
}
99+
100+
analyzer.db.get_property_index_mut().add_description(
101+
analyzer.file_id,
102+
LuaPropertyOwnerId::TypeDecl(type_decl_id.clone()),
103+
description_text,
104+
);
105+
}
106+
80107
pub fn analyze_enum(analyzer: &mut DocAnalyzer, tag: LuaDocTagEnum) -> Option<()> {
81108
let file_id = analyzer.file_id;
82109
let name = tag.get_name_token()?.get_name_text().to_string();
@@ -107,16 +134,8 @@ pub fn analyze_enum(analyzer: &mut DocAnalyzer, tag: LuaDocTagEnum) -> Option<()
107134
enum_decl.add_enum_base(base_type);
108135
}
109136

110-
if let Some(description) = tag.get_description() {
111-
let description_text = preprocess_description(&description.get_description_text());
112-
if !description_text.is_empty() {
113-
analyzer.db.get_property_index_mut().add_description(
114-
file_id,
115-
LuaPropertyOwnerId::TypeDecl(enum_decl_id.clone()),
116-
description_text,
117-
);
118-
}
119-
}
137+
let description = tag.get_description();
138+
add_description_for_type_decl(analyzer, &enum_decl_id, description);
120139

121140
bind_def_type(analyzer, LuaType::Def(enum_decl_id.clone()));
122141

@@ -167,16 +186,8 @@ pub fn analyze_alias(analyzer: &mut DocAnalyzer, tag: LuaDocTagAlias) -> Option<
167186

168187
alias.add_alias_origin(origin_type);
169188

170-
let description_text = tag.get_description()?.get_description_text();
171-
if description_text.is_empty() {
172-
return None;
173-
}
174-
let description_text = preprocess_description(&description_text);
175-
analyzer.db.get_property_index_mut().add_description(
176-
file_id,
177-
LuaPropertyOwnerId::TypeDecl(alias_decl_id.clone()),
178-
description_text,
179-
);
189+
let description = tag.get_description();
190+
add_description_for_type_decl(analyzer, &alias_decl_id, description);
180191

181192
Some(())
182193
}

crates/emmylua_ls/src/handlers/completion/providers/type_special_provider.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,19 @@ fn add_multi_line_union_member_completion(
298298
None
299299
};
300300

301+
let label_details = if let Some(description) = description {
302+
Some(lsp_types::CompletionItemLabelDetails {
303+
detail: None,
304+
description: Some(description.clone()),
305+
})
306+
} else {
307+
None
308+
};
309+
301310
let completion_item = CompletionItem {
302311
label: name,
303312
kind: Some(lsp_types::CompletionItemKind::ENUM_MEMBER),
313+
label_details,
304314
documentation,
305315
..Default::default()
306316
};

0 commit comments

Comments
 (0)