diff --git a/src/completion/sv.rs b/src/completion/sv.rs index 0d87213..9e5bb34 100644 --- a/src/completion/sv.rs +++ b/src/completion/sv.rs @@ -199,6 +199,23 @@ fn get_completion_token(text: &Rope, line: RopeSlice, pos: Position) -> String { } } +fn make_primitives_instantiation_code(text: &str) -> String { + let mut instantiations = text.lines() + .filter(|line| !line.trim().is_empty() && !line.trim().starts_with("//")) + .collect::>(); + // remove fake module and endmodule + instantiations.remove(0); + instantiations.pop(); + + instantiations.iter().map(|line| { + let trimmed = line.trim_start(); + if trimmed.contains('.') { + format!("\t{}", trimmed) + } else { + trimmed.to_string() + } + }).collect::>().join("\n") +} fn make_instantiation_code(module: &crate::core::hdlparam::Module) -> String { @@ -270,27 +287,41 @@ fn make_module_completions( let prefix = token.to_string().to_lowercase(); let path_to_files = server.srcs.hdl_param.path_to_hdl_file.read().unwrap(); - + // 遍历 hdlparam 中所有的 modules for (path_string, hdl_file) in path_to_files.iter() { for module in &hdl_file.fast.content { if !module.name.to_string().to_lowercase().starts_with(&prefix) { continue; } - let insert_text = make_instantiation_code(module); - let module_profile = make_module_profile_code(module); + let (insert_text, module_profile, define_info) = if hdl_file.fast.file_type == "primitives" { + 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(), + format!("[Definition] Primitive module: {}", module.name) + ) + } else { + continue; + } + } else { + let path_uri = Url::from_file_path(path_string.to_string()).unwrap().to_string(); + let def_row = module.range.start.line; + let def_col = module.range.start.character; - let path_uri = Url::from_file_path(path_string.to_string()).unwrap().to_string(); - let def_row = module.range.start.line; - let def_col = module.range.start.character; - let define_info = format!("Go to [Definition]({path_uri}#L{def_row}:{def_col})"); + ( + make_instantiation_code(module), + make_module_profile_code(module), + format!("Go to [Definition]({path_uri}#L{def_row}:{def_col})") + ) + }; let module_profile = MarkupContent { kind: MarkupKind::Markdown, value: format!("```{}\n{}\n```\n{}", language_id, module_profile, define_info) }; - let item = CompletionItem { label: module.name.to_string(), detail: Some("module instantiation".to_string()),