This commit is contained in:
锦恢 2024-10-26 22:33:05 +08:00
parent 7398d742c9
commit 7f5ed156a5
7 changed files with 120 additions and 0 deletions

View File

@ -34,11 +34,39 @@ impl Range {
LspRange::new(start_pos, end_pos)
}
#[allow(unused)]
pub fn from_lsp_range(range: &LspRange) -> Range {
Range {
start: Position {
line: range.start.line,
character: range.start.character
},
end: Position {
line: range.end.line,
character: range.end.character
}
}
}
/// 判断输入的 position 是否在当前 range 内
pub fn contains(&self, postion: &LspPosition) -> bool {
compare_pos(&self.start.to_lsp_position(), postion) != 1 && compare_pos(postion, &self.end.to_lsp_position()) != 1
}
#[allow(unused)]
pub fn before(&self, range: &Range) -> bool {
let current_pos = &self.end.to_lsp_position();
let target_pos = &range.start.to_lsp_position();
compare_pos(current_pos, target_pos) != 1
}
#[allow(unused)]
pub fn after(&self, range: &Range) -> bool {
let current_pos = &self.start.to_lsp_position();
let target_pos = &range.end.to_lsp_position();
compare_pos(current_pos, target_pos) != -1
}
/// 对所有的 line 或者 所有的 character 进行偏移操作
pub fn affine(&mut self, line_offset: i32, character_offset: i32) -> &Range {
if line_offset > 0 {
@ -61,6 +89,10 @@ impl Range {
}
}
/// 比较两个 pos 的位置关系
/// 1pos1 在 pos2 后面
/// 0pos1 和 pos2 一样
/// -1pos1 在 pos2 前面
fn compare_pos(pos1: &LspPosition, pos2: &LspPosition) -> i32 {
if pos1.line > pos2.line {
1

27
src/inlay_hint/mod.rs Normal file
View File

@ -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 inlay_hint(&self, params: InlayHintParams) -> Option<Vec<InlayHint>> {
let language_id = get_language_id_by_uri(&params.text_document.uri);
match language_id.as_str() {
"vhdl" => vhdl::inlay_hint(
self,
&params
),
"verilog" | "systemverilog" => sv::inlay_hint(
self,
&params
),
_ => None
}
}
}

42
src/inlay_hint/sv.rs Normal file
View File

@ -0,0 +1,42 @@
use std::{path::PathBuf, str::FromStr};
use crate::{core, server::LSPServer};
#[allow(unused)]
use log::info;
use tower_lsp::lsp_types::*;
use super::to_escape_path;
pub fn inlay_hint(server: &LSPServer, params: &InlayHintParams) -> Option<Vec<InlayHint>> {
let uri = &params.text_document.uri;
let path = PathBuf::from_str(uri.path()).unwrap();
let path = to_escape_path(&path);
let path_string = path.to_str().unwrap();
let visible_range = core::hdlparam::Range::from_lsp_range(&params.range);
let fast_map = server.srcs.hdl_param.path_to_hdl_file.read().unwrap();
// 先找到 当前 所在的 hdlfile
if let Some(hdl_file) = &fast_map.get(path_string) {
let fast = &hdl_file.fast;
let mut hints = Vec::<InlayHint>::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;
}
}
}
}
}
None
}
fn make_instport_hint(params: &InlayHintParams) {
}

8
src/inlay_hint/vhdl.rs Normal file
View File

@ -0,0 +1,8 @@
use crate::server::LSPServer;
#[allow(unused)]
use log::info;
use tower_lsp::lsp_types::*;
pub fn inlay_hint(server: &LSPServer, params: &InlayHintParams) -> Option<Vec<InlayHint>> {
None
}

View File

@ -18,6 +18,9 @@ pub mod document_symbol;
// 语法高亮
pub mod document_highlight;
// 内部提示
pub mod inlay_hint;
// 诊断
pub mod diagnostics;

View File

@ -13,6 +13,7 @@ mod definition;
mod hover;
mod document_symbol;
mod document_highlight;
mod inlay_hint;
mod utils;
mod diagnostics;
mod format;

View File

@ -304,4 +304,11 @@ impl LanguageServer for Backend {
) -> Result<Option<Vec<DocumentHighlight>>> {
Ok(self.server.document_highlight(params))
}
async fn inlay_hint(
&self,
params: InlayHintParams
) -> Result<Option<Vec<InlayHint>>> {
Ok(self.server.inlay_hint(params))
}
}