Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add inlay hints for function call args #1473

Merged
merged 11 commits into from
Jul 10, 2024
Merged
76 changes: 74 additions & 2 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
SymbolSemanticInfo, UnresolvedSymbol, ValueSymbol,
},
},
ty::{Type, TypeKind, SCHEMA_MEMBER_FUNCTIONS},
ty::{Parameter, Type, TypeKind, SCHEMA_MEMBER_FUNCTIONS},
};

use super::AdvancedResolver;
Expand Down Expand Up @@ -573,7 +573,29 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {

fn walk_call_expr(&mut self, call_expr: &'ctx ast::CallExpr) -> Self::Result {
self.expr(&call_expr.func)?;
self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords)?;
let ty = self
.ctx
.node_ty_map
.borrow()
.get(&self.ctx.get_node_key(&call_expr.func.id))
.unwrap()
.clone();

if let TypeKind::Function(func_ty) = &ty.kind {
let func_params = &func_ty.params;

if func_params.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can not use do_arguments_symbol_resolve_with_hint here?

self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords)?;
} else {
self.do_arguments_symbol_resolve_with_hint(
&call_expr.args,
&call_expr.keywords,
true,
func_params.to_vec(),
)?;
}
}

Ok(None)
}

Expand Down Expand Up @@ -1264,6 +1286,56 @@ impl<'ctx> AdvancedResolver<'ctx> {
Ok(())
}

pub fn do_arguments_symbol_resolve_with_hint(
&mut self,
args: &'ctx [ast::NodeRef<ast::Expr>],
kwargs: &'ctx [ast::NodeRef<ast::Keyword>],
with_hint: bool,
params: Vec<Parameter>,
) -> anyhow::Result<()> {
for (arg, param) in args.iter().zip(params.iter()) {
self.expr(arg)?;

let symbol_data = self.gs.get_symbols_mut();
if let Some(arg_ref) = symbol_data
.symbols_info
.node_symbol_map
.get(&self.ctx.get_node_key(&arg.id))
{
if let Some(value) = symbol_data.values.get_mut(arg_ref.get_id()) {
if with_hint {
value.hint = Some(SymbolHint::VarHint(param.name.clone()));
}
}
}
}

for kw in kwargs.iter() {
if let Some(value) = &kw.node.value {
self.expr(value)?;
Peefy marked this conversation as resolved.
Show resolved Hide resolved
}
let (start_pos, end_pos): Range = kw.get_span_pos();
let value = self.gs.get_symbols_mut().alloc_value_symbol(
ValueSymbol::new(kw.node.arg.node.get_name(), start_pos, end_pos, None, false),
self.ctx.get_node_key(&kw.id),
self.ctx.current_pkgpath.clone().unwrap(),
);

if let Some(value) = self.gs.get_symbols_mut().values.get_mut(value.get_id()) {
value.sema_info = SymbolSemanticInfo {
ty: self
.ctx
.node_ty_map
.borrow()
.get(&self.ctx.get_node_key(&kw.id))
.map(|ty| ty.clone()),
doc: None,
};
}
}
Ok(())
}

pub(crate) fn walk_config_entries(
&mut self,
entries: &'ctx [ast::NodeRef<ast::ConfigEntry>],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: tools/src/LSP/src/inlay_hints.rs
assertion_line: 118
expression: "format!(\"{:#?}\", res)"
---
Some(
Expand Down Expand Up @@ -246,5 +247,93 @@ Some(
padding_right: None,
data: None,
},
InlayHint {
position: Position {
line: 23,
character: 4,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": (any, any, any) -> any",
tooltip: None,
location: None,
command: None,
},
],
),
kind: None,
text_edits: None,
tooltip: None,
padding_left: None,
padding_right: None,
data: None,
},
InlayHint {
position: Position {
line: 27,
character: 1,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": any",
tooltip: None,
location: None,
command: None,
},
],
),
kind: None,
text_edits: None,
tooltip: None,
padding_left: None,
padding_right: None,
data: None,
},
InlayHint {
position: Position {
line: 27,
character: 9,
},
label: LabelParts(
[
InlayHintLabelPart {
value: "x: ",
tooltip: None,
location: None,
command: None,
},
],
),
kind: None,
text_edits: None,
tooltip: None,
padding_left: None,
padding_right: None,
data: None,
},
InlayHint {
position: Position {
line: 27,
character: 12,
},
label: LabelParts(
[
InlayHintLabelPart {
value: "y: ",
tooltip: None,
location: None,
command: None,
},
],
),
kind: None,
text_edits: None,
tooltip: None,
padding_left: None,
padding_right: None,
data: None,
},
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ ee = {
a: "asda"
}
ccc = 1Ki

func = lambda x, y, z{
x * y + z
}

z = func(a, 1, z = 2)
Loading