This commit is contained in:
锦恢 2024-10-26 22:33:09 +08:00
commit 4bde6263a3
2 changed files with 41 additions and 1 deletions

View File

@ -438,6 +438,19 @@ impl HdlParam {
None None
} }
/// 遍历 path_string 下的所有 module并根据条件返回
pub fn walk_module(&self, path_string: &str, condition: impl Fn(&Module) -> bool) -> Option<Module> {
let path_to_hdl_file = self.path_to_hdl_file.read().unwrap();
if let Some(hdl_file) = path_to_hdl_file.get(path_string) {
for def_module in &hdl_file.fast.content {
if condition(def_module) {
return Some(def_module.clone());
}
}
}
None
}
/// 遍历 path_string 下的所有 instance并根据条件返回 /// 遍历 path_string 下的所有 instance并根据条件返回
pub fn walk_instantiation(&self, path_string: &str, condition: impl Fn(&Module, &Instance) -> bool) -> Option<Instance> { pub fn walk_instantiation(&self, path_string: &str, condition: impl Fn(&Module, &Instance) -> bool) -> Option<Instance> {
let path_to_hdl_file = self.path_to_hdl_file.read().unwrap(); let path_to_hdl_file = self.path_to_hdl_file.read().unwrap();

View File

@ -41,8 +41,13 @@ pub fn hover(server: &LSPServer, params: &HoverParams) -> Option<Hover> {
// match module name // match module name
if let Some(hover) = hover_module_declaration(server, &token, &language_id) { if let Some(hover) = hover_module_declaration(server, &token, &language_id) {
// info!("[LSPServer] in hover: get module hover");
if hover_for_module(server, pos, doc) {
// info!("[LSPServer] in hover: it is instance");
return Some(hover); return Some(hover);
} }
}
// match digit 5'b00110 // match digit 5'b00110
if let Some(hover) = hover_format_digit(&line_text, pos, &language_id) { if let Some(hover) = hover_format_digit(&line_text, pos, &language_id) {
@ -290,3 +295,25 @@ fn hover_common_symbol(
make_hover_with_comment(&file.text, def_line, &language_id, false) make_hover_with_comment(&file.text, def_line, &language_id, false)
} }
fn hover_for_module(server: &LSPServer, pos: Position, doc: &Url) -> bool {
let pathbuf = PathBuf::from_str(doc.path()).unwrap();
let pathbuf = to_escape_path(&pathbuf);
let path_string = pathbuf.to_str().unwrap().replace("\\", "/");
let hdlparam = server.srcs.hdl_param.clone();
let find_instance_range = |_: &Module, instance: &Instance| {
pos.line + 1 == instance.range.start.line
};
let find_module_range = |module: &Module| {
pos.line + 1 == module.range.start.line
};
if let Some(_) = hdlparam.walk_module(&path_string, find_module_range) {
// info!("[LSPServer] in hover: it is module");
true
} else if let Some(_) = hdlparam.walk_instantiation(&path_string, find_instance_range) {
// info!("[LSPServer] in hover: it is instance");
true
} else {
// info!("[LSPServer] in hover: it is not instance");
false
}
}