Skip to content

Commit 8a1c867

Browse files
committed
refactor signature
1 parent d5ce2b7 commit 8a1c867

File tree

11 files changed

+76
-49
lines changed

11 files changed

+76
-49
lines changed

crates/code_analysis/src/compilation/analyzer/decl/exprs.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn analyze_closure_expr(analyzer: &mut DeclAnalyzer, expr: LuaClosureExpr) -
7979
let params = expr.get_params_list()?;
8080
let signature_id = LuaSignatureId::new(analyzer.get_file_id(), &expr);
8181

82-
for param in params.get_params() {
82+
for (idx, param) in params.get_params().enumerate() {
8383
let name = param.get_name_token().map_or_else(
8484
|| {
8585
if param.is_dots() {
@@ -95,12 +95,40 @@ pub fn analyze_closure_expr(analyzer: &mut DeclAnalyzer, expr: LuaClosureExpr) -
9595
name,
9696
file_id: analyzer.get_file_id(),
9797
range: param.get_range(),
98+
idx,
9899
signature_id,
99100
};
100101

101102
analyzer.add_decl(decl);
102103
}
103104

105+
analyze_closure_params(analyzer, &signature_id, &expr);
106+
107+
Some(())
108+
}
109+
110+
fn analyze_closure_params(
111+
analyzer: &mut DeclAnalyzer,
112+
signature_id: &LuaSignatureId,
113+
closure: &LuaClosureExpr,
114+
) -> Option<()> {
115+
let signature = analyzer
116+
.db
117+
.get_signature_index_mut()
118+
.get_or_create(signature_id.clone());
119+
let params = closure.get_params_list()?.get_params();
120+
for param in params {
121+
let name = if let Some(name_token) = param.get_name_token() {
122+
name_token.get_name_text().to_string()
123+
} else if param.is_dots() {
124+
"...".to_string()
125+
} else {
126+
return None;
127+
};
128+
129+
signature.params.push(name);
130+
}
131+
104132
Some(())
105133
}
106134

crates/code_analysis/src/compilation/analyzer/doc/file_generic_index.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ impl FileGenericIndex {
9595
for effect_id in self.root_node_ids.iter() {
9696
if self
9797
.effect_nodes
98-
.get(effect_id.id)
99-
.unwrap()
98+
.get(effect_id.id)?
10099
.range
101100
.contains(position)
102101
{

crates/code_analysis/src/compilation/analyzer/doc/type_ref_tags.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ pub fn analyze_param(analyzer: &mut DocAnalyzer, tag: LuaDocTagParam) -> Option<
123123
nullable,
124124
description,
125125
};
126-
signature.param_docs.insert(name.clone(), param_info);
126+
127+
let idx = signature.find_param_idx(&name)?;
128+
129+
signature.param_docs.insert(idx, param_info);
127130

128131
let param_list = closure.get_params_list()?;
129132
for param in param_list.get_params() {

crates/code_analysis/src/compilation/analyzer/lua/closure.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub fn analyze_closure(analyzer: &mut LuaAnalyzer, closure: LuaClosureExpr) -> O
1313
let signature_id = LuaSignatureId::new(analyzer.file_id, &closure);
1414

1515
analyze_colon_define(analyzer, &signature_id, &closure);
16-
analyze_params(analyzer, &signature_id, &closure);
1716
analyze_lambda_params(analyzer, &signature_id, &closure);
1817
analyze_return(analyzer, &signature_id, &closure);
1918
Some(())
@@ -39,31 +38,6 @@ fn analyze_colon_define(
3938
Some(())
4039
}
4140

42-
fn analyze_params(
43-
analyzer: &mut LuaAnalyzer,
44-
signature_id: &LuaSignatureId,
45-
closure: &LuaClosureExpr,
46-
) -> Option<()> {
47-
let signature = analyzer
48-
.db
49-
.get_signature_index_mut()
50-
.get_or_create(signature_id.clone());
51-
let params = closure.get_params_list()?.get_params();
52-
for param in params {
53-
let name = if let Some(name_token) = param.get_name_token() {
54-
name_token.get_name_text().to_string()
55-
} else if param.is_dots() {
56-
"...".to_string()
57-
} else {
58-
return None;
59-
};
60-
61-
signature.params.push(name);
62-
}
63-
64-
Some(())
65-
}
66-
6741
fn analyze_lambda_params(
6842
analyzer: &mut LuaAnalyzer,
6943
signature_id: &LuaSignatureId,

crates/code_analysis/src/compilation/analyzer/unresolve/resolve_closure_param.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ pub fn try_resolve_closure_params(
4242
return Some(true);
4343
};
4444

45+
4546
let signature_params = &mut signature.param_docs;
46-
for (name, type_ref) in expr_closure_params {
47-
if signature_params.contains_key(name) {
47+
for (idx, (name, type_ref)) in expr_closure_params.iter().enumerate() {
48+
if signature_params.contains_key(&idx) {
4849
continue;
4950
}
5051

5152
signature_params.insert(
52-
name.clone(),
53+
idx,
5354
LuaDocParamInfo {
5455
name: name.clone(),
5556
type_ref: type_ref.clone().unwrap_or(LuaType::Any),

crates/code_analysis/src/db_index/declaration/decl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub enum LuaDecl {
2121
name: String,
2222
file_id: FileId,
2323
range: TextRange,
24+
idx: usize,
2425
signature_id: LuaSignatureId,
2526
},
2627
Global {

crates/code_analysis/src/db_index/reference/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl LuaReferenceIndex {
9595
.get(&LuaMemberKey::Name(ArcIntern::new(name.to_string())))?
9696
.iter()
9797
.filter_map(|(key_file_id, syntax_ids)| {
98-
if *key_file_id != file_id {
98+
if *key_file_id == file_id {
9999
Some(syntax_ids.iter().map(|syntax_id| syntax_id.get_range()))
100100
} else {
101101
None

crates/code_analysis/src/db_index/signature/signature.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
pub struct LuaSignature {
1616
pub generic_params: Vec<(String, Option<LuaType>)>,
1717
pub overloads: Vec<Arc<LuaFunctionType>>,
18-
pub param_docs: HashMap<String, LuaDocParamInfo>,
18+
pub param_docs: HashMap<usize, LuaDocParamInfo>,
1919
pub params: Vec<String>,
2020
pub return_docs: Vec<LuaDocReturnInfo>,
2121
pub(crate) resolve_return: bool,
@@ -45,8 +45,8 @@ impl LuaSignature {
4545

4646
pub fn get_type_params(&self) -> Vec<(String, Option<LuaType>)> {
4747
let mut type_params = Vec::new();
48-
for param_name in &self.params {
49-
if let Some(param_info) = self.param_docs.get(param_name) {
48+
for (idx, param_name) in self.params.iter().enumerate() {
49+
if let Some(param_info) = self.param_docs.get(&idx) {
5050
type_params.push((param_name.clone(), Some(param_info.type_ref.clone())));
5151
} else {
5252
type_params.push((param_name.clone(), None));
@@ -56,8 +56,26 @@ impl LuaSignature {
5656
type_params
5757
}
5858

59-
pub fn get_param_doc(&self, param_name: &str) -> Option<&LuaDocParamInfo> {
60-
self.param_docs.get(param_name)
59+
pub fn find_param_idx(&self, param_name: &str) -> Option<usize> {
60+
self.params.iter().position(|name| name == param_name)
61+
}
62+
63+
pub fn get_param_info_by_name(&self, param_name: &str) -> Option<&LuaDocParamInfo> {
64+
// fast enough
65+
let idx = self.params.iter().position(|name| name == param_name)?;
66+
self.param_docs.get(&idx)
67+
}
68+
69+
pub fn get_param_info_by_id(&self, idx: usize) -> Option<&LuaDocParamInfo> {
70+
if idx < self.params.len() {
71+
return self.param_docs.get(&idx);
72+
} else if let Some(name) = self.params.last() {
73+
if name == "..." {
74+
return self.param_docs.get(&(self.params.len() - 1));
75+
}
76+
}
77+
78+
None
6179
}
6280
}
6381

crates/code_analysis/src/semantic/infer/infer_name.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,19 @@ pub fn infer_name_expr(
3434
} else if decl.is_param() {
3535
match decl {
3636
LuaDecl::Param {
37-
name, signature_id, ..
37+
idx, signature_id, ..
3838
} => {
3939
let signature = db.get_signature_index().get(&signature_id)?;
40-
let param_info = signature.get_param_doc(&name)?;
41-
let mut typ = param_info.type_ref.clone();
42-
if param_info.nullable && !typ.is_nullable() {
43-
typ = LuaType::Nullable(typ.into());
44-
}
40+
if let Some(param_info) = signature.get_param_info_by_id(*idx) {
41+
let mut typ = param_info.type_ref.clone();
42+
if param_info.nullable && !typ.is_nullable() {
43+
typ = LuaType::Nullable(typ.into());
44+
}
4545

46-
typ
46+
typ
47+
} else {
48+
LuaType::Unknown
49+
}
4750
}
4851
_ => unreachable!(),
4952
}

crates/code_analysis/src/semantic/semantic_info/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ pub fn infer_token_semantic_info(
4242
let decl = db.get_decl_index().get_decl(&decl_id)?;
4343
match decl {
4444
LuaDecl::Param {
45-
name, signature_id, ..
45+
idx, signature_id, ..
4646
} => {
4747
let signature = db.get_signature_index().get(&signature_id)?;
48-
let param_info = signature.get_param_doc(&name)?;
48+
let param_info = signature.get_param_info_by_id(*idx)?;
4949
let mut typ = param_info.type_ref.clone();
5050
if param_info.nullable && !typ.is_nullable() {
5151
typ = LuaType::Nullable(typ.into());

0 commit comments

Comments
 (0)