Skip to content

Commit

Permalink
conditional attrs for union types
Browse files Browse the repository at this point in the history
Signed-off-by: shruti2522 <[email protected]>
  • Loading branch information
shruti2522 committed Jun 12, 2024
1 parent 5b0aca9 commit 94faaf1
Showing 1 changed file with 32 additions and 86 deletions.
118 changes: 32 additions & 86 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,25 @@ fn completion_dot(
Some(def_ref) => {
if let Some(def) = gs.get_symbols().get_symbol(def_ref) {
let module_info = gs.get_packages().get_module_info(&pos.filename);
let attrs = def.get_all_attributes(gs.get_symbols(), module_info);
let attrs = match &def.get_sema_info().ty {
Some(symbol_ty) => match &symbol_ty.kind {
TypeKind::Union(union_types) => {
let mut union_attrs = vec![];
for union_ty in union_types {
if let TypeKind::StrLit(_) | TypeKind::Str = &union_ty.kind {
union_attrs.extend(gs.get_symbols().get_type_all_attribute(
union_ty,
"",
module_info,
));
}
}
union_attrs
}
_ => def.get_all_attributes(gs.get_symbols(), module_info),
},
None => def.get_all_attributes(gs.get_symbols(), module_info),
};
for attr in attrs {
let attr_def = gs.get_symbols().get_symbol(attr);
if let Some(attr_def) = attr_def {
Expand All @@ -277,6 +295,18 @@ fn completion_dot(
};
let kind = match &def.get_sema_info().ty {
Some(symbol_ty) => match &symbol_ty.kind {
TypeKind::Union(union_types) => {
if union_types.iter().any(|ut| {
matches!(
&ut.kind,
TypeKind::StrLit(_) | TypeKind::Str
)
}) {
Some(KCLCompletionItemKind::Function)
} else {
type_to_item_kind(attr_ty)
}
}
TypeKind::Schema(_) => {
Some(KCLCompletionItemKind::SchemaAttr)
}
Expand Down Expand Up @@ -307,7 +337,7 @@ fn completion_dot(
label: name,
detail: None,
documentation: None,
kind: None,
kind: Some(KCLCompletionItemKind::Function),
insert_text: None,
});
}
Expand All @@ -319,90 +349,6 @@ fn completion_dot(
None => {}
}

if let Some(scope) = gs.look_up_scope(pos) {
if let Some(defs) = gs.get_all_defs_in_scope(scope) {
for symbol_ref in defs {
if let Some(def) = gs.get_symbols().get_symbol(symbol_ref) {
let sema_info = def.get_sema_info();
if let Some(ty) = &sema_info.ty {
if let TypeKind::Union(union_types) = &ty.kind {
for union_ty in union_types {
if let TypeKind::StrLit(_) | TypeKind::Str = &union_ty.kind {
let module_info =
gs.get_packages().get_module_info(&pos.filename);
let str_attrs = gs.get_symbols().get_type_all_attribute(
&union_ty,
"",
module_info,
);
for str_attr in str_attrs {
let str_attr_def = gs.get_symbols().get_symbol(str_attr);
if let Some(str_attr_def) = str_attr_def {
let str_sema_info = str_attr_def.get_sema_info();
let attr_name = str_attr_def.get_name();
match &str_sema_info.ty {
Some(str_attr_ty) => {
let label: String = match &str_attr_ty.kind {
TypeKind::Function(func_ty) => {
func_ty_complete_label(
&attr_name, func_ty,
)
}
_ => attr_name.clone(),
};
let insert_text = match &str_attr_ty.kind {
TypeKind::Function(func_ty) => {
Some(func_ty_complete_insert_text(
&attr_name, func_ty,
))
}
_ => None,
};
let kind =
Some(KCLCompletionItemKind::Function);
let documentation = match &str_sema_info.doc {
Some(doc) => {
if doc.is_empty() {
None
} else {
Some(doc.clone())
}
}
None => None,
};
items.insert(KCLCompletionItem {
label,
detail: Some(format!(
"{}: {}",
attr_name,
str_attr_ty.ty_str()
)),
documentation,
kind,
insert_text,
});
}
None => {
items.insert(KCLCompletionItem {
label: attr_name,
detail: None,
documentation: None,
kind: None,
insert_text: None,
});
}
}
}
}
}
}
}
}
}
}
}
}

Some(into_completion_items(&items).into())
}

Expand Down

0 comments on commit 94faaf1

Please sign in to comment.