Skip to content

Commit

Permalink
feat: lsp hover for dict into schema expr key (#1343)
Browse files Browse the repository at this point in the history
* feat: lsp hover for dict into schema expr key

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

* chore: move insta to dev-dependencies

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

---------

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored May 21, 2024
1 parent 9e1ff13 commit 00807ce
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
}
self.ctx.maybe_def = true;
self.walk_identifier_expr(target)?;

if let Some(target_ty) = self.ctx.node_ty_map.get(&self.ctx.get_node_key(&target.id)) {
match &target_ty.kind {
TypeKind::Schema(_) => {
let schema_symbol = self
.gs
.get_symbols()
.get_type_symbol(&target_ty, self.get_current_module_info())
.ok_or(anyhow!("schema_symbol not found"))?;
self.ctx.current_schema_symbol = Some(schema_symbol);
}
_ => {}
}
}
self.ctx.maybe_def = false;
}
self.walk_type_expr(assign_stmt.ty.as_ref().map(|ty| ty.as_ref()))?;
Expand Down Expand Up @@ -861,7 +875,7 @@ impl<'ctx> AdvancedResolver<'ctx> {
}
None => Ok(None),
},
some => some,
res => res,
}
}

Expand Down
3 changes: 3 additions & 0 deletions kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ proc_macro_crate = { path = "../../benches/proc_macro_crate" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.37.0", features = ["full"] }

[dev-dependencies]
insta = "1.8.0"
30 changes: 30 additions & 0 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,34 @@ mod tests {
_ => unreachable!("test error"),
}
}

#[test]
#[bench_test]
fn dict_key_in_schema() {
let (file, program, _, gs) =
compile_test_file("src/test_data/hover_test/dict_key_in_schema/dict_key_in_schema.k");
let pos = KCLPos {
filename: file.clone(),
line: 5,
column: Some(5),
};
let got = hover(&program, &pos, &gs).unwrap();
insta::assert_snapshot!(format!("{:?}", got));

let pos = KCLPos {
filename: file.clone(),
line: 9,
column: Some(5),
};
let got = hover(&program, &pos, &gs).unwrap();
insta::assert_snapshot!(format!("{:?}", got));

let pos = KCLPos {
filename: file.clone(),
line: 13,
column: Some(5),
};
let got = hover(&program, &pos, &gs).unwrap();
insta::assert_snapshot!(format!("{:?}", got));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/hover.rs
expression: "format!(\"{:?}\", got)"
---
Hover { contents: Scalar(String("name: int")), range: None }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/hover.rs
expression: "format!(\"{:?}\", got)"
---
Hover { contents: Scalar(String("name: int")), range: None }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tools/src/LSP/src/hover.rs
expression: "format!(\"{:?}\", got)"
---
Hover { contents: Scalar(String("name: int")), range: None }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema Name:
name: int

n1: Name = {
name = 1
}

n2 = Name{
name: 1
}

n3: Name = Name{
name: 1
}

0 comments on commit 00807ce

Please sign in to comment.