Skip to content

Commit 3419a23

Browse files
committed
feat: add variable hover message
now when hover over variables, lsp wold return the hover infomation in format of: ``` type typedoc(if presents) ```
1 parent c318911 commit 3419a23

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/ast/ctx.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ impl<'a, 'ctx> Ctx<'a> {
728728
return Err(self.add_diag(range.new_err(ErrorCode::REDECLARATION)));
729729
}
730730

731+
// add basic hover infomation for defs
732+
self.save_if_var_hover(range, &pltype.borrow());
731733
self.add_symbol_without_check(name, pv, real_tp(pltype), range, is_glob, is_extern)
732734
}
733735
pub fn add_symbol_without_check(

src/ast/ctx/lsp.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ impl Ctx<'_> {
5757
self.save_if_hover(range, HoverContents::Array(content))
5858
}
5959

60+
/// # save_if_var_hover
61+
///
62+
/// save infomation to display if the variable is hovered
63+
pub fn save_if_var_hover(&self, range: Range, tp: &PLType) {
64+
if self.need_highlight.borrow().ne(&0) {
65+
return;
66+
}
67+
let mut content = vec![];
68+
let mut string = String::new();
69+
string.push_str(&tp.get_name());
70+
string.push_str("\n\n");
71+
string.push_str(&tp.get_docs().unwrap_or("".to_string()));
72+
content.push(MarkedString::String(string));
73+
self.save_if_hover(range, HoverContents::Array(content))
74+
}
75+
6076
pub fn save_if_hover(&self, range: Range, value: HoverContents) {
6177
if self.need_highlight.borrow().ne(&0) {
6278
return;

src/ast/node/function.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,14 @@ impl Node for FuncCallNode {
222222
ctx: &'b mut Ctx<'a>,
223223
builder: &'b BuilderEnum<'a, '_>,
224224
) -> NodeResult {
225-
let id_range = self.callee.range();
225+
let mut id_range = self.callee.range();
226226
let v = self.callee.emit(ctx, builder)?.get_value();
227227
if v.is_none() {
228228
return Err(ctx.add_diag(self.range.new_err(ErrorCode::FUNCTION_NOT_FOUND)));
229229
}
230+
if let NodeEnum::Take(tk) = &*self.callee {
231+
id_range = tk.field.as_ref().map(|f| f.range).unwrap_or(tk.range);
232+
}
230233
let v = v.unwrap();
231234
if let Some(builtin) = BUILTIN_FN_MAP.get(&v.get_value()) {
232235
return builtin(self, ctx, builder);

src/ast/node/primary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl Node for VarNode {
179179
0
180180
},
181181
);
182+
ctx.save_if_var_hover(self.range, &symbol.get_data_ref().pltype.borrow());
182183

183184
let symbol_data = symbol.get_data();
184185
let symbol_value = match ctx.generator_data {

0 commit comments

Comments
 (0)