Skip to content

Commit 788eb1e

Browse files
committed
remove runtime.class_default_call
1 parent a25a73c commit 788eb1e

File tree

8 files changed

+52
-181
lines changed

8 files changed

+52
-181
lines changed

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@
110110
"runtime": {
111111
"$ref": "#/$defs/EmmyrcRuntime",
112112
"default": {
113-
"classDefaultCall": {
114-
"forceNonColon": false,
115-
"forceReturnSelf": false,
116-
"functionName": ""
117-
},
118113
"extensions": [],
119114
"frameworkVersions": [],
120115
"nonstandardSymbol": [],
@@ -163,26 +158,6 @@
163158
}
164159
},
165160
"$defs": {
166-
"ClassDefaultCall": {
167-
"type": "object",
168-
"properties": {
169-
"forceNonColon": {
170-
"description": "Mandatory non`:` definition. When `function_name` is not empty, it takes effect.",
171-
"type": "boolean",
172-
"default": true
173-
},
174-
"forceReturnSelf": {
175-
"description": "Force to return `self`.",
176-
"type": "boolean",
177-
"default": true
178-
},
179-
"functionName": {
180-
"description": "class default overload function. eg. \"__init\".",
181-
"type": "string",
182-
"default": ""
183-
}
184-
}
185-
},
186161
"DiagnosticCode": {
187162
"oneOf": [
188163
{
@@ -952,15 +927,6 @@
952927
"EmmyrcRuntime": {
953928
"type": "object",
954929
"properties": {
955-
"classDefaultCall": {
956-
"description": "class default overload function.",
957-
"$ref": "#/$defs/ClassDefaultCall",
958-
"default": {
959-
"forceNonColon": false,
960-
"forceReturnSelf": false,
961-
"functionName": ""
962-
}
963-
},
964930
"extensions": {
965931
"description": "file Extensions. eg: .lua, .lua.txt",
966932
"type": "array",

crates/emmylua_code_analysis/src/compilation/analyzer/lua/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl LuaAnalyzer<'_> {
110110
}
111111
}
112112

113+
#[allow(unused)]
113114
pub fn get_emmyrc(&self) -> &Emmyrc {
114115
self.db.get_emmyrc()
115116
}

crates/emmylua_code_analysis/src/compilation/analyzer/lua/stats.rs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use emmylua_parser::{
2-
BinaryOperator, LuaAssignStat, LuaAstNode, LuaAstToken, LuaExpr, LuaFuncStat, LuaIndexExpr,
2+
BinaryOperator, LuaAssignStat, LuaAstNode, LuaExpr, LuaFuncStat, LuaIndexExpr,
33
LuaLocalFuncStat, LuaLocalStat, LuaTableField, LuaVarExpr, PathTrait,
44
};
55

66
use crate::{
7-
InFiled, InferFailReason, LuaOperator, LuaOperatorMetaMethod, LuaOperatorOwner, LuaTypeCache,
8-
LuaTypeOwner, OperatorFunction,
7+
InFiled, InferFailReason, LuaTypeCache, LuaTypeOwner,
98
compilation::analyzer::{
109
common::{add_member, bind_type},
1110
unresolve::{UnResolveDecl, UnResolveMember},
@@ -415,8 +414,6 @@ pub fn analyze_func_stat(analyzer: &mut LuaAnalyzer, func_stat: LuaFuncStat) ->
415414
.get_type_index_mut()
416415
.bind_type(type_owner, LuaTypeCache::InferType(signature_type.clone()));
417416

418-
try_add_class_default_call(analyzer, func_name, signature_type);
419-
420417
Some(())
421418
}
422419

@@ -503,53 +500,3 @@ fn special_assign_pattern(
503500

504501
Some(())
505502
}
506-
507-
pub fn try_add_class_default_call(
508-
analyzer: &mut LuaAnalyzer,
509-
func_name: LuaVarExpr,
510-
signature_type: LuaType,
511-
) -> Option<()> {
512-
let LuaType::Signature(signature_id) = signature_type else {
513-
return None;
514-
};
515-
516-
let default_name = &analyzer
517-
.get_emmyrc()
518-
.runtime
519-
.class_default_call
520-
.function_name;
521-
522-
if default_name.is_empty() {
523-
return None;
524-
}
525-
if let LuaVarExpr::IndexExpr(index_expr) = func_name {
526-
let index_key = index_expr.get_index_key()?;
527-
if index_key.get_path_part() == *default_name {
528-
let prefix_expr = index_expr.get_prefix_expr()?;
529-
if let Ok(prefix_type) = analyzer.infer_expr(&prefix_expr)
530-
&& let LuaType::Def(decl_id) = prefix_type
531-
{
532-
// 如果已经存在, 则不添加
533-
let call = analyzer.db.get_operator_index().get_operators(
534-
&LuaOperatorOwner::Type(decl_id.clone()),
535-
LuaOperatorMetaMethod::Call,
536-
);
537-
if call.is_some() {
538-
return None;
539-
}
540-
541-
let operator = LuaOperator::new(
542-
decl_id.into(),
543-
LuaOperatorMetaMethod::Call,
544-
analyzer.file_id,
545-
// 必须指向名称, 使用 index_expr 的完整范围不会跳转到函数上
546-
index_expr.get_name_token()?.syntax().text_range(),
547-
OperatorFunction::DefaultCall(signature_id),
548-
);
549-
analyzer.db.get_operator_index_mut().add_operator(operator);
550-
}
551-
}
552-
}
553-
554-
Some(())
555-
}

crates/emmylua_code_analysis/src/compilation/test/annotation_test.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,16 @@ mod test {
127127

128128
#[test]
129129
fn test_generic_type_extends() {
130-
let mut ws = VirtualWorkspace::new();
131-
let mut emmyrc = ws.get_emmyrc();
132-
emmyrc.runtime.class_default_call.force_non_colon = true;
133-
emmyrc.runtime.class_default_call.force_return_self = true;
134-
emmyrc.runtime.class_default_call.function_name = "__init".to_string();
135-
ws.update_emmyrc(emmyrc);
130+
let mut ws = VirtualWorkspace::new_with_init_std_lib();
131+
ws.def(
132+
r#"
133+
---@generic T
134+
---@param [class_ctor("__init")] name `T`
135+
---@return T
136+
function meta(name)
137+
end
138+
"#,
139+
);
136140
ws.def(
137141
r#"
138142
---@class State
@@ -141,7 +145,7 @@ mod test {
141145
---@class StateMachine<T: State>
142146
---@field aaa T
143147
---@field new fun(self: self): self
144-
StateMachine = {}
148+
StateMachine = meta("StateMachine")
145149
146150
---@return self
147151
function StateMachine:abc()

crates/emmylua_code_analysis/src/compilation/test/overload_test.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[cfg(test)]
22
mod test {
3-
use std::{ops::Deref, sync::Arc};
43

54
use crate::{DiagnosticCode, VirtualWorkspace};
65

@@ -31,18 +30,24 @@ mod test {
3130
#[test]
3231
fn test_class_default_call() {
3332
let mut ws = VirtualWorkspace::new();
34-
let mut emmyrc = ws.analysis.emmyrc.deref().clone();
35-
emmyrc.runtime.class_default_call.function_name = "__init".to_string();
36-
emmyrc.runtime.class_default_call.force_non_colon = true;
37-
emmyrc.runtime.class_default_call.force_return_self = true;
38-
ws.analysis.update_config(Arc::new(emmyrc));
33+
ws.def(
34+
r#"
35+
---@attribute class_ctor(name: string, strip_self: boolean?, return_self: boolean?)
36+
37+
---@generic T
38+
---@param [class_ctor("__init")] name `T`
39+
---@return T
40+
function meta(name)
41+
end
42+
"#,
43+
);
3944

4045
ws.def(
4146
r#"
4247
---@class MyClass
43-
local M = {}
48+
local M = meta("MyClass")
4449
45-
function M:__init(a)
50+
function M:__init()
4651
end
4752
4853
A = M()

crates/emmylua_code_analysis/src/config/configs/runtime.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ pub struct EmmyrcRuntime {
2424
#[serde(default)]
2525
/// Require pattern. eg. "?.lua", "?/init.lua"
2626
pub require_pattern: Vec<String>,
27-
#[serde(default)]
28-
/// class default overload function.
29-
pub class_default_call: ClassDefaultCall,
3027
/// Non-standard symbols.
3128
#[serde(default)]
3229
pub nonstandard_symbol: Vec<EmmyrcNonStdSymbol>,
@@ -75,20 +72,7 @@ impl EmmyrcLuaVersion {
7572
}
7673
}
7774

78-
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone, Default)]
79-
#[serde(rename_all = "camelCase")]
80-
pub struct ClassDefaultCall {
81-
#[serde(default)]
82-
/// class default overload function. eg. "__init".
83-
pub function_name: String,
84-
#[serde(default = "default_true")]
85-
/// Mandatory non`:` definition. When `function_name` is not empty, it takes effect.
86-
pub force_non_colon: bool,
87-
/// Force to return `self`.
88-
#[serde(default = "default_true")]
89-
pub force_return_self: bool,
90-
}
91-
75+
#[allow(unused)]
9276
fn default_true() -> bool {
9377
true
9478
}

crates/emmylua_code_analysis/src/db_index/operators/lua_operator.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub struct LuaOperator {
2323
pub enum OperatorFunction {
2424
Func(Arc<LuaFunctionType>),
2525
Signature(LuaSignatureId),
26-
DefaultCall(LuaSignatureId),
2726
DefaultClassCtor {
2827
id: LuaSignatureId,
2928
strip_self: bool,
@@ -78,7 +77,6 @@ impl LuaOperator {
7877
LuaType::Any
7978
}
8079
// 只有 .field 才有`operand`, call 不会有这个
81-
OperatorFunction::DefaultCall(_) => LuaType::Unknown,
8280
OperatorFunction::DefaultClassCtor { .. } => LuaType::Unknown,
8381
}
8482
}
@@ -101,25 +99,6 @@ impl LuaOperator {
10199

102100
Ok(LuaType::Any)
103101
}
104-
OperatorFunction::DefaultCall(signature_id) => {
105-
let emmyrc = db.get_emmyrc();
106-
if emmyrc.runtime.class_default_call.force_return_self {
107-
return Ok(LuaType::SelfInfer);
108-
}
109-
110-
if let Some(signature) = db.get_signature_index().get(signature_id) {
111-
if signature.resolve_return == SignatureReturnStatus::UnResolve {
112-
return Err(InferFailReason::UnResolveSignatureReturn(*signature_id));
113-
}
114-
115-
let return_type = signature.return_docs.first();
116-
if let Some(return_type) = return_type {
117-
return Ok(return_type.type_ref.clone());
118-
}
119-
}
120-
121-
Ok(LuaType::Any)
122-
}
123102
OperatorFunction::DefaultClassCtor {
124103
id, return_self, ..
125104
} => {
@@ -144,32 +123,6 @@ impl LuaOperator {
144123
match &self.func {
145124
OperatorFunction::Func(func) => LuaType::DocFunction(func.clone()),
146125
OperatorFunction::Signature(signature) => LuaType::Signature(*signature),
147-
OperatorFunction::DefaultCall(signature_id) => {
148-
let emmyrc = db.get_emmyrc();
149-
150-
if let Some(signature) = db.get_signature_index().get(signature_id) {
151-
let params = signature.get_type_params();
152-
let is_colon_define = if emmyrc.runtime.class_default_call.force_non_colon {
153-
false
154-
} else {
155-
signature.is_colon_define
156-
};
157-
let return_type = if emmyrc.runtime.class_default_call.force_return_self {
158-
LuaType::SelfInfer
159-
} else {
160-
signature.get_return_type()
161-
};
162-
let func_type = LuaFunctionType::new(
163-
signature.async_state,
164-
is_colon_define,
165-
params,
166-
return_type,
167-
);
168-
return LuaType::DocFunction(Arc::new(func_type));
169-
}
170-
171-
LuaType::Signature(*signature_id)
172-
}
173126
OperatorFunction::DefaultClassCtor {
174127
id,
175128
strip_self,

crates/emmylua_ls/src/handlers/test/inlay_hint_test.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(test)]
22
mod tests {
33
use googletest::prelude::*;
4-
use std::{ops::Deref, sync::Arc};
54

65
use crate::handlers::test_lib::{ProviderVirtualWorkspace, VirtualInlayHint, check};
76

@@ -138,28 +137,40 @@ mod tests {
138137
#[gtest]
139138
fn test_class_call_hint() -> Result<()> {
140139
let mut ws = ProviderVirtualWorkspace::new();
141-
let mut emmyrc = ws.analysis.get_emmyrc().deref().clone();
142-
emmyrc.runtime.class_default_call.function_name = "__init".to_string();
143-
emmyrc.runtime.class_default_call.force_non_colon = true;
144-
emmyrc.runtime.class_default_call.force_return_self = true;
145-
ws.analysis.update_config(Arc::new(emmyrc));
140+
ws.def(
141+
r#"
142+
---@generic T
143+
---@param [class_ctor("__init")] name `T`
144+
---@return T
145+
function meta(name)
146+
end
147+
"#,
148+
);
146149

147150
check!(ws.check_inlay_hint(
148151
r#"
149152
---@class MyClass
150-
local A
153+
local A = meta("MyClass")
151154
152155
function A:__init(a)
153156
end
154157
155158
A()
156159
"#,
157-
vec![VirtualInlayHint {
158-
label: "new".to_string(),
159-
line: 7,
160-
pos: 16,
161-
ref_file: Some("".to_string()),
162-
}]
160+
vec![
161+
VirtualInlayHint {
162+
label: "name:".to_string(),
163+
line: 2,
164+
pos: 31,
165+
ref_file: Some("".to_string()),
166+
},
167+
VirtualInlayHint {
168+
label: "new".to_string(),
169+
line: 7,
170+
pos: 16,
171+
ref_file: Some("".to_string()),
172+
}
173+
]
163174
));
164175
Ok(())
165176
}

0 commit comments

Comments
 (0)