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

fix: fix inherit schema attrs hover #1323

Merged
merged 2 commits into from
May 16, 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
22 changes: 21 additions & 1 deletion kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ impl SymbolData {
}
}

pub fn get_attr_symbol(&self, id: SymbolRef) -> Option<&AttributeSymbol> {
if matches!(id.get_kind(), SymbolKind::Attribute) {
self.attributes.get(id.get_id())
} else {
None
}
}

pub fn get_symbol(&self, id: SymbolRef) -> Option<&KCLSymbol> {
match id.get_kind() {
SymbolKind::Schema => self
Expand Down Expand Up @@ -1095,6 +1103,7 @@ pub struct AttributeSymbol {
pub(crate) end: Position,
pub(crate) owner: SymbolRef,
pub(crate) sema_info: KCLSymbolSemanticInfo,
pub(crate) is_optional: bool,
}

impl Symbol for AttributeSymbol {
Expand Down Expand Up @@ -1198,16 +1207,27 @@ impl Symbol for AttributeSymbol {
}

impl AttributeSymbol {
pub fn new(name: String, start: Position, end: Position, owner: SymbolRef) -> Self {
pub fn new(
name: String,
start: Position,
end: Position,
owner: SymbolRef,
is_optional: bool,
) -> Self {
Self {
id: None,
name,
start,
end,
sema_info: KCLSymbolSemanticInfo::default(),
owner,
is_optional,
}
}

pub fn is_optional(&self) -> bool {
self.is_optional
}
}
#[allow(unused)]
#[derive(Debug, Clone)]
Expand Down
8 changes: 7 additions & 1 deletion kclvm/sema/src/namer/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for Namer<'ctx> {
let (start_pos, end_pos): Range = schema_attr.name.get_span_pos();
let owner = self.ctx.owner_symbols.last().unwrap().clone();
let attribute_ref = self.gs.get_symbols_mut().alloc_attribute_symbol(
AttributeSymbol::new(schema_attr.name.node.clone(), start_pos, end_pos, owner),
AttributeSymbol::new(
schema_attr.name.node.clone(),
start_pos,
end_pos,
owner,
schema_attr.is_optional,
),
self.ctx.get_node_key(&schema_attr.name.id),
);
Some(vec![attribute_ref])
Expand Down
104 changes: 70 additions & 34 deletions kclvm/tools/src/LSP/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use kclvm_error::Position as KCLPos;
use kclvm_sema::{
builtin::BUILTIN_DECORATORS,
core::global_state::GlobalState,
ty::{FunctionType, SchemaType},
ty::{FunctionType, SchemaType, ANY_TYPE_STR},
};
use lsp_types::{Hover, HoverContents, MarkedString};

Expand All @@ -22,8 +22,51 @@ pub(crate) fn hover(
Some(def_ref) => match gs.get_symbols().get_symbol(def_ref) {
Some(obj) => match def_ref.get_kind() {
kclvm_sema::core::symbol::SymbolKind::Schema => match &obj.get_sema_info().ty {
Some(schema_ty) => {
docs.extend(build_schema_hover_content(&schema_ty.into_schema_type()));
Some(ty) => {
// Build hover content for schema definition
// Schema Definition hover
// ```
// pkg
// schema Foo(Base)[param: type]
// -----------------
// doc
// -----------------
// Attributes:
// attr1: type
// attr2? type
// ```
let schema_ty = ty.into_schema_type();
docs.push(schema_ty.schema_ty_signature_str());
if !schema_ty.doc.is_empty() {
docs.push(schema_ty.doc.clone());
}

// The attr of schema_ty does not contain the attrs from inherited base schema.
// Use the api provided by GlobalState to get all attrs
let module_info = gs.get_packages().get_module_info(&kcl_pos.filename);
let schema_attrs = obj.get_all_attributes(gs.get_symbols(), module_info);
let mut attrs = vec!["Attributes:".to_string()];
for schema_attr in schema_attrs {
if let kclvm_sema::core::symbol::SymbolKind::Attribute =
schema_attr.get_kind()
{
let attr = gs.get_symbols().get_symbol(schema_attr).unwrap();
let name = attr.get_name();
let attr_symbol =
gs.get_symbols().get_attr_symbol(schema_attr).unwrap();
let attr_ty_str = match &attr.get_sema_info().ty {
Some(ty) => ty.ty_str(),
None => ANY_TYPE_STR.to_string(),
};
attrs.push(format!(
"{}{}: {}",
name,
if attr_symbol.is_optional() { "?" } else { "" },
attr_ty_str,
));
}
}
docs.push(attrs.join("\n\n"));
}
_ => {}
},
Expand Down Expand Up @@ -100,37 +143,6 @@ fn docs_to_hover(docs: Vec<String>) -> Option<lsp_types::Hover> {
}
}

// Build hover content for schema definition
// Schema Definition hover
// ```
// pkg
// schema Foo(Base)[param: type]
// -----------------
// doc
// -----------------
// Attributes:
// attr1: type
// attr2? type
// ```
fn build_schema_hover_content(schema_ty: &SchemaType) -> Vec<String> {
let mut docs = vec![];
docs.push(schema_ty.schema_ty_signature_str());
if !schema_ty.doc.is_empty() {
docs.push(schema_ty.doc.clone());
}
let mut attrs = vec!["Attributes:".to_string()];
for (name, attr) in &schema_ty.attrs {
attrs.push(format!(
"{}{}: {}",
name,
if attr.is_optional { "?" } else { "" },
attr.ty.ty_str(),
));
}
docs.push(attrs.join("\n\n"));
docs
}

// Build hover content for function call
// ```
// pkg
Expand Down Expand Up @@ -574,4 +586,28 @@ mod tests {
_ => unreachable!("test error"),
}
}

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

let expect_content = vec![
MarkedString::String("__main__\n\nschema Data1\\[m: {str:str}](Data)".to_string()),
MarkedString::String("Attributes:\n\nname: str\n\nage: int".to_string()),
];

match got.contents {
lsp_types::HoverContents::Array(vec) => {
assert_eq!(vec, expect_content);
}
_ => unreachable!("test error"),
}
}
}
7 changes: 7 additions & 0 deletions kclvm/tools/src/LSP/src/test_data/hover_test/inherit.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
schema Data:
name: str = "1"
age: int

schema Data1[m: {str:str}](Data):
name = m.name

Loading