From 2dd7f98d5f15848df67769bc1d86ebb4e61e456e Mon Sep 17 00:00:00 2001 From: LSTM-Kirigaya <1193466151@qq.com> Date: Thu, 31 Oct 2024 15:35:17 +0800 Subject: [PATCH] use proposed tower lsp --- Cargo.toml | 2 +- src/core/hdlparam.rs | 8 ++-- src/core/vhdl_parser.rs | 12 +++--- src/hover/sv.rs | 8 ++-- src/inlay_hint/sv.rs | 84 +++++++++++++++++++++++++++++++++++++---- src/main.rs | 2 +- src/server.rs | 21 ++++++----- 7 files changed, 104 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3402eb3..ebc4a72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ dirs-next = "2.0" bincode = "1.3" percent-encoding = "2.1.0" log = "0.4.19" -tower-lsp = "0.20.0" +tower-lsp = { version = "0.20.0", features = ["proposed"]} flexi_logger = "0.29.0" ropey = "1.6.0" tokio = { version = "1.29.1", features = ["macros", "io-std", "rt-multi-thread"] } diff --git a/src/core/hdlparam.rs b/src/core/hdlparam.rs index 8bfc735..d9a9ec5 100644 --- a/src/core/hdlparam.rs +++ b/src/core/hdlparam.rs @@ -170,9 +170,9 @@ pub struct Instance { #[serde(rename = "type")] pub inst_type: String, pub instparams: Option, - pub intstparams_assignment: Vec, + pub intstparam_assignments: Vec, pub instports: Option, - pub intstport_assignment: Vec, + pub intstport_assignments: Vec, pub range: Range } @@ -334,9 +334,9 @@ impl FastHdlparam { name: name.to_string(), inst_type: inst_type.to_string(), instparams: param_range, - intstparams_assignment: params_assign, + intstparam_assignments: params_assign, instports: port_range, - intstport_assignment: ports_assign, + intstport_assignments: ports_assign, range }; last_module.instances.push(instance); diff --git a/src/core/vhdl_parser.rs b/src/core/vhdl_parser.rs index 09d9732..93cde9d 100644 --- a/src/core/vhdl_parser.rs +++ b/src/core/vhdl_parser.rs @@ -206,8 +206,8 @@ fn parse_tokens(tokens: Vec) -> Vec { inst_type: get_value(&tokens[i+1]), instports: None, instparams: None, - intstport_assignment: Vec::new(), - intstparams_assignment: Vec::new(), + intstport_assignments: Vec::new(), + intstparam_assignments: Vec::new(), range: Range { start: Position { line: tokens[i-1].pos.range.start.line + 1, @@ -228,8 +228,8 @@ fn parse_tokens(tokens: Vec) -> Vec { inst_type: get_value(&tokens[i+2]), instports: None, instparams: None, - intstport_assignment: Vec::new(), - intstparams_assignment: Vec::new(), + intstport_assignments: Vec::new(), + intstparam_assignments: Vec::new(), range: Range { start: Position { line: tokens[i-1].pos.range.start.line + 1, @@ -261,8 +261,8 @@ fn parse_tokens(tokens: Vec) -> Vec { inst_type, instports: None, instparams: None, - intstport_assignment: Vec::new(), - intstparams_assignment: Vec::new(), + intstport_assignments: Vec::new(), + intstparam_assignments: Vec::new(), range: Range { start: Position { line: tokens[i-1].pos.range.start.line + 1, diff --git a/src/hover/sv.rs b/src/hover/sv.rs index 4ceaa2b..b2111c7 100644 --- a/src/hover/sv.rs +++ b/src/hover/sv.rs @@ -33,18 +33,18 @@ pub fn hover(server: &LSPServer, params: &HoverParams) -> Option { return Some(hover); } - info!("enter hover_position_port_param"); + // info!("enter hover_position_port_param"); // match positional port param if let Some(hover) = hover_position_port_param(server, &line_text, doc, pos, &language_id) { return Some(hover); } - info!("enter hover_module_declaration"); + // info!("enter hover_module_declaration"); // match module name if let Some(hover) = hover_module_declaration(server, &token, &language_id) { - info!("[LSPServer] in hover: get module hover"); + // info!("[LSPServer] in hover: get module hover"); if hover_for_module(server, pos, doc) { - info!("[LSPServer] in hover: it is instance"); + // info!("[LSPServer] in hover: it is instance"); return Some(hover); } } diff --git a/src/inlay_hint/sv.rs b/src/inlay_hint/sv.rs index 9e59f9d..324b8af 100644 --- a/src/inlay_hint/sv.rs +++ b/src/inlay_hint/sv.rs @@ -14,29 +14,97 @@ pub fn inlay_hint(server: &LSPServer, params: &InlayHintParams) -> Option::new(); // 制作例化模块的 hint for module in &fast.content { for instance in &module.instances { - if let Some(instport_range) = &instance.instports { - // 根据在可见视图外面的 range 就不管了 - if instport_range.before(&visible_range) || instport_range.after(&visible_range) { - continue; - } - + // 根据在可见视图外面的 range 就不管了 + if is_visible_range(&instance.instparams, &visible_range) { + hints.extend(make_instparam_hints(params, instance)); + } + + if is_visible_range(&instance.instports, &visible_range) { + hints.extend(make_instport_hints(params, instance)); } - } } + + return Some(hints); } None } -fn make_instport_hint(params: &InlayHintParams) { +fn is_visible_range( + target_range: &Option, + visible_range: &core::hdlparam::Range +) -> bool { + if let Some(target_range) = target_range { + if target_range.before(visible_range) || target_range.after(visible_range) { + return false; + } + return true; + } + false +} +fn make_instparam_hints( + params: &InlayHintParams, + instance: &core::hdlparam::Instance +) -> Vec { + let mut hints = Vec::::new(); + + + hints +} + +fn make_instport_hints( + params: &InlayHintParams, + instance: &core::hdlparam::Instance +) -> Vec { + let mut hints = Vec::::new(); + for port_assigment in &instance.intstport_assignments { + let port_desc = MarkupContent { + kind: MarkupKind::Markdown, + value: format!("```verilog\n{:?}\n```", port_assigment.port) + }; + + let hint = InlayHint { + position: port_assigment.range.to_lsp_range().start, + label: InlayHintLabel::String("start".to_string()), + padding_left: Some(true), + padding_right: Some(true), + kind: Some(InlayHintKind::PARAMETER), + text_edits: None, + tooltip: Some(InlayHintTooltip::MarkupContent(port_desc)), + data: None + }; + hints.push(hint); + + let port_desc = MarkupContent { + kind: MarkupKind::Markdown, + value: format!("```verilog\n{:?}\n```", port_assigment.port) + }; + let hint = InlayHint { + position: port_assigment.range.to_lsp_range().end, + label: InlayHintLabel::String("end".to_string()), + padding_left: Some(true), + padding_right: Some(true), + kind: Some(InlayHintKind::PARAMETER), + text_edits: None, + tooltip: Some(InlayHintTooltip::MarkupContent(port_desc)), + data: None + }; + hints.push(hint); + } + + hints } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7db8313..e0ef812 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ async fn main() { .start() .unwrap(); - info!("launch Digital LSP"); + info!("launch Digital LSP, version: 0.4.0"); let stdin = tokio::io::stdin(); let stdout = tokio::io::stdout(); diff --git a/src/server.rs b/src/server.rs index d2ab856..18e6ead 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,7 +4,6 @@ use crate::completion::keyword::*; use flexi_logger::LoggerHandle; #[allow(unused)] use log::{debug, info, warn}; -use notification::DidDeleteFiles; use serde::{Deserialize, Serialize}; use std::string::ToString; use std::sync::{Mutex, RwLock}; @@ -214,12 +213,13 @@ impl LanguageServer for Backend { completion_provider: Some(completion_provider), definition_provider: Some(OneOf::Left(true)), hover_provider: Some(HoverProviderCapability::Simple(true)), + // inlay_hint_provider: Some(OneOf::Left(true)), document_symbol_provider: Some(OneOf::Left(true)), document_highlight_provider: Some(OneOf::Left(true)), ..ServerCapabilities::default() }; - Ok(InitializeResult { server_info, capabilities }) + Ok(InitializeResult::default()) } async fn initialized(&self, _: InitializedParams) { @@ -280,13 +280,6 @@ impl LanguageServer for Backend { Ok(self.server.hover(params)) } - async fn document_symbol( - &self, - params: DocumentSymbolParams, - ) -> Result> { - Ok(self.server.document_symbol(params)) - } - async fn formatting(&self, params: DocumentFormattingParams) -> Result>> { Ok(self.server.formatting(params)) } @@ -298,10 +291,19 @@ impl LanguageServer for Backend { Ok(self.server.range_formatting(params)) } + async fn document_symbol( + &self, + params: DocumentSymbolParams, + ) -> Result> { + info!("enter document"); + Ok(self.server.document_symbol(params)) + } + async fn document_highlight( &self, params: DocumentHighlightParams, ) -> Result>> { + info!("enter highlight"); Ok(self.server.document_highlight(params)) } @@ -309,6 +311,7 @@ impl LanguageServer for Backend { &self, params: InlayHintParams ) -> Result>> { + info!("enter inlay_hint"); Ok(self.server.inlay_hint(params)) } }