Skip to content

Commit

Permalink
fix: dup inlay hint and padding for hints
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jul 4, 2024
1 parent 497188a commit 9a24204
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 74 deletions.
54 changes: 46 additions & 8 deletions kclvm/tools/src/LSP/src/inlay_hints.rs
Original file line number Diff line number Diff line change
@@ -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<H: std::hash::Hasher>(&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<Vec<InlayHint>> {
let mut inlay_hints: Vec<InlayHint> = vec![];
let mut inlay_hints: IndexSet<KCLInlayHint> = 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,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
},
],
Expand Down

0 comments on commit 9a24204

Please sign in to comment.