add primitive init

This commit is contained in:
light-ly 2024-11-12 21:45:24 +08:00
parent 2851c1dfa6
commit 6039bdf2c8
3 changed files with 38 additions and 0 deletions

View File

@ -1,5 +1,6 @@
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::RwLock;
use std::{collections::HashMap, fs::File}; use std::{collections::HashMap, fs::File};
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
@ -12,6 +13,23 @@ use xml::reader::{EventReader, XmlEvent};
use super::hdlparam::{FastHdlparam, InstParameter, Macro, Range}; use super::hdlparam::{FastHdlparam, InstParameter, Macro, Range};
use super::sv_parser::{get_identifier, get_instance_params, get_instance_ports, get_position, get_pp_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<HashMap<String, String>>
}
impl PrimitiveText {
pub fn new() -> Self {
Self {
name_to_text: RwLock::new(HashMap::<String, String>::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)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Template { pub struct Template {
pub text: String, pub text: String,

View File

@ -207,6 +207,8 @@ impl LanguageServer for Backend {
info!("extensionPath: {:?}", configure.extension_path); info!("extensionPath: {:?}", configure.extension_path);
info!("toolChain: {:?}", configure.tool_chain); info!("toolChain: {:?}", configure.tool_chain);
self.server.srcs.init_primitive(&configure.extension_path);
let text_document_sync = TextDocumentSyncCapability::Options( let text_document_sync = TextDocumentSyncCapability::Options(
TextDocumentSyncOptions { TextDocumentSyncOptions {
open_close: Some(true), open_close: Some(true),

View File

@ -1,4 +1,5 @@
use crate::core::hdlparam::HdlParam; use crate::core::hdlparam::HdlParam;
use crate::core::primitive_parser::PrimitiveText;
use crate::core::sv_parser::make_fast_from_syntaxtree; use crate::core::sv_parser::make_fast_from_syntaxtree;
use crate::core::vhdl_parser::make_fast_from_design_file; use crate::core::vhdl_parser::make_fast_from_design_file;
use crate::core::vhdl_parser::vhdl_parse_str; use crate::core::vhdl_parser::vhdl_parse_str;
@ -223,6 +224,8 @@ pub struct Sources {
pub scope_tree: Arc<RwLock<Option<GenericScope>>>, pub scope_tree: Arc<RwLock<Option<GenericScope>>>,
// include directories, passed to parser to resolve `include // include directories, passed to parser to resolve `include
pub include_dirs: Arc<RwLock<Vec<PathBuf>>>, pub include_dirs: Arc<RwLock<Vec<PathBuf>>>,
// primitive instance text
pub primitive_text: Arc<PrimitiveText>,
/// hdlparam 后端实现 /// hdlparam 后端实现
pub hdl_param: Arc<HdlParam>, pub hdl_param: Arc<HdlParam>,
// 同步解析线程和发送 fast 请求的 // 同步解析线程和发送 fast 请求的
@ -243,11 +246,26 @@ impl Sources {
meta: Arc::new(RwLock::new(Vec::new())), meta: Arc::new(RwLock::new(Vec::new())),
scope_tree: Arc::new(RwLock::new(None)), scope_tree: Arc::new(RwLock::new(None)),
include_dirs: Arc::new(RwLock::new(Vec::new())), include_dirs: Arc::new(RwLock::new(Vec::new())),
primitive_text: Arc::new(PrimitiveText::new()),
hdl_param: Arc::new(HdlParam::new()), hdl_param: Arc::new(HdlParam::new()),
fast_sync_controller: Arc::new(RwLock::new(HashMap::<String, Arc<RwLock<bool>>>::new())) fast_sync_controller: Arc::new(RwLock::new(HashMap::<String, Arc<RwLock<bool>>>::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 文件,并为该文件添加单独的解析线程 /// 增加一个 hdl 文件,并为该文件添加单独的解析线程
pub fn add(&self, server: &LSPServer, doc: TextDocumentItem) { pub fn add(&self, server: &LSPServer, doc: TextDocumentItem) {
// 对于当前的文件增加一个解析线程,不断进行解析和同步 // 对于当前的文件增加一个解析线程,不断进行解析和同步