diff --git a/kclvm/tools/src/LSP/src/inlay_hints.rs b/kclvm/tools/src/LSP/src/inlay_hints.rs index e72b5afd6..2e4905805 100644 --- a/kclvm/tools/src/LSP/src/inlay_hints.rs +++ b/kclvm/tools/src/LSP/src/inlay_hints.rs @@ -1,35 +1,73 @@ +use indexmap::IndexSet; use kclvm_sema::core::symbol::SymbolHint; use kclvm_sema::core::{global_state::GlobalState, symbol::KCLSymbol}; use lsp_types::{InlayHint, InlayHintLabelPart, Position as LspPosition}; use std::convert::TryInto; +use std::hash::Hash; + +#[derive(Clone, Debug)] +struct KCLInlayHint { + /// The position of this hint. + pub position: LspPosition, + + /// An inlay hint label part allows for interactive and composite labels + /// of inlay hints. + pub part: InlayHintLabelPart, +} + +impl Hash for KCLInlayHint { + fn hash(&self, state: &mut H) { + self.position.line.hash(state); + self.position.character.hash(state); + self.part.value.hash(state); + } +} + +impl PartialEq for KCLInlayHint { + fn eq(&self, other: &Self) -> bool { + self.position == other.position && self.part.value == other.part.value + } +} + +impl Eq for KCLInlayHint {} pub fn inlay_hints(file: &str, gs: &GlobalState) -> Option> { - let mut inlay_hints: Vec = vec![]; + let mut inlay_hints: IndexSet = Default::default(); let sema_db = gs.get_sema_db(); if let Some(file_sema) = sema_db.get_file_sema(file) { let symbols = file_sema.get_symbols(); for symbol_ref in symbols { if let Some(symbol) = gs.get_symbols().get_symbol(*symbol_ref) { if let Some(hint) = symbol.get_hint() { - inlay_hints.push(generate_inlay_hint(symbol, hint)); + inlay_hints.insert(generate_inlay_hint(symbol, hint)); } } } } - Some(inlay_hints) + Some( + inlay_hints + .into_iter() + .map(|h| into_lsp_inlay_hint(&h)) + .collect(), + ) } #[inline] -fn generate_inlay_hint(symbol: &KCLSymbol, hint: &SymbolHint) -> InlayHint { +fn generate_inlay_hint(symbol: &KCLSymbol, hint: &SymbolHint) -> KCLInlayHint { let (part, position) = get_hint_label(symbol, &hint); + KCLInlayHint { position, part } +} + +#[inline] +fn into_lsp_inlay_hint(hint: &KCLInlayHint) -> InlayHint { InlayHint { - position, - label: lsp_types::InlayHintLabel::LabelParts(vec![part]), + position: hint.position.clone(), + label: lsp_types::InlayHintLabel::LabelParts(vec![hint.part.clone()]), kind: None, text_edits: None, tooltip: None, - padding_left: Some(true), - padding_right: Some(true), + padding_left: None, + padding_right: None, data: None, } } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap index fb56296b3..efeded072 100644 --- a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap @@ -22,12 +22,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -48,12 +44,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -74,12 +66,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -100,12 +88,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -126,12 +110,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -152,12 +132,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -178,12 +154,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -204,12 +176,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -230,12 +198,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -256,12 +220,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, InlayHint { @@ -282,12 +242,8 @@ Some( kind: None, text_edits: None, tooltip: None, - padding_left: Some( - true, - ), - padding_right: Some( - true, - ), + padding_left: None, + padding_right: None, data: None, }, ],