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: inlayhint for config key type #1659

Merged
merged 1 commit into from
Sep 24, 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
43 changes: 37 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 @@ -1146,6 +1146,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
}
match first_symbol {
Some(symbol_ref) => {
let mut ret_symbol = symbol_ref.clone();
let (start_pos, end_pos): Range = first_name.get_span_pos();
let def_symbol = self
.gs
Expand Down Expand Up @@ -1189,6 +1190,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
name,
first_unresolved_ref,
);
ret_symbol = first_unresolved_ref;
}
}
}
Expand Down Expand Up @@ -1280,7 +1282,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
}
}
}
Ok(Some(symbol_ref))
Ok(Some(ret_symbol))
}
None => {
if maybe_def {
Expand Down Expand Up @@ -1612,7 +1614,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 +1683,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 +1762,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 +1937,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"
}
Loading