add primitives auto instantiation

This commit is contained in:
light-ly 2024-11-13 22:24:07 +08:00
parent 290d1aec05
commit 82b4f6f092

View File

@ -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::<Vec<&str>>();
// 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::<Vec<String>>().join("\n")
}
fn make_instantiation_code(module: &crate::core::hdlparam::Module) -> String { 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 prefix = token.to_string().to_lowercase();
let path_to_files = server.srcs.hdl_param.path_to_hdl_file.read().unwrap(); let path_to_files = server.srcs.hdl_param.path_to_hdl_file.read().unwrap();
// 遍历 hdlparam 中所有的 modules // 遍历 hdlparam 中所有的 modules
for (path_string, hdl_file) in path_to_files.iter() { for (path_string, hdl_file) in path_to_files.iter() {
for module in &hdl_file.fast.content { for module in &hdl_file.fast.content {
if !module.name.to_string().to_lowercase().starts_with(&prefix) { if !module.name.to_string().to_lowercase().starts_with(&prefix) {
continue; continue;
} }
let insert_text = make_instantiation_code(module); let (insert_text, module_profile, define_info) = if hdl_file.fast.file_type == "primitives" {
let module_profile = make_module_profile_code(module); 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; make_instantiation_code(module),
let def_col = module.range.start.character; make_module_profile_code(module),
let define_info = format!("Go to [Definition]({path_uri}#L{def_row}:{def_col})"); format!("Go to [Definition]({path_uri}#L{def_row}:{def_col})")
)
};
let module_profile = MarkupContent { let module_profile = MarkupContent {
kind: MarkupKind::Markdown, kind: MarkupKind::Markdown,
value: format!("```{}\n{}\n```\n{}", language_id, module_profile, define_info) value: format!("```{}\n{}\n```\n{}", language_id, module_profile, define_info)
}; };
let item = CompletionItem { let item = CompletionItem {
label: module.name.to_string(), label: module.name.to_string(),
detail: Some("module instantiation".to_string()), detail: Some("module instantiation".to_string()),