From 302dafbc053db1801ca7eb4cf76d1fb89659def4 Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Fri, 27 Sep 2024 14:53:40 +0800 Subject: [PATCH] save --- config.toml | 4 + rustfmt.toml | 2 +- src/custom_request.rs | 6 +- src/server.rs | 171 ++++++++++++------------------------------ src/sources.rs | 1 - sv-parser | 2 +- 6 files changed, 57 insertions(+), 129 deletions(-) create mode 100644 config.toml diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..baa4e8a --- /dev/null +++ b/config.toml @@ -0,0 +1,4 @@ +[build] +jobs = 16 # 并行构建任务的数量,默认等于 CPU 的核心数 +rustc = "rustc" # rust 编译器 +target-dir = "target" # 存放编译输出结果的目录 \ No newline at end of file diff --git a/rustfmt.toml b/rustfmt.toml index c94de51..4831ce7 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,2 @@ edition = "2018" -max_width = 100 +max_width = 76 diff --git a/src/custom_request.rs b/src/custom_request.rs index 8b9f60a..bc0d06f 100644 --- a/src/custom_request.rs +++ b/src/custom_request.rs @@ -69,7 +69,9 @@ impl <'a>tower_lsp::jsonrpc::Method<&'a Arc, (DoFastApiRequestParams, ) fn invoke(&self, _server: &'a Arc, _params: (DoFastApiRequestParams, )) -> Self::Future { let request_param = _params.0; let path = request_param.path; - future::ready(do_fast(path)) + let fut = future::ready(do_fast(path)); + info!("get future: {:?}", fut); + fut } } @@ -119,7 +121,7 @@ pub fn do_fast(path: String) -> Result { if let Some(syntax_tree) = parse_result { if let Ok(hdlparam) = make_fast_from_syntaxtree(&syntax_tree, &path_buf) { - info!("after parse {}, get hdlparam {:?}", path, hdlparam); + info!("after parse {}, get hdlparam", path); return Ok(hdlparam); } } diff --git a/src/server.rs b/src/server.rs index d985fd1..144f0ff 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,18 +4,12 @@ use flexi_logger::LoggerHandle; #[allow(unused)] use log::{debug, info, warn}; use once_cell::sync::OnceCell; -use path_clean::PathClean; use serde::{Deserialize, Serialize}; -use std::env::current_dir; -use std::fs::File; -use std::io::Read; -use std::path::PathBuf; use std::string::ToString; use std::sync::{Arc, Mutex, RwLock}; -use tower_lsp::jsonrpc::{Error, ErrorCode, Result}; +use tower_lsp::jsonrpc::Result; use tower_lsp::lsp_types::*; use tower_lsp::{Client, LanguageServer}; -use which::which; pub static GLOBAL_BACKEND: OnceCell> = OnceCell::new(); @@ -25,6 +19,7 @@ pub struct LSPServer { pub sys_tasks: Vec, pub directives: Vec, pub conf: RwLock, + #[allow(unused)] pub log_handle: Mutex>, } @@ -171,128 +166,56 @@ impl Default for VeribleFormat { } } -fn read_config(root_uri: Option) -> anyhow::Result { - let path = root_uri - .ok_or_else(|| anyhow::anyhow!("couldn't resolve workdir path"))? - .to_file_path() - .map_err(|_| anyhow::anyhow!("couldn't resolve workdir path"))?; - let mut config: Option = None; - for dir in path.ancestors() { - let config_path = dir.join("veridian.yaml"); - if config_path.exists() { - info!("found config: veridian.yaml"); - config = Some(config_path); - break; - } - let config_path = dir.join("veridian.yml"); - if config_path.exists() { - info!("found config: veridian.yml"); - config = Some(config_path); - break; - } - } - let mut contents = String::new(); - File::open(config.ok_or_else(|| anyhow::anyhow!("unable to read config file"))?)? - .read_to_string(&mut contents)?; - info!("reading config file"); - Ok(serde_yaml::from_str(&contents)?) -} - -// convert string path to absolute path -fn absolute_path(path_str: &str) -> Option { - let path = PathBuf::from(path_str); - if !path.exists() { - return None; - } - if !path.has_root() { - Some(current_dir().unwrap().join(path).clean()) - } else { - Some(path) - } -} #[tower_lsp::async_trait] impl LanguageServer for Backend { - async fn initialize(&self, params: InitializeParams) -> Result { - // grab include dirs and source dirs from config, and convert to abs path - let mut inc_dirs = self.server.srcs.include_dirs.write().unwrap(); - let mut src_dirs = self.server.srcs.source_dirs.write().unwrap(); - match read_config(params.root_uri) { - Ok(conf) => { - inc_dirs.extend(conf.include_dirs.iter().filter_map(|x| absolute_path(x))); - src_dirs.extend(conf.source_dirs.iter().filter_map(|x| absolute_path(x))); - let mut log_handle = self.server.log_handle.lock().unwrap(); - let log_handle = log_handle.as_mut(); - if let Some(handle) = log_handle { - handle - .parse_and_push_temp_spec(&conf.log_level.to_string()) - .map_err(|e| Error { - code: ErrorCode::InvalidParams, - message: e.to_string().into(), - data: None, - })?; - } - *self.server.conf.write().unwrap() = conf; - } - Err(e) => { - warn!("found errors in config file: {:#?}", e); - } - } - let mut conf = self.server.conf.write().unwrap(); - conf.verible.syntax.enabled = which(&conf.verible.syntax.path).is_ok(); - - if conf.verilator.syntax.enabled { - info!("enabled linting with verilator") - } else if conf.verible.syntax.enabled { - info!("enabled linting with verible-verilog-syntax") - } - conf.verible.format.enabled = which(&conf.verible.format.path).is_ok(); - if conf.verible.format.enabled { - info!("enabled formatting with verible-verilog-format"); - } else { - info!("formatting unavailable"); - } - drop(inc_dirs); - drop(src_dirs); - // parse all source files found from walking source dirs and include dirs + async fn initialize(&self, _: InitializeParams) -> Result { self.server.srcs.init(); - Ok(InitializeResult { - server_info: None, - capabilities: ServerCapabilities { - text_document_sync: Some(TextDocumentSyncCapability::Options( - TextDocumentSyncOptions { - open_close: Some(true), - change: Some(TextDocumentSyncKind::INCREMENTAL), - will_save: None, - will_save_wait_until: None, - save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions { - include_text: None, - })), - }, - )), - completion_provider: Some(CompletionOptions { - resolve_provider: Some(false), - trigger_characters: Some(vec![ - ".".to_string(), - "$".to_string(), - "`".to_string(), - ]), - work_done_progress_options: WorkDoneProgressOptions { - work_done_progress: None, - }, - all_commit_characters: None, - // TODO: check if corect - completion_item: None, - }), - definition_provider: Some(OneOf::Left(true)), - hover_provider: Some(HoverProviderCapability::Simple(true)), - document_symbol_provider: Some(OneOf::Left(true)), - document_formatting_provider: Some(OneOf::Left(conf.verible.format.enabled)), - document_range_formatting_provider: Some(OneOf::Left(conf.verible.format.enabled)), - document_highlight_provider: Some(OneOf::Left(true)), - ..ServerCapabilities::default() + + // 申明 LSP 的基本信息和提供的能力 + let server_info = Some(ServerInfo { + name: "Digital IDE 专用 LSP 后端服务器".to_string(), + version: Some("0.4.0".to_string()) + }); + + let text_document_sync = TextDocumentSyncCapability::Options( + TextDocumentSyncOptions { + open_close: Some(true), + change: Some(TextDocumentSyncKind::INCREMENTAL), + will_save: None, + will_save_wait_until: None, + save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions { + include_text: None, + })), + } + ); + + let completion_provider = CompletionOptions { + resolve_provider: Some(false), + trigger_characters: Some(vec![ + ".".to_string(), + "$".to_string(), + "`".to_string(), + ]), + work_done_progress_options: WorkDoneProgressOptions { + work_done_progress: None, }, - }) + all_commit_characters: None, + // TODO: 检查这里的选项 + completion_item: None, + }; + + let capabilities = ServerCapabilities { + text_document_sync: Some(text_document_sync), + completion_provider: Some(completion_provider), + definition_provider: Some(OneOf::Left(true)), + hover_provider: Some(HoverProviderCapability::Simple(true)), + document_symbol_provider: Some(OneOf::Left(true)), + document_highlight_provider: Some(OneOf::Left(true)), + ..ServerCapabilities::default() + }; + + Ok(InitializeResult { server_info, capabilities }) } async fn initialized(&self, _: InitializedParams) { diff --git a/src/sources.rs b/src/sources.rs index b4c9c06..c9a5606 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -247,7 +247,6 @@ impl Sources { None => None, }; - info!("finish parse {:?}", uri.to_string()); // 计算 fast diff --git a/sv-parser b/sv-parser index 90e9d4c..4a73dc8 160000 --- a/sv-parser +++ b/sv-parser @@ -1 +1 @@ -Subproject commit 90e9d4cb3ba4e3a55a9b3fcd0fb83389a376ec35 +Subproject commit 4a73dc87f7ab8dfdc0b08de89a04d9e39fd26895