From ec5e1f19c4400165df2e28f197efbf375721b2ec Mon Sep 17 00:00:00 2001 From: light-ly Date: Wed, 13 Nov 2024 22:45:18 +0800 Subject: [PATCH] add primitives port/param auto completion --- src/completion/feature.rs | 154 +++++++++++++++++++++++++------------- src/completion/sv.rs | 15 +++- 2 files changed, 113 insertions(+), 56 deletions(-) diff --git a/src/completion/feature.rs b/src/completion/feature.rs index fe81758..9aaeeea 100644 --- a/src/completion/feature.rs +++ b/src/completion/feature.rs @@ -167,35 +167,58 @@ fn get_position_port_param_completion( // 补全当前 module 的所有 param if let Some((inst_module, file_type, _)) = hdl_param.find_module_context_by_name(&instance.inst_type) { let mut completion_items = Vec::::new(); - for param in inst_module.params { - let label_details = CompletionItemLabelDetails { - detail: None, - ..CompletionItemLabelDetails::default() - }; + match file_type.as_str() { + "primitives" => { + if let Some(primitives_inst) = inst_module.instances.first() { + for param_assignment in &primitives_inst.intstparam_assignments { + let label_details = CompletionItemLabelDetails { + detail: None, + ..CompletionItemLabelDetails::default() + }; - let param_desc = match file_type.as_str() { - "common" => { - param.to_vlog_description() - } - "ip" => { - param.to_vhdl_description() - } - "primitives" => { - param.to_vlog_description() - } - _ => { - param.to_vlog_description() - } - }; + let name = param_assignment.parameter.clone().unwrap(); + let param_desc = format!("parameter {}", name); - let c_item = CompletionItem { - label: param.name, - detail: Some(param_desc), - label_details: Some(label_details), - kind: Some(CompletionItemKind::TYPE_PARAMETER), - ..CompletionItem::default() - }; - completion_items.push(c_item); + let c_item = CompletionItem { + label: name, + detail: Some(param_desc), + label_details: Some(label_details), + kind: Some(CompletionItemKind::TYPE_PARAMETER), + ..CompletionItem::default() + }; + completion_items.push(c_item); + } + } + } + _ => { + for param in inst_module.params { + let label_details = CompletionItemLabelDetails { + detail: None, + ..CompletionItemLabelDetails::default() + }; + + let param_desc = match file_type.as_str() { + "common" => { + param.to_vlog_description() + } + "ip" => { + param.to_vhdl_description() + } + _ => { + param.to_vlog_description() + } + }; + + let c_item = CompletionItem { + label: param.name, + detail: Some(param_desc), + label_details: Some(label_details), + kind: Some(CompletionItemKind::TYPE_PARAMETER), + ..CompletionItem::default() + }; + completion_items.push(c_item); + } + } } return Some(CompletionList { is_incomplete: false, @@ -210,35 +233,58 @@ fn get_position_port_param_completion( if port_range.contains(pos) { if let Some((inst_module, file_type, _)) = hdl_param.find_module_context_by_name(&instance.inst_type) { let mut completion_items = Vec::::new(); - for port in inst_module.ports { - let label_details = CompletionItemLabelDetails { - detail: None, - ..CompletionItemLabelDetails::default() - }; + match file_type.as_str() { + "primitives" => { + if let Some(primitives_inst) = inst_module.instances.first() { + for port_assignment in &primitives_inst.intstport_assignments { + let label_details = CompletionItemLabelDetails { + detail: None, + ..CompletionItemLabelDetails::default() + }; - let port_desc = match file_type.as_str() { - "common" => { - port.to_vlog_description() - } - "ip" => { - port.to_vhdl_description() - } - "primitives" => { - port.to_vlog_description() - } - _ => { - port.to_vlog_description() - } - }; + let name = port_assignment.port.clone().unwrap(); + let port_desc = format!("parameter {}", name); - let c_item = CompletionItem { - label: port.name, - detail: Some(port_desc), - label_details: Some(label_details), - kind: Some(CompletionItemKind::PROPERTY), - ..CompletionItem::default() - }; - completion_items.push(c_item); + let c_item = CompletionItem { + label: name, + detail: Some(port_desc), + label_details: Some(label_details), + kind: Some(CompletionItemKind::PROPERTY), + ..CompletionItem::default() + }; + completion_items.push(c_item); + } + } + } + _ => { + for port in inst_module.ports { + let label_details = CompletionItemLabelDetails { + detail: None, + ..CompletionItemLabelDetails::default() + }; + + let port_desc = match file_type.as_str() { + "common" => { + port.to_vlog_description() + } + "ip" => { + port.to_vhdl_description() + } + _ => { + port.to_vlog_description() + } + }; + + let c_item = CompletionItem { + label: port.name, + detail: Some(port_desc), + label_details: Some(label_details), + kind: Some(CompletionItemKind::PROPERTY), + ..CompletionItem::default() + }; + completion_items.push(c_item); + } + } } return Some(CompletionList { is_incomplete: false, diff --git a/src/completion/sv.rs b/src/completion/sv.rs index 9e5bb34..ec59f10 100644 --- a/src/completion/sv.rs +++ b/src/completion/sv.rs @@ -217,6 +217,17 @@ fn make_primitives_instantiation_code(text: &str) -> String { }).collect::>().join("\n") } +fn make_primitives_module_profile_code(text: &str) -> String { + let mut lines: Vec<&str> = text.split_inclusive('\n').collect(); + + if lines.len() > 1 { + lines.remove(0); + lines.pop(); + } + + lines.join("") +} + fn make_instantiation_code(module: &crate::core::hdlparam::Module) -> String { // makeVlogParamAssignments 参数信息列表 @@ -298,8 +309,8 @@ fn make_module_completions( let primitive_map = server.srcs.primitive_text.name_to_text.read().unwrap(); if let Some(text) = primitive_map.get(&module.name) { ( - make_primitives_instantiation_code(&text), - (*text).clone(), + make_primitives_instantiation_code(text), + make_primitives_module_profile_code(text), format!("[Definition] Primitive module: {}", module.name) ) } else {