Skip to content

Commit

Permalink
feat: add inlay hints for function call args (#1473)
Browse files Browse the repository at this point in the history
* add inlay hints for function call args

Signed-off-by: shruti2522 <[email protected]>

make fmt

Signed-off-by: shruti2522 <[email protected]>

add inlay hints for function call args

Signed-off-by: shruti2522 <[email protected]>

* fix ci

Signed-off-by: shruti2522 <[email protected]>

remove inlay hints for kwargs

Signed-off-by: shruti2522 <[email protected]>

remove kw_name

Signed-off-by: shruti2522 <[email protected]>

fix ci

Signed-off-by: shruti2522 <[email protected]>

add inlay hints for function call args

Signed-off-by: shruti2522 <[email protected]>

make fmt

Signed-off-by: shruti2522 <[email protected]>

add inlay hints for function call args

Signed-off-by: shruti2522 <[email protected]>

test ci

Signed-off-by: shruti2522 <[email protected]>

test ci

Signed-off-by: shruti2522 <[email protected]>

fix indexing error

Signed-off-by: shruti2522 <[email protected]>

fix ci

Signed-off-by: shruti2522 <[email protected]>

update test snaps

Signed-off-by: shruti2522 <[email protected]>

fix ci

Signed-off-by: shruti2522 <[email protected]>

fix ci

Signed-off-by: shruti2522 <[email protected]>

* update snaps

Signed-off-by: shruti2522 <[email protected]>

* remove hints for builtin func calls

Signed-off-by: shruti2522 <[email protected]>

remove inlay hints for system module functions

Signed-off-by: shruti2522 <[email protected]>

change function name

Signed-off-by: shruti2522 <[email protected]>

fix ci

Signed-off-by: shruti2522 <[email protected]>

remove hints for builtin func calls

Signed-off-by: shruti2522 <[email protected]>

* update builtin loader snap to incliude inlay hints

Signed-off-by: shruti2522 <[email protected]>

* test inlay hints

Signed-off-by: shruti2522 <[email protected]>

* add function type param

Signed-off-by: shruti2522 <[email protected]>

* fix ci

Signed-off-by: shruti2522 <[email protected]>

* add get_hint for expression

Signed-off-by: shruti2522 <[email protected]>

* add value symbol

Signed-off-by: shruti2522 <[email protected]>

* correct character position

Signed-off-by: shruti2522 <[email protected]>

---------

Signed-off-by: shruti2522 <[email protected]>
  • Loading branch information
shruti2522 authored Jul 10, 2024
1 parent 128012c commit 8092444
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
86 changes: 84 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::{self, Type, TypeKind, SCHEMA_MEMBER_FUNCTIONS},
};

use super::AdvancedResolver;
Expand Down Expand Up @@ -574,7 +574,23 @@ 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 {
self.do_arguments_symbol_resolve_with_hint(
&call_expr.args,
&call_expr.keywords,
true,
&func_ty,
)?;
}

Ok(None)
}

Expand Down Expand Up @@ -1265,6 +1281,72 @@ 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,
func_ty: &ty::FunctionType,
) -> anyhow::Result<()> {
if func_ty.params.is_empty() {
self.do_arguments_symbol_resolve(args, kwargs)?;
} else {
for (arg, param) in args.iter().zip(func_ty.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))
{
match arg_ref.get_kind() {
crate::core::symbol::SymbolKind::Value => {
if let Some(value) = symbol_data.values.get_mut(arg_ref.get_id()) {
if with_hint {
value.hint = Some(SymbolHint::VarHint(param.name.clone()));
}
}
}
crate::core::symbol::SymbolKind::Expression => {
if let Some(expr) = symbol_data.exprs.get_mut(arg_ref.get_id()) {
if with_hint {
expr.hint = Some(SymbolHint::VarHint(param.name.clone()));
}
}
}
_ => {}
}
}
}

for kw in kwargs.iter() {
if let Some(value) = &kw.node.value {
self.expr(value)?;
}
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
4 changes: 3 additions & 1 deletion kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,7 @@ pub struct ExpressionSymbol {
pub(crate) name: String,

pub(crate) sema_info: SymbolSemanticInfo,
pub(crate) hint: Option<SymbolHint>,
}

impl Symbol for ExpressionSymbol {
Expand Down Expand Up @@ -2029,7 +2030,7 @@ impl Symbol for ExpressionSymbol {
}

fn get_hint(&self) -> Option<&Self::SymbolHint> {
None
self.hint.as_ref()
}

fn simple_dump(&self) -> String {
Expand Down Expand Up @@ -2072,6 +2073,7 @@ impl ExpressionSymbol {
end,
sema_info: SymbolSemanticInfo::default(),
owner,
hint: None,
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion kclvm/tools/src/LSP/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ fn get_hint_label(symbol: &KCLSymbol, hint: &SymbolHint) -> (InlayHintLabelPart,
},
LspPosition::new(
(start.line - 1).try_into().unwrap(),
start.column.unwrap_or(0).try_into().unwrap(),
start
.column
.unwrap_or(1)
.saturating_sub(1)
.try_into()
.unwrap(),
),
),
}
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,71 @@ 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: 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
}

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

0 comments on commit 8092444

Please sign in to comment.