diff --git a/src/core/primitive_parser.rs b/src/core/primitive_parser.rs index 1a096c5..3f3eb9b 100644 --- a/src/core/primitive_parser.rs +++ b/src/core/primitive_parser.rs @@ -1,5 +1,6 @@ use std::fs; use std::path::PathBuf; +use std::sync::RwLock; use std::{collections::HashMap, fs::File}; use std::io::{BufReader, Read}; @@ -12,6 +13,23 @@ use xml::reader::{EventReader, XmlEvent}; use super::hdlparam::{FastHdlparam, InstParameter, Macro, Range}; use super::sv_parser::{get_identifier, get_instance_params, get_instance_ports, get_position, get_pp_range}; +pub struct PrimitiveText { + pub name_to_text: RwLock> +} + +impl PrimitiveText { + pub fn new() -> Self { + Self { + name_to_text: RwLock::new(HashMap::::new()) + } + } + + pub fn update_text(&self, name: &str, text: &str) { + let mut text_map = self.name_to_text.write().unwrap(); + text_map.insert(name.to_owned(), text.to_owned()); + } +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Template { pub text: String, diff --git a/src/server.rs b/src/server.rs index b0141bd..71a7a33 100644 --- a/src/server.rs +++ b/src/server.rs @@ -207,6 +207,8 @@ impl LanguageServer for Backend { info!("extensionPath: {:?}", configure.extension_path); info!("toolChain: {:?}", configure.tool_chain); + self.server.srcs.init_primitive(&configure.extension_path); + let text_document_sync = TextDocumentSyncCapability::Options( TextDocumentSyncOptions { open_close: Some(true), diff --git a/src/sources.rs b/src/sources.rs index fb3f38d..31f3882 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -1,4 +1,5 @@ use crate::core::hdlparam::HdlParam; +use crate::core::primitive_parser::PrimitiveText; use crate::core::sv_parser::make_fast_from_syntaxtree; use crate::core::vhdl_parser::make_fast_from_design_file; use crate::core::vhdl_parser::vhdl_parse_str; @@ -223,6 +224,8 @@ pub struct Sources { pub scope_tree: Arc>>, // include directories, passed to parser to resolve `include pub include_dirs: Arc>>, + // primitive instance text + pub primitive_text: Arc, /// hdlparam 后端实现 pub hdl_param: Arc, // 同步解析线程和发送 fast 请求的 @@ -243,11 +246,26 @@ impl Sources { meta: Arc::new(RwLock::new(Vec::new())), scope_tree: Arc::new(RwLock::new(None)), include_dirs: Arc::new(RwLock::new(Vec::new())), + primitive_text: Arc::new(PrimitiveText::new()), hdl_param: Arc::new(HdlParam::new()), fast_sync_controller: Arc::new(RwLock::new(HashMap::>>::new())) } } + pub fn init_primitive(&self, extension_path: &str) { + let primitive_bin = extension_path.to_string() + "/resources/dide-lsp/static/primitive.bin"; + if let Some(primitive_xml) = crate::core::primitive_parser::load_primitive_bin(&primitive_bin) { + let primitive_text_handle = self.primitive_text.clone(); + let hdl_param_handle = self.hdl_param.clone(); + for (name, template) in primitive_xml.name_to_template { + // use "primitive/name" as a fake path + let fake_path = "primitive/".to_string() + &name; + hdl_param_handle.update_fast(fake_path, template.fast); + primitive_text_handle.update_text(&name, &template.text); + } + } + } + /// 增加一个 hdl 文件,并为该文件添加单独的解析线程 pub fn add(&self, server: &LSPServer, doc: TextDocumentItem) { // 对于当前的文件增加一个解析线程,不断进行解析和同步