From 0a1eb5e0803077d6c63f7fa11b79c27c449198d9 Mon Sep 17 00:00:00 2001 From: LSTM-Kirigaya <1193466151@qq.com> Date: Tue, 1 Oct 2024 21:29:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20vhdl=20=E7=9A=84=20UT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/request/mod.rs | 2 +- src/test/mod.rs | 4 ++++ src/test/vhdl.rs | 26 ++++++++++++++++++++++++++ src/utils/file.rs | 34 +++++++++++++++++++++++++++++++++- src/utils/mod.rs | 12 ++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/test/vhdl.rs diff --git a/src/request/mod.rs b/src/request/mod.rs index b106c97..05cdc27 100644 --- a/src/request/mod.rs +++ b/src/request/mod.rs @@ -89,7 +89,7 @@ fn make_textdocumenitem_from_path(path_buf: &PathBuf) -> Option(path: String, backend: &Arc) -> Result { - info!("parse fast {}", path); + info!("parse fast \"{}\"", path); let language_id = get_language_id_by_path_str(&path); match language_id.as_str() { diff --git a/src/test/mod.rs b/src/test/mod.rs index 696ca5a..79af1e0 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -1,3 +1,5 @@ +mod vhdl; + #[allow(unused)] const TESTFILES_DIR: &str = "testfiles"; @@ -10,6 +12,8 @@ const TEST_FILE: &str = "/home/dide/project/Digital-Test/MipsDesign/src/MyCpu.v" #[allow(unused)] const INCOMPLETE_EXAMPLE: &str = "/home/dide/project/Digital-Test/Digital-IDE-test/user/src/incomplete-example"; +#[allow(unused)] +const TESTFILES_TEMP_DIR: &str = "/home/dide/project/Digital-Test/Digital-IDE-temp"; #[allow(unused)] macro_rules! unwrap_result { diff --git a/src/test/vhdl.rs b/src/test/vhdl.rs new file mode 100644 index 0000000..6dfbfdb --- /dev/null +++ b/src/test/vhdl.rs @@ -0,0 +1,26 @@ + + +#[cfg(test)] +mod test_vhdl_fast { + use crate::{core::vhdl_parser::{make_fast_from_design_file, vhdl_parse}, test::TESTFILES_TEMP_DIR, utils::*}; + + #[test] + fn test_temp() { + let file_iter = RecursiveFileIterator::new(TESTFILES_TEMP_DIR); + for file in file_iter { + let language_id = get_language_id_by_pathbuf(&file); + if language_id == "vhdl" { + println!("test file: {:?}", file); + if let Some(design_file) = vhdl_parse(&file) { + if let Some(_) = make_fast_from_design_file(&design_file) { + println!("<(^-^)>"); + } else { + eprintln!("error happen when make fast {:?}", file); + } + } else { + eprintln!("error happen when parse {:?}", file); + } + } + } + } +} \ No newline at end of file diff --git a/src/utils/file.rs b/src/utils/file.rs index ac4fcf7..5315bed 100644 --- a/src/utils/file.rs +++ b/src/utils/file.rs @@ -1,4 +1,4 @@ -use std::{fs, path::PathBuf}; +use std::{fs, path::PathBuf, str::FromStr}; use log::info; use ropey::Rope; @@ -28,4 +28,36 @@ pub fn open_doc_as_rope(path: &PathBuf) -> Option { } else { None } +} + +pub struct RecursiveFileIterator { + stack: Vec, +} + +impl RecursiveFileIterator { + pub fn new(start_dir: &str) -> Self { + let start_dir = PathBuf::from_str(start_dir).unwrap(); + RecursiveFileIterator { + stack: vec![start_dir], + } + } +} + +impl Iterator for RecursiveFileIterator { + type Item = PathBuf; + + fn next(&mut self) -> Option { + while let Some(path) = self.stack.pop() { + if path.is_dir() { + if let Ok(entries) = fs::read_dir(path) { + for entry in entries.flatten() { + self.stack.push(entry.path()); + } + } + } else { + return Some(path); + } + } + None + } } \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c38e2ec..c808a98 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -8,6 +8,7 @@ use vhdl_lang::{ast::ObjectClass, AnyEntKind, Concurrent, Object, Overloaded, Sr pub mod fast; pub mod file; +pub use file::*; /// 根据 pos 获取到当前光标所在的字符串,相当于 getWordRangeAtPosition pub fn get_definition_token(line: &RopeSlice, pos: Position) -> String { @@ -91,6 +92,17 @@ pub fn get_language_id_by_uri(uri: &Url) -> String { get_language_id_by_extname(ext_name) } +/// 根据路径字符串获取 hdl 的 language id +/// 返回值为 "vhdl" | "verilog" | "systemverilog" | "plaintext" +/// 不采用枚举是因为需要在 lsp 中使用到它们的字符串值 +pub fn get_language_id_by_pathbuf(pathbuf: &PathBuf) -> String { + let ext_name = pathbuf.as_path() + .extension() + .and_then(std::ffi::OsStr::to_str) + .unwrap_or(""); + get_language_id_by_extname(ext_name) +} + /// 根据路径字符串获取 hdl 的 language id /// 返回值为 "vhdl" | "verilog" | "systemverilog" | "plaintext" /// 不采用枚举是因为需要在 lsp 中使用到它们的字符串值