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: enhance hint. #1722

Merged
merged 1 commit into from
Oct 28, 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
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
Loading