From ca272a67b8e1f47db39cae4d8960b07c020b3441 Mon Sep 17 00:00:00 2001 From: LSTM-Kirigaya <1193466151@qq.com> Date: Wed, 25 Sep 2024 14:58:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=20loop=20=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=20fast=20=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/definition.rs | 3 + src/lib.rs | 1 + src/sources.rs | 47 ++++++----- src/test/mod.rs | 196 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 226 insertions(+), 21 deletions(-) create mode 100644 src/test/mod.rs diff --git a/src/definition.rs b/src/definition.rs index fc31e74..6128720 100644 --- a/src/definition.rs +++ b/src/definition.rs @@ -2,6 +2,7 @@ use crate::definition::extract_defs::get_ident; use crate::server::LSPServer; use crate::sources::LSPSupport; +use log::info; use ropey::{Rope, RopeSlice}; use sv_parser::*; use tower_lsp::lsp_types::*; @@ -23,6 +24,8 @@ impl LSPServer { let file = file.read().ok()?; let token: String = get_definition_token(file.text.line(pos.line as usize), pos); + info!("definition token: {:?}", token); + let scope_tree = self.srcs.scope_tree.read().ok()?; let def = scope_tree diff --git a/src/lib.rs b/src/lib.rs index 18a1fed..00ca923 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,3 +9,4 @@ pub mod server; pub mod sources; pub mod support; pub mod custom_request; +pub mod test; \ No newline at end of file diff --git a/src/sources.rs b/src/sources.rs index a9667b8..a005ea2 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -36,6 +36,8 @@ macro_rules! unwrap_result { impl LSPServer { pub fn did_open(&self, params: DidOpenTextDocumentParams) -> PublishDiagnosticsParams { + info!("[LSPServer] did open"); + let document: TextDocumentItem = params.text_document; let uri = document.uri.clone(); @@ -62,7 +64,8 @@ impl LSPServer { } pub fn did_change(&self, params: DidChangeTextDocumentParams) { - debug!("did_change: {}", ¶ms.text_document.uri); + info!("[LSPServer] did change"); + let file_id = self.srcs.get_id(¶ms.text_document.uri); let file = self.srcs.get_file(file_id).unwrap(); let mut file = file.write().unwrap(); @@ -87,6 +90,8 @@ impl LSPServer { } pub fn did_save(&self, params: DidSaveTextDocumentParams) -> PublishDiagnosticsParams { + info!("[LSPServer] did save"); + let urls = self.srcs.names.read().unwrap().keys().cloned().collect(); let file_id = self.srcs.get_id(¶ms.text_document.uri); let file = self.srcs.get_file(file_id).unwrap(); @@ -241,26 +246,26 @@ impl Sources { }; // 计算 fast - if let Some(syntax_tree) = &syntax_tree { - let path = PathBuf::from(uri.path().to_string()); - let fast = if let Ok(fast) = make_fast_from_syntaxtree(&syntax_tree, &path) { - fast - } else { - crate::core::fast_hdlparam::FastHdlparam { - fast_macro: crate::core::fast_hdlparam::Macro { - defines: Vec::new(), - errors: Vec::new(), - includes: Vec::new(), - invalid: Vec::new() - }, - content: Vec::new() - } - }; - // update_fast_to_client(backend.clone(), fast); - block_on(async { - update_fast_to_client(fast, &path).await; - }); - } + // if let Some(syntax_tree) = &syntax_tree { + // let path = PathBuf::from(uri.path().to_string()); + // let fast = if let Ok(fast) = make_fast_from_syntaxtree(&syntax_tree, &path) { + // fast + // } else { + // crate::core::fast_hdlparam::FastHdlparam { + // fast_macro: crate::core::fast_hdlparam::Macro { + // defines: Vec::new(), + // errors: Vec::new(), + // includes: Vec::new(), + // invalid: Vec::new() + // }, + // content: Vec::new() + // } + // }; + // // update_fast_to_client(backend.clone(), fast); + // block_on(async { + // update_fast_to_client(fast, &path).await; + // }); + // } let mut file = source_handle.write().unwrap(); diff --git a/src/test/mod.rs b/src/test/mod.rs new file mode 100644 index 0000000..2588471 --- /dev/null +++ b/src/test/mod.rs @@ -0,0 +1,196 @@ +#[allow(unused)] +const TESTFILES_DIR: &str = "testfiles"; + +#[allow(unused)] +const DIGTIAL_IDE_TEST: &str = "/home/dide/project/Digital-Test/Digital-IDE-test/user"; + +#[allow(unused)] +macro_rules! unwrap_result { + ($expr:expr) => { + match $expr { + Ok(e) => e, + Err(_) => return + } + }; +} + +#[allow(unused)] +macro_rules! unwrap_option { + ($expr:expr) => { + match $expr { + Some(e) => e, + None => return + } + }; +} + +#[cfg(test)] +mod test_fast { + use std::{fs, path::Path}; + use crate::core::sv_parser::sv_parser; + use super::*; + + #[test] + fn test_testfiles() { + let entries = unwrap_result!(fs::read_dir(TESTFILES_DIR)); + for entry in entries { + let entry = unwrap_result!(entry); + let file_path = entry.path(); + if file_path.is_file() { + let extension = unwrap_option!(file_path.extension()); + let file_path = unwrap_option!(file_path.to_str()); + if extension == "v" || extension == "sv" { + println!("Test file: {:?}", file_path); + let _ = sv_parser(file_path); + } + } + } + } + + #[test] + fn test_digital_ide_test() { + // 判断路径是否存在且为文件夹 + let path = Path::new(DIGTIAL_IDE_TEST); + if path.exists() && path.is_dir() { + // 递归遍历文件夹 + if let Err(e) = traverse_directory(path) { + eprintln!("Error: {}", e); + } + } else { + eprintln!("Path does not exist or is not a directory"); + } + } + + fn traverse_directory(dir: &Path) -> Result<(), Box> { + if dir.is_dir() { + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + + if path.is_dir() { + // 递归遍历子文件夹 + traverse_directory(&path)?; + } else if path.is_file() { + // 检查文件扩展名 + if let Some(ext) = path.extension() { + if ext == "v" || ext == "sv" { + println!("Test file: {:?}", path); + // TODO: Check Stack Overflow tests + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/ivtest/comp1001.sv" { + continue; + } + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/ivtest/comp1000.sv" { + continue; + } + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/ivtest/br_gh330.sv" { + continue; + } + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/hdlconv/pri_encoder_using_assign.sv" { + continue; + } + let file_path = path.to_str().unwrap(); + let _ = sv_parser(file_path); + } + } + } + } + } + Ok(()) + } +} + +#[cfg(test)] +mod test_scope_tree { + use std::{collections::HashMap, fs, path::{Path, PathBuf}}; + use crate::definition::{get_scopes, GenericScope}; + use sv_parser::parse_sv; + use tower_lsp::lsp_types::Url; + + use super::*; + + fn get_scope_tree(file_path: &str) -> Option { + let defines = HashMap::new(); + let includes: Vec = Vec::new(); + + let result = parse_sv(file_path, &defines, &includes, true, true); + if let Ok((syntax_tree, _)) = result { + let file_url = format!("file://{}", file_path); + let uri = Url::parse(&file_url); + if let Ok(uri) = uri { + return get_scopes(&syntax_tree, &uri); + } else { + eprintln!("error happen when unwrap uri: {:?}", uri); + return None; + } + } + None + } + + #[test] + fn test_testfiles() { + let entries = unwrap_result!(fs::read_dir(TESTFILES_DIR)); + for entry in entries { + let entry = unwrap_result!(entry); + let file_path = entry.path(); + if file_path.is_file() { + let extension = unwrap_option!(file_path.extension()); + let file_path = unwrap_option!(file_path.to_str()); + if extension == "v" || extension == "sv" { + println!("Test file: {:?}", file_path); + let _ = get_scope_tree(file_path); + } + } + } + } + + #[test] + fn test_digital_ide_test() { + // 判断路径是否存在且为文件夹 + let path = Path::new(DIGTIAL_IDE_TEST); + if path.exists() && path.is_dir() { + // 递归遍历文件夹 + if let Err(e) = traverse_directory(path) { + eprintln!("Error: {}", e); + } + } else { + eprintln!("Path does not exist or is not a directory"); + } + } + + fn traverse_directory(dir: &Path) -> Result<(), Box> { + if dir.is_dir() { + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + + if path.is_dir() { + // 递归遍历子文件夹 + traverse_directory(&path)?; + } else if path.is_file() { + // 检查文件扩展名 + if let Some(ext) = path.extension() { + if ext == "v" || ext == "sv" { + println!("Test file: {:?}", path); + // TODO: Check Stack Overflow tests + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/ivtest/comp1001.sv" { + continue; + } + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/ivtest/comp1000.sv" { + continue; + } + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/ivtest/br_gh330.sv" { + continue; + } + if path.to_str().unwrap() == "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/svlog/tools/hdlconv/pri_encoder_using_assign.sv" { + continue; + } + let file_path = path.to_str().unwrap(); + let _ = get_scope_tree(file_path); + } + } + } + } + } + Ok(()) + } +} \ No newline at end of file