Skip to content

Commit 19efb90

Browse files
committed
conditional attrs for union types
Signed-off-by: shruti2522 <shruti.apc01@gmail.com> make fmt Signed-off-by: shruti2522 <shruti.apc01@gmail.com> conditional attrs for union types Signed-off-by: shruti2522 <shruti.apc01@gmail.com>
1 parent 5b0aca9 commit 19efb90

File tree

1 file changed

+31
-85
lines changed

1 file changed

+31
-85
lines changed

kclvm/tools/src/LSP/src/completion.rs

Lines changed: 31 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,25 @@ fn completion_dot(
255255
Some(def_ref) => {
256256
if let Some(def) = gs.get_symbols().get_symbol(def_ref) {
257257
let module_info = gs.get_packages().get_module_info(&pos.filename);
258-
let attrs = def.get_all_attributes(gs.get_symbols(), module_info);
258+
let attrs = match &def.get_sema_info().ty {
259+
Some(symbol_ty) => match &symbol_ty.kind {
260+
TypeKind::Union(union_types) => {
261+
let mut union_attrs = vec![];
262+
for union_ty in union_types {
263+
if let TypeKind::StrLit(_) | TypeKind::Str = &union_ty.kind {
264+
union_attrs.extend(gs.get_symbols().get_type_all_attribute(
265+
union_ty,
266+
"",
267+
module_info,
268+
));
269+
}
270+
}
271+
union_attrs
272+
}
273+
_ => def.get_all_attributes(gs.get_symbols(), module_info),
274+
},
275+
None => def.get_all_attributes(gs.get_symbols(), module_info),
276+
};
259277
for attr in attrs {
260278
let attr_def = gs.get_symbols().get_symbol(attr);
261279
if let Some(attr_def) = attr_def {
@@ -277,6 +295,18 @@ fn completion_dot(
277295
};
278296
let kind = match &def.get_sema_info().ty {
279297
Some(symbol_ty) => match &symbol_ty.kind {
298+
TypeKind::Union(union_types) => {
299+
if union_types.iter().any(|ut| {
300+
matches!(
301+
&ut.kind,
302+
TypeKind::StrLit(_) | TypeKind::Str
303+
)
304+
}) {
305+
Some(KCLCompletionItemKind::Function)
306+
} else {
307+
type_to_item_kind(attr_ty)
308+
}
309+
}
280310
TypeKind::Schema(_) => {
281311
Some(KCLCompletionItemKind::SchemaAttr)
282312
}
@@ -319,90 +349,6 @@ fn completion_dot(
319349
None => {}
320350
}
321351

322-
if let Some(scope) = gs.look_up_scope(pos) {
323-
if let Some(defs) = gs.get_all_defs_in_scope(scope) {
324-
for symbol_ref in defs {
325-
if let Some(def) = gs.get_symbols().get_symbol(symbol_ref) {
326-
let sema_info = def.get_sema_info();
327-
if let Some(ty) = &sema_info.ty {
328-
if let TypeKind::Union(union_types) = &ty.kind {
329-
for union_ty in union_types {
330-
if let TypeKind::StrLit(_) | TypeKind::Str = &union_ty.kind {
331-
let module_info =
332-
gs.get_packages().get_module_info(&pos.filename);
333-
let str_attrs = gs.get_symbols().get_type_all_attribute(
334-
&union_ty,
335-
"",
336-
module_info,
337-
);
338-
for str_attr in str_attrs {
339-
let str_attr_def = gs.get_symbols().get_symbol(str_attr);
340-
if let Some(str_attr_def) = str_attr_def {
341-
let str_sema_info = str_attr_def.get_sema_info();
342-
let attr_name = str_attr_def.get_name();
343-
match &str_sema_info.ty {
344-
Some(str_attr_ty) => {
345-
let label: String = match &str_attr_ty.kind {
346-
TypeKind::Function(func_ty) => {
347-
func_ty_complete_label(
348-
&attr_name, func_ty,
349-
)
350-
}
351-
_ => attr_name.clone(),
352-
};
353-
let insert_text = match &str_attr_ty.kind {
354-
TypeKind::Function(func_ty) => {
355-
Some(func_ty_complete_insert_text(
356-
&attr_name, func_ty,
357-
))
358-
}
359-
_ => None,
360-
};
361-
let kind =
362-
Some(KCLCompletionItemKind::Function);
363-
let documentation = match &str_sema_info.doc {
364-
Some(doc) => {
365-
if doc.is_empty() {
366-
None
367-
} else {
368-
Some(doc.clone())
369-
}
370-
}
371-
None => None,
372-
};
373-
items.insert(KCLCompletionItem {
374-
label,
375-
detail: Some(format!(
376-
"{}: {}",
377-
attr_name,
378-
str_attr_ty.ty_str()
379-
)),
380-
documentation,
381-
kind,
382-
insert_text,
383-
});
384-
}
385-
None => {
386-
items.insert(KCLCompletionItem {
387-
label: attr_name,
388-
detail: None,
389-
documentation: None,
390-
kind: None,
391-
insert_text: None,
392-
});
393-
}
394-
}
395-
}
396-
}
397-
}
398-
}
399-
}
400-
}
401-
}
402-
}
403-
}
404-
}
405-
406352
Some(into_completion_items(&items).into())
407353
}
408354

0 commit comments

Comments
 (0)