From 348214e42d62dc484d2453c3b3d207b3b813c32a Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Thu, 14 Nov 2024 20:43:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20CodeLens=20=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- src/code_lens/mod.rs | 27 ++++++++++++++++++++ src/code_lens/sv.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ src/code_lens/vhdl.rs | 14 +++++++++++ src/inlay_hint/sv.rs | 2 +- src/lib.rs | 3 +++ src/main.rs | 1 + src/server.rs | 8 ++++++ src/sources.rs | 3 +++ 9 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/code_lens/mod.rs create mode 100644 src/code_lens/sv.rs create mode 100644 src/code_lens/vhdl.rs diff --git a/.gitignore b/.gitignore index 657e4ff..5c292bc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ test.txt build.bat .DS_Store -.cache \ No newline at end of file +.cache +deploy.* \ No newline at end of file diff --git a/src/code_lens/mod.rs b/src/code_lens/mod.rs new file mode 100644 index 0000000..189f4e5 --- /dev/null +++ b/src/code_lens/mod.rs @@ -0,0 +1,27 @@ +use crate::server::LSPServer; +use crate::utils::*; +#[allow(unused)] +use log::info; +use tower_lsp::lsp_types::*; + +mod sv; +mod vhdl; + +impl LSPServer { + pub fn code_lens(&self, params: CodeLensParams) -> Option> { + let language_id = get_language_id_by_uri(¶ms.text_document.uri); + match language_id.as_str() { + "vhdl" => vhdl::code_lens( + self, + ¶ms + ), + + "verilog" | "systemverilog" => sv::code_lens( + self, + ¶ms + ), + + _ => None + } + } +} \ No newline at end of file diff --git a/src/code_lens/sv.rs b/src/code_lens/sv.rs new file mode 100644 index 0000000..e2053b0 --- /dev/null +++ b/src/code_lens/sv.rs @@ -0,0 +1,57 @@ +use std::{path::PathBuf, str::FromStr}; + +use crate::server::LSPServer; +#[allow(unused)] +use log::info; +use tower_lsp::lsp_types::*; + +use super::to_escape_path; + +pub fn code_lens( + #[allow(unused)] + server: &LSPServer, + #[allow(unused)] + params: &CodeLensParams +) -> Option> { + let hdl_param = server.srcs.hdl_param.clone(); + let uri = ¶ms.text_document.uri; + + let path_to_hdl_file = hdl_param.path_to_hdl_file.read().unwrap(); + let pathbuf = PathBuf::from_str(uri.path()).unwrap(); + let pathbuf = to_escape_path(&pathbuf); + let path_string = pathbuf.to_str().unwrap(); + + info!("[code_lens] {}", path_string); + + let mut code_lens_array = Vec::::new(); + if let Some(hdl_file) = path_to_hdl_file.get(path_string) { + for module in &hdl_file.fast.content { + // 此处只有 name 和 path 有用 + let module_data_item: serde_json::Value = serde_json::json!({ + "icon": "", + "name": module.name.to_string(), + "type": "", + "doFastFileType": "common", + "range": null, + "path": path_string.to_string(), + "parent": null + }); + + let command = Command { + title: "Simulate".to_string(), + command: "digital-ide.tool.icarus.simulateFile".to_string(), + arguments: Some(vec![module_data_item]) + }; + let code_lens = CodeLens { + range: module.range.to_lsp_range(), + command: Some(command), + data: None + }; + code_lens_array.push(code_lens); + } + + return Some(code_lens_array); + } + + None +} \ No newline at end of file diff --git a/src/code_lens/vhdl.rs b/src/code_lens/vhdl.rs new file mode 100644 index 0000000..bf97612 --- /dev/null +++ b/src/code_lens/vhdl.rs @@ -0,0 +1,14 @@ + +use crate::server::LSPServer; +#[allow(unused)] +use log::info; +use tower_lsp::lsp_types::*; + +pub fn code_lens( + #[allow(unused)] + server: &LSPServer, + #[allow(unused)] + params: &CodeLensParams +) -> Option> { + None +} \ No newline at end of file diff --git a/src/inlay_hint/sv.rs b/src/inlay_hint/sv.rs index 04e5a7f..b4e1969 100644 --- a/src/inlay_hint/sv.rs +++ b/src/inlay_hint/sv.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, path::PathBuf, str::FromStr}; -use crate::{core, server::LSPServer, sources::LSPSupport}; +use crate::{core, server::LSPServer}; #[allow(unused)] use log::info; use ropey::Rope; diff --git a/src/lib.rs b/src/lib.rs index 1be98ca..fd2ac23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,9 @@ pub mod document_highlight; // 内部提示 pub mod inlay_hint; +// code lens 按钮 +pub mod code_lens; + // 诊断 pub mod diagnostics; diff --git a/src/main.rs b/src/main.rs index 94ce59b..4b875ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ mod hover; mod document_symbol; mod document_highlight; mod inlay_hint; +mod code_lens; mod utils; mod diagnostics; mod format; diff --git a/src/server.rs b/src/server.rs index 2d149f8..9b286f1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -258,6 +258,7 @@ impl LanguageServer for Backend { document_symbol_provider: Some(OneOf::Left(true)), document_highlight_provider: Some(OneOf::Left(true)), workspace: Some(workspace), + code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }), ..ServerCapabilities::default() }; @@ -370,4 +371,11 @@ impl LanguageServer for Backend { ) -> Result>> { Ok(self.server.inlay_hint(params)) } + + async fn code_lens( + &self, + params: CodeLensParams + ) -> Result>> { + Ok(self.server.code_lens(params)) + } } diff --git a/src/sources.rs b/src/sources.rs index 6f8d2e6..d1a1f72 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -447,6 +447,7 @@ impl Sources { }) } + #[allow(unused)] pub fn get_lsp_configuration_string_value(&self, name: &str) -> Option { let lsp_configuration = self.lsp_configuration.read().unwrap(); if let Some(Value::String(value)) = lsp_configuration.get(name) { @@ -455,6 +456,7 @@ impl Sources { None } + #[allow(unused)] pub fn get_lsp_configuration_i64_value(&self, name: &str) -> Option { let lsp_configuration = self.lsp_configuration.read().unwrap(); if let Some(Value::Number(number)) = lsp_configuration.get(name) { @@ -463,6 +465,7 @@ impl Sources { None } + #[allow(unused)] pub fn get_lsp_configuration_bool_value(&self, name: &str) -> Option { let lsp_configuration = self.lsp_configuration.read().unwrap(); if let Some(Value::Bool(value)) = lsp_configuration.get(name) {