diff --git a/src/core/hdlparam.rs b/src/core/hdlparam.rs index eb3b99c..d848a66 100644 --- a/src/core/hdlparam.rs +++ b/src/core/hdlparam.rs @@ -406,6 +406,19 @@ impl HdlParam { None } + /// 遍历 path_string 下的所有 module,并根据条件返回 + pub fn walk_module(&self, path_string: &str, condition: impl Fn(&Module) -> bool) -> Option { + 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,并根据条件返回 pub fn walk_instantiation(&self, path_string: &str, condition: impl Fn(&Module, &Instance) -> bool) -> Option { let path_to_hdl_file = self.path_to_hdl_file.read().unwrap(); diff --git a/src/hover/sv.rs b/src/hover/sv.rs index cfa7b1d..f19790a 100644 --- a/src/hover/sv.rs +++ b/src/hover/sv.rs @@ -41,7 +41,12 @@ pub fn hover(server: &LSPServer, params: &HoverParams) -> Option { // match module name if let Some(hover) = hover_module_declaration(server, &token, &language_id) { - return Some(hover); + // info!("[LSPServer] in hover: get module hover"); + + if hover_for_module(server, pos, doc) { + // info!("[LSPServer] in hover: it is instance"); + return Some(hover); + } } // match digit 5'b00110 @@ -290,3 +295,25 @@ fn hover_common_symbol( 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 + } +}