Skip to content

Commit

Permalink
feat: enhance hint. (#1722)
Browse files Browse the repository at this point in the history
feat: enhance hint. 1. add more hint details for hint type and text_edit(double-click to insert). 2. split hint from symbol. 3. fix function call args hint

Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa authored Oct 28, 2024
1 parent f7be5ad commit 8302dcf
Show file tree
Hide file tree
Showing 9 changed files with 914 additions and 172 deletions.
143 changes: 86 additions & 57 deletions kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
core::{
scope::{ConfigScopeContext, LocalSymbolScopeKind},
symbol::{
CommentOrDocSymbol, DecoratorSymbol, ExpressionSymbol, Symbol, SymbolHint,
SymbolHintKind, SymbolRef, SymbolSemanticInfo, UnresolvedSymbol, ValueSymbol,
CommentOrDocSymbol, DecoratorSymbol, ExpressionSymbol, SymbolHint, SymbolHintKind,
SymbolRef, SymbolSemanticInfo, UnresolvedSymbol, ValueSymbol,
},
},
ty::{Parameter, Type, TypeKind, ANY_TYPE_STR, SCHEMA_MEMBER_FUNCTIONS},
Expand Down Expand Up @@ -499,25 +499,38 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
&target.node.names.last().unwrap().id
};
let value = self.gs.get_symbols_mut().alloc_value_symbol(
ValueSymbol::new(name.clone(), start_pos, end_pos, None, false),
ValueSymbol::new(name.clone(), start_pos, end_pos.clone(), None, false),
self.ctx.get_node_key(&ast_id),
self.ctx.current_pkgpath.clone().unwrap(),
);
self.gs
.get_scopes_mut()
.add_def_to_scope(cur_scope, name, value);
if let Some(symbol) = self.gs.get_symbols_mut().values.get_mut(value.get_id()) {
let ty = self
.ctx
.node_ty_map
.borrow()
.get(&self.ctx.get_node_key(ast_id))
.map(|ty| ty.clone());
symbol.hint = ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: symbol.get_range().1,
});
symbol.sema_info = SymbolSemanticInfo { ty, doc: None };
let symbols = self.gs.get_symbols_mut();
let ty = match symbols.values.get_mut(value.get_id()) {
Some(symbol) => {
let ty = self
.ctx
.node_ty_map
.borrow()
.get(&self.ctx.get_node_key(ast_id))
.map(|ty| ty.clone());
symbol.sema_info = SymbolSemanticInfo {
ty: ty.clone(),
doc: None,
};
ty
}
None => None,
};
if let Some(ty) = ty {
symbols.alloc_hint(
SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: end_pos,
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
}

Expand Down Expand Up @@ -1595,12 +1608,9 @@ impl<'ctx> AdvancedResolver<'ctx> {
.get(&self.ctx.get_node_key(&&target.id))
.map(|symbol_ref| *symbol_ref)
{
if let Some(symbol) = self
.gs
.get_symbols_mut()
.values
.get_mut(identifier_symbol.get_id())
{
let symbols = self.gs.get_symbols_mut();

if let Some(symbol) = symbols.values.get_mut(identifier_symbol.get_id()) {
let id = if let Some(last) = target.node.paths.last() {
last.id()
} else {
Expand All @@ -1612,13 +1622,20 @@ impl<'ctx> AdvancedResolver<'ctx> {
.borrow()
.get(&self.ctx.get_node_key(&id))
.map(|ty| ty.clone());
if with_hint {
symbol.hint = ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: symbol.get_range().1,
});

symbol.sema_info = SymbolSemanticInfo {
ty: ty.clone(),
doc: None,
};
if with_hint & ty.is_some() {
symbols.alloc_hint(
SymbolHint {
kind: SymbolHintKind::TypeHint(ty.unwrap().ty_hint()),
pos: target.get_end_pos(),
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
symbol.sema_info = SymbolSemanticInfo { ty, doc: None };
}

let cur_scope = *self.ctx.scopes.last().unwrap();
Expand Down Expand Up @@ -1664,12 +1681,9 @@ impl<'ctx> AdvancedResolver<'ctx> {
.get(&self.ctx.get_node_key(&&identifier.id))
.map(|symbol_ref| *symbol_ref)
{
if let Some(symbol) = self
.gs
.get_symbols_mut()
.values
.get_mut(identifier_symbol.get_id())
{
let symbols = self.gs.get_symbols_mut();

if let Some(symbol) = symbols.values.get_mut(identifier_symbol.get_id()) {
let id = if identifier.node.names.is_empty() {
&identifier.id
} else {
Expand All @@ -1679,15 +1693,22 @@ impl<'ctx> AdvancedResolver<'ctx> {
.ctx
.node_ty_map
.borrow()
.get(&self.ctx.get_node_key(id))
.get(&self.ctx.get_node_key(&id))
.map(|ty| ty.clone());
if with_hint {
symbol.hint = ty.as_ref().map(|ty| SymbolHint {
kind: SymbolHintKind::TypeHint(ty.ty_hint()),
pos: symbol.get_range().1,
});

symbol.sema_info = SymbolSemanticInfo {
ty: ty.clone(),
doc: None,
};
if with_hint & ty.is_some() {
symbols.alloc_hint(
SymbolHint {
kind: SymbolHintKind::TypeHint(ty.unwrap().ty_hint()),
pos: identifier.get_end_pos(),
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
symbol.sema_info = SymbolSemanticInfo { ty, doc: None };
}

if self.ctx.maybe_def && identifier.node.names.len() > 0 {
Expand Down Expand Up @@ -1842,20 +1863,12 @@ impl<'ctx> AdvancedResolver<'ctx> {
ast::Expr::Identifier(id) => id.names.last().unwrap().id.clone(),
_ => arg.id.clone(),
};
if let Some(arg_ref) = symbol_data
match symbol_data
.symbols_info
.node_symbol_map
.get(&self.ctx.get_node_key(&id))
{
match arg_ref.get_kind() {
crate::core::symbol::SymbolKind::Expression => {
if let Some(expr) = symbol_data.exprs.get_mut(arg_ref.get_id()) {
expr.hint = Some(SymbolHint {
pos: arg.get_pos(),
kind: SymbolHintKind::VarHint(param.name.clone()),
});
}
}
Some(arg_ref) => match arg_ref.get_kind() {
crate::core::symbol::SymbolKind::Unresolved => {
let mut has_hint = false;
if let Some(unresolved) =
Expand All @@ -1870,17 +1883,33 @@ impl<'ctx> AdvancedResolver<'ctx> {
}
}
if has_hint {
if let Some(unresolved) =
symbol_data.unresolved.get_mut(arg_ref.get_id())
{
unresolved.hint = Some(SymbolHint {
symbol_data.alloc_hint(
SymbolHint {
kind: SymbolHintKind::VarHint(param.name.clone()),
pos: arg.get_pos(),
});
}
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
}
_ => {}
_ => {
symbol_data.alloc_hint(
SymbolHint {
kind: SymbolHintKind::VarHint(param.name.clone()),
pos: arg.get_pos(),
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
},
None => {
symbol_data.alloc_hint(
SymbolHint {
kind: SymbolHintKind::VarHint(param.name.clone()),
pos: arg.get_pos(),
},
self.ctx.current_pkgpath.clone().unwrap(),
);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions kclvm/sema/src/core/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ impl GlobalState {
);
}

for (_, hints) in &self.symbols.hints {
for hint in hints {
if file_sema_map_cache.contains_key(&hint.pos.filename) {
continue;
}
let filename = &hint.pos.filename;
if !file_sema_map.contains_key(filename) {
file_sema_map.insert(filename.clone(), FileSemanticInfo::new(filename.clone()));
}
let file_sema_info = file_sema_map.get_mut(filename).unwrap();
file_sema_info.hints.push(hint.clone());
}
}

// remove dummy file
file_sema_map.remove("");

Expand Down
11 changes: 10 additions & 1 deletion kclvm/sema/src/core/semantic_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use indexmap::IndexMap;
use kclvm_ast::ast::AstIndex;
use std::sync::Arc;

use super::{scope::ScopeRef, symbol::SymbolRef};
use super::{
scope::ScopeRef,
symbol::{SymbolHint, SymbolRef},
};
use crate::ty::Type;
#[allow(unused)]
#[derive(Debug, Default, Clone)]
Expand All @@ -25,6 +28,7 @@ pub struct FileSemanticInfo {
pub(crate) scopes: Vec<ScopeRef>,
pub(crate) symbol_locs: IndexMap<SymbolRef, CachedLocation>,
pub(crate) local_scope_locs: IndexMap<ScopeRef, CachedRange>,
pub(crate) hints: Vec<SymbolHint>,
}

impl FileSemanticInfo {
Expand All @@ -35,6 +39,7 @@ impl FileSemanticInfo {
scopes: vec![],
symbol_locs: IndexMap::default(),
local_scope_locs: IndexMap::default(),
hints: vec![],
}
}

Expand All @@ -57,6 +62,10 @@ impl FileSemanticInfo {
pub fn get_symbols(&self) -> &Vec<SymbolRef> {
&self.symbols
}

pub fn get_hints(&self) -> &Vec<SymbolHint> {
&self.hints
}
}

#[derive(Debug, Eq, PartialEq, Clone)]
Expand Down
Loading

0 comments on commit 8302dcf

Please sign in to comment.