Skip to content

Commit

Permalink
feat: inlayhint for config key type
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa committed Sep 23, 2024
1 parent 7452116 commit 716bac6
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 6 deletions.
41 changes: 35 additions & 6 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
.get(&self.ctx.get_node_key(ast_id))
.map(|ty| ty.clone());
symbol.hint = ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_str()),
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: symbol.get_range().1,
});
symbol.sema_info = SymbolSemanticInfo { ty, doc: None };
Expand Down Expand Up @@ -1280,7 +1280,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
}
}
}
Ok(Some(symbol_ref))
Ok(Some(first_unresolved_ref))
}
None => {
if maybe_def {
Expand Down Expand Up @@ -1612,7 +1612,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
.map(|ty| ty.clone());
if with_hint {
symbol.hint = ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_str()),
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: symbol.get_range().1,
});
}
Expand Down Expand Up @@ -1681,7 +1681,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
.map(|ty| ty.clone());
if with_hint {
symbol.hint = ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_str()),
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: symbol.get_range().1,
});
}
Expand Down Expand Up @@ -1760,7 +1760,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
Some(ty) => {
let (_, end) = ty_node.get_span_pos();
let mut expr_symbol =
ExpressionSymbol::new(format!("@{}", ty.ty_str()), end.clone(), end, None);
ExpressionSymbol::new(format!("@{}", ty.ty_hint()), end.clone(), end, None);

expr_symbol.sema_info.ty = Some(ty.clone());
self.gs.get_symbols_mut().alloc_expression_symbol(
Expand Down Expand Up @@ -1935,7 +1935,36 @@ impl<'ctx> AdvancedResolver<'ctx> {

if let Some(key) = &entry.node.key {
self.ctx.maybe_def = true;
self.expr(key)?;
if let Some(symbol_ref) = self.expr(key)? {
let config_key_symbol =
self.gs.get_symbols().unresolved.get(symbol_ref.get_id());
let hint: Option<SymbolHint> =
if let Some(config_key_symbol) = config_key_symbol {
match config_key_symbol.get_definition() {
Some(def_ref) => match self.gs.get_symbols().get_symbol(def_ref) {
Some(def_symbol) => {
let ty = def_symbol.get_sema_info().ty.clone();
ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: config_key_symbol.get_range().1.clone(),
})
}
None => None,
},
None => None,
}
} else {
None
};
if let Some(config_key_symbol_mut_ref) = self
.gs
.get_symbols_mut()
.unresolved
.get_mut(symbol_ref.get_id())
{
config_key_symbol_mut_ref.hint = hint;
}
}
self.ctx.maybe_def = false;
}
}
Expand Down
18 changes: 18 additions & 0 deletions kclvm/sema/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ impl Type {
}
}

pub fn ty_hint(&self) -> String {
match &self.kind {
TypeKind::StrLit(s) => format!("\"{}\"", s),
TypeKind::IntLit(v) => v.to_string(),
TypeKind::FloatLit(v) => v.to_string(),
TypeKind::List(item_ty) => format!("[{}]", item_ty.ty_hint()),
TypeKind::Dict(DictType { key_ty, val_ty, .. }) => {
format!("{{{}:{}}}", key_ty.ty_hint(), val_ty.ty_hint())
}
TypeKind::Union(types) => types
.iter()
.map(|ty| ty.ty_hint())
.collect::<Vec<String>>()
.join(" | "),
_ => self.ty_str(),
}
}

/// Returns the full type string with the package path used for the error handler.
pub fn full_ty_str(&self) -> String {
match &self.kind {
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 @@ -123,4 +123,9 @@ mod tests {
test_schema_arg_hint,
"src/test_data/inlay_hints/schema_args/schema_args_hint.k"
);

inlay_hints_test_snapshot!(
test_config_key_ty,
"src/test_data/inlay_hints/config_key/config_key.k"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
source: tools/src/LSP/src/inlay_hints.rs
expression: "format!(\"{:#?}\", res)"
---
Some(
[
InlayHint {
position: Position {
line: 6,
character: 3,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": App",
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: 7,
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: 8,
character: 5,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": 1 | 2",
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: 9,
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: 10,
character: 5,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": \"A\" | \"B\"",
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,12 @@
schema App:
a: int
b: 1 | 2
c: str
d: "A" | "B"

app = App {
a: 1
b: 1
c: "1"
d: "A"
}

0 comments on commit 716bac6

Please sign in to comment.