fix module range logic

This commit is contained in:
light-ly 2024-10-01 00:44:20 +08:00
parent 8ae9a09738
commit 79f505177d
2 changed files with 41 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::f64::consts::E;
use std::sync::RwLock;
use serde::{Deserialize, Serialize};
@ -187,6 +188,13 @@ impl FastHdlparam {
self.content.push(module);
}
pub fn update_module_range(&mut self, name: &str, end_line: u32, end_character: u32) {
if let Some(matched_module) = self.content.iter_mut().find(|module| module.name == name) {
matched_module.range.end.line = end_line;
matched_module.range.end.character = end_character;
}
}
pub fn add_parameter(&mut self, name: &str, net_type: &str, init: &str,
start_line: u32, start_character: u32, end_line: u32, end_character: u32) {
if let Some(last_module) = self.content.last_mut() {

View File

@ -3,6 +3,7 @@ use std::io::BufRead;
use std::io::BufReader;
use std::path::PathBuf;
use anyhow::Error;
use regex::Regex;
use ropey::Rope;
use tower_lsp::lsp_types::Url;
use sv_parser::{unwrap_node, Locate, RefNode, SyntaxTree};
@ -312,6 +313,8 @@ pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> Re
}
}
update_module_range(&path, &mut hdlparam);
Ok(hdlparam)
}
@ -573,6 +576,36 @@ fn get_includes(path: &PathBuf) -> Vec<crate::core::fast_hdlparam::Include> {
includes
}
fn update_module_range(path: &PathBuf, hdlparam: &mut FastHdlparam) {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let re_module = Regex::new(r"^\s*module\s+(\w+)\s*\(").unwrap();
let re_endmodule = Regex::new(r"^\s*endmodule\s*$").unwrap();
let mut current_offset = 0;
let mut module_stack: Vec<String> = Vec::new();
for (line_number, line_content) in reader.lines().enumerate() {
match line_content {
Ok(line) => {
let line_length = line.len() + 1; // +1 for newline character
current_offset += line_length;
if let Some(captures) = re_module.captures(&line) {
let module_name = captures.get(1).unwrap().as_str().to_string();
module_stack.push(module_name.clone());
} else if re_endmodule.is_match(&line) {
if let Some(module_name) = module_stack.pop() {
hdlparam.update_module_range(&module_name, (line_number + 1) as u32, current_offset as u32);
// println!("Module {} ends.", module_name);
}
}
}
_ => ()
}
}
}
fn get_column_by_offset(content: &[String], offset: usize) -> usize {
let mut current_offset = 0;