Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lsp hint for config entry key type #1751

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use kclvm_ast::pos::GetPos;
use kclvm_ast::walker::MutSelfTypedResultWalker;
use kclvm_error::{diagnostic::Range, Position};

use crate::core::symbol::Symbol;
use crate::{
core::{
scope::{ConfigScopeContext, LocalSymbolScopeKind},
Expand Down Expand Up @@ -1977,7 +1978,27 @@ impl<'ctx> AdvancedResolver<'_> {

if let Some(key) = &entry.node.key {
self.ctx.maybe_def = true;
self.expr(key)?;
if let Some(symbol_ref) = self.expr(key)? {
if let Some(config_key_symbol) =
self.gs.get_symbols().unresolved.get(symbol_ref.get_id())
{
if let Some(def_ref) = config_key_symbol.get_definition() {
if let Some(def_symbol) = self.gs.get_symbols().get_symbol(def_ref) {
let ty = def_symbol.get_sema_info().ty.clone();
let symbols = self.gs.get_symbols_mut();
if let Some(ty) = ty {
symbols.alloc_hint(
SymbolHint {
kind: SymbolHintKind::KeyTypeHint(ty.ty_hint()),
pos: key.get_end_pos(),
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
}
}
}
}
self.ctx.maybe_def = false;
}
}
Expand Down
5 changes: 3 additions & 2 deletions kclvm/sema/src/core/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ pub struct SymbolDB {
pub(crate) pkg_symbol_map: IndexMap<String, IndexSet<SymbolRef>>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SymbolHint {
pub kind: SymbolHintKind,
pub pos: Position,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SymbolHintKind {
TypeHint(String),
VarHint(String),
KeyTypeHint(String),
}

impl SymbolData {
Expand Down
45 changes: 37 additions & 8 deletions kclvm/tools/src/LSP/src/inlay_hints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use indexmap::IndexSet;
use indexmap::{IndexMap, IndexSet};
use kclvm_sema::core::global_state::GlobalState;
use kclvm_sema::core::symbol::{SymbolHint, SymbolHintKind};
use lsp_types::{
Expand Down Expand Up @@ -43,18 +43,34 @@ pub fn inlay_hints(file: &str, gs: &GlobalState) -> Option<Vec<InlayHint>> {
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 mut line_hint: IndexMap<u64, SymbolHint> = IndexMap::new();
for hint in file_sema.get_hints() {
inlay_hints.insert(generate_inlay_hint(hint));
match &hint.kind {
SymbolHintKind::KeyTypeHint(_) => match line_hint.get(&hint.pos.line) {
Some(h) => {
if hint.pos.column.unwrap_or_default() > h.pos.column.unwrap_or_default() {
line_hint.insert(hint.pos.line, hint.clone());
}
}
None => {
line_hint.insert(hint.pos.line, hint.clone());
}
},
_ => {
inlay_hints.insert(generate_inlay_hint(hint));
}
}
}
inlay_hints.extend(line_hint.values().map(|h| generate_inlay_hint(h)));
}

Some(
inlay_hints
.into_iter()
.map(|h| into_lsp_inlay_hint(&h))
.collect(),
)
}

#[inline]
fn generate_inlay_hint(hint: &SymbolHint) -> KCLInlayHint {
let (part, position, kind) = get_hint_label(hint);
Expand All @@ -67,6 +83,7 @@ fn generate_inlay_hint(hint: &SymbolHint) -> KCLInlayHint {
new_text: part.value.clone(),
}]),
SymbolHintKind::VarHint(_) => None,
SymbolHintKind::KeyTypeHint(_) => None,
};
KCLInlayHint {
position,
Expand Down Expand Up @@ -108,6 +125,14 @@ fn get_hint_label(hint: &SymbolHint) -> (InlayHintLabelPart, LspPosition, InlayH
lsp_pos(&hint.pos),
InlayHintKind::PARAMETER,
),
SymbolHintKind::KeyTypeHint(ty) => (
InlayHintLabelPart {
value: format!(": {ty}"),
..Default::default()
},
lsp_pos(&hint.pos),
InlayHintKind::TYPE,
),
}
}

Expand Down Expand Up @@ -143,9 +168,13 @@ mod tests {
"src/test_data/inlay_hints/schema_args/schema_args_hint.k"
);

// Temporary revert
// inlay_hints_test_snapshot!(
// test_config_key_ty,
// "src/test_data/inlay_hints/config_key/config_key.k"
// );
inlay_hints_test_snapshot!(
test_config_key_ty,
"src/test_data/inlay_hints/config_key/config_key.k"
);

inlay_hints_test_snapshot!(
test_config_key_ty_1,
"src/test_data/inlay_hints/config_key1/config_key.k"
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tools/src/LSP/src/inlay_hints.rs
expression: "format!(\"{:#?}\", res)"
expression: "format! (\"{:#?}\", res)"
---
Some(
[
Expand All @@ -19,8 +19,26 @@ Some(
},
],
),
kind: None,
text_edits: None,
kind: Some(
Type,
),
text_edits: Some(
[
TextEdit {
range: Range {
start: Position {
line: 6,
character: 3,
},
end: Position {
line: 6,
character: 3,
},
},
new_text: ": App",
},
],
),
tooltip: None,
padding_left: None,
padding_right: None,
Expand All @@ -41,7 +59,9 @@ Some(
},
],
),
kind: None,
kind: Some(
Type,
),
text_edits: None,
tooltip: None,
padding_left: None,
Expand All @@ -63,7 +83,9 @@ Some(
},
],
),
kind: None,
kind: Some(
Type,
),
text_edits: None,
tooltip: None,
padding_left: None,
Expand All @@ -85,7 +107,9 @@ Some(
},
],
),
kind: None,
kind: Some(
Type,
),
text_edits: None,
tooltip: None,
padding_left: None,
Expand All @@ -107,7 +131,9 @@ Some(
},
],
),
kind: None,
kind: Some(
Type,
),
text_edits: None,
tooltip: None,
padding_left: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
source: tools/src/LSP/src/inlay_hints.rs
expression: "format! (\"{:#?}\", res)"
---
Some(
[
InlayHint {
position: Position {
line: 6,
character: 1,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": Person",
tooltip: None,
location: None,
command: None,
},
],
),
kind: Some(
Type,
),
text_edits: Some(
[
TextEdit {
range: Range {
start: Position {
line: 6,
character: 1,
},
end: Position {
line: 6,
character: 1,
},
},
new_text: ": Person",
},
],
),
tooltip: None,
padding_left: None,
padding_right: None,
data: None,
},
InlayHint {
position: Position {
line: 7,
character: 13,
},
label: LabelParts(
[
InlayHintLabelPart {
value: ": str",
tooltip: None,
location: None,
command: None,
},
],
),
kind: Some(
Type,
),
text_edits: None,
tooltip: None,
padding_left: None,
padding_right: None,
data: None,
},
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
schema Name:
name: str

schema Person:
name: Name

p = Person{
name.name = ""
}
Loading