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 hints for schema args #1595

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,12 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
.get_scopes_mut()
.set_owner_to_scope(*cur_scope, owner);
}
self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords)?;
self.do_arguments_symbol_resolve_with_hint_schema(
&call_expr.args,
&call_expr.keywords,
true,
schema_ty,
)?;

self.leave_scope();
}
Expand All @@ -706,7 +711,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
.set_owner_to_scope(*cur_scope, owner);
}

self.do_arguments_symbol_resolve_with_hint(
self.do_arguments_symbol_resolve_with_hint_func(
&call_expr.args,
&call_expr.keywords,
true,
Expand Down Expand Up @@ -1781,7 +1786,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
Ok(())
}

pub fn do_arguments_symbol_resolve_with_hint(
pub fn do_arguments_symbol_resolve_with_hint_func(
&mut self,
args: &'ctx [ast::NodeRef<ast::Expr>],
kwargs: &'ctx [ast::NodeRef<ast::Keyword>],
Expand Down Expand Up @@ -1871,6 +1876,96 @@ impl<'ctx> AdvancedResolver<'ctx> {
Ok(())
}

pub fn do_arguments_symbol_resolve_with_hint_schema(
&mut self,
args: &'ctx [ast::NodeRef<ast::Expr>],
kwargs: &'ctx [ast::NodeRef<ast::Keyword>],
with_hint: bool,
schema_ty: &ty::SchemaType,
) -> anyhow::Result<()> {
if schema_ty.func.params.is_empty() {
self.do_arguments_symbol_resolve(args, kwargs)?;
} else {
for (arg, param) in args.iter().zip(schema_ty.func.params.iter()) {
self.expr(arg)?;

if with_hint {
let symbol_data = self.gs.get_symbols_mut();
let id = match &arg.node {
ast::Expr::Identifier(id) => id.names.last().unwrap().id.clone(),
_ => arg.id.clone(),
};
if let Some(arg_ref) = symbol_data
.symbols_info
.node_symbol_map
.get(&self.ctx.get_node_key(&id))
{
match arg_ref.get_kind() {
crate::core::symbol::SymbolKind::Expression => {
if let Some(expr) = symbol_data.exprs.get_mut(arg_ref.get_id()) {
expr.hint = Some(SymbolHint {
pos: arg.get_pos(),
kind: SymbolHintKind::VarHint(param.name.clone()),
});
}
}
crate::core::symbol::SymbolKind::Unresolved => {
let mut has_hint = false;
if let Some(unresolved) =
symbol_data.unresolved.get(arg_ref.get_id())
{
if let Some(def) = unresolved.def {
if let Some(def) = symbol_data.get_symbol(def) {
if def.get_name() != param.name {
has_hint = true;
}
}
}
}
if has_hint {
if let Some(unresolved) =
symbol_data.unresolved.get_mut(arg_ref.get_id())
{
unresolved.hint = Some(SymbolHint {
kind: SymbolHintKind::VarHint(param.name.clone()),
pos: arg.get_pos(),
});
}
}
}
_ => {}
}
}
}
}

for kw in kwargs.iter() {
if let Some(value) = &kw.node.value {
self.expr(value)?;
}
let (start_pos, end_pos): Range = kw.node.arg.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
5 changes: 5 additions & 0 deletions kclvm/tools/src/LSP/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ mod tests {
test_function_call_arg_hint,
"src/test_data/inlay_hints/function_call/function_call.k"
);

inlay_hints_test_snapshot!(
test_schema_arg_hint,
"src/test_data/inlay_hints/schema_args/schema_args_hint.k"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
source: tools/src/LSP/src/inlay_hints.rs
assertion_line: 122
expression: "format!(\"{:#?}\", res)"
---
Some(
[
InlayHint {
position: Position {
line: 1,
character: 5,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": int",
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: 2,
character: 5,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": str",
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: 4,
character: 1,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": Person",
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: 4,
character: 11,
},
label: LabelParts(
[
InlayHintLabelPart {
value: "age: ",
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: 4,
character: 14,
},
label: LabelParts(
[
InlayHintLabelPart {
value: "name: ",
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
@@ -0,0 +1,5 @@
schema Person[age: int, name: str]:
a = age
n = name

p = Person(1, "Alice")
Loading