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 config expr #1589

Closed
wants to merge 7 commits into from
Closed
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
64 changes: 61 additions & 3 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,20 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
}

fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result {
self.walk_config_entries(&config_expr.items)?;
let (start, _) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone());

let pre_pos = Position {
filename: start.filename.clone(),
line: start.line.clone(),
column: start.column.map(|c| if c >= 1 { c - 1 } else { 0 }),
};

let with_hint = self.ctx.program.pos_to_stmt(&pre_pos).map_or(
false,
|stmt| matches!(stmt.node, ast::Stmt::Assign(assign_stmt) if assign_stmt.ty.is_some()),
);

self.walk_config_entries_with_hint(&config_expr.items, with_hint)?;
Ok(None)
}

Expand Down Expand Up @@ -1879,6 +1892,14 @@ impl<'ctx> AdvancedResolver<'ctx> {
pub(crate) fn walk_config_entries(
&mut self,
entries: &'ctx [ast::NodeRef<ast::ConfigEntry>],
) -> anyhow::Result<()> {
self.walk_config_entries_with_hint(entries, false)
}

pub(crate) fn walk_config_entries_with_hint(
&mut self,
entries: &'ctx [ast::NodeRef<ast::ConfigEntry>],
with_hint: bool,
) -> anyhow::Result<()> {
let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone());

Expand All @@ -1887,8 +1908,8 @@ impl<'ctx> AdvancedResolver<'ctx> {

self.enter_local_scope(
&self.ctx.current_filename.as_ref().unwrap().clone(),
start,
end,
start.clone(),
end.clone(),
kind,
);

Expand All @@ -1899,6 +1920,43 @@ impl<'ctx> AdvancedResolver<'ctx> {
.set_owner_to_scope(*cur_scope, owner);
}

if with_hint {
if let Some(schema_symbol) = schema_symbol {
let symbol_data = self.gs.get_symbols_mut();
if let Some(schema_data) = symbol_data.get_symbol(schema_symbol) {
if let Some(ty) = &schema_data.get_sema_info().ty {
let mut expr_symbol = ExpressionSymbol::new(
Peefy marked this conversation as resolved.
Show resolved Hide resolved
format!("@{}", ty.ty_str()),
end.clone(),
end.clone(),
None,
);

expr_symbol.sema_info = SymbolSemanticInfo {
ty: self
.ctx
.node_ty_map
.borrow()
.get(&self.ctx.get_node_key(&ast::AstIndex::default()))
.cloned(),
doc: None,
};

expr_symbol.hint = Some(SymbolHint {
pos: start.clone(),
kind: SymbolHintKind::SchemaHint(schema_data.get_name().to_owned()),
});

symbol_data.alloc_expression_symbol(
expr_symbol,
self.ctx.get_node_key(&ast::AstIndex::default()),
self.ctx.current_pkgpath.clone().unwrap(),
);
}
}
}
}

let mut entries_range = vec![];
for entry in entries.iter() {
entries_range.push((
Expand Down
1 change: 1 addition & 0 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct SymbolHint {
pub enum SymbolHintKind {
TypeHint(String),
VarHint(String),
SchemaHint(String),
}

impl SymbolData {
Expand Down
12 changes: 12 additions & 0 deletions kclvm/tools/src/LSP/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ fn get_hint_label(hint: &SymbolHint) -> (InlayHintLabelPart, LspPosition) {
},
lsp_pos(&hint.pos),
),
SymbolHintKind::SchemaHint(schema) => (
InlayHintLabelPart {
value: format!("{schema} "),
..Default::default()
},
lsp_pos(&hint.pos),
),
}
}

Expand Down Expand Up @@ -123,4 +130,9 @@ mod tests {
test_schema_arg_hint,
"src/test_data/inlay_hints/schema_args/schema_args_hint.k"
);

inlay_hints_test_snapshot!(
test_schema_config_hint,
"src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
source: tools/src/LSP/src/inlay_hints.rs
assertion_line: 134
expression: "format!(\"{:#?}\", res)"
---
Some(
[
InlayHint {
position: Position {
line: 3,
character: 13,
},
label: LabelParts(
[
InlayHintLabelPart {
value: "People ",
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: 16,
character: 13,
},
label: LabelParts(
[
InlayHintLabelPart {
value: "Config ",
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: 14,
character: 12,
},
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,20 @@
schema People:
name: str

n1: People = {
name = "kcl"
}

schema Name:
name: str
config: Config

schema Config:
count: int

n2: Name = {
name = "foo"
config: {
count: 1
}
}
Loading