Skip to content

Commit 9a574e7

Browse files
authored
Merge pull request #648 from xuhuanzy/merge
update based on flow
2 parents 3c7a37f + 14ca723 commit 9a574e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2439
-406
lines changed

crates/emmylua_code_analysis/locales/lint.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,8 @@ Cannot use `...` outside a vararg function.:
268268
"Unknown doc tag: `%{name}`":
269269
en: "Unknown doc tag: `%{name}`"
270270
# TODO: translate
271+
272+
"Value '%{value}' does not match any enum value. Expected one of: %{enum_values}":
273+
en: "Value '%{value}' does not match any enum value. Expected one of: %{enum_values}"
274+
zh_CN: "值 '%{value}' 与任何枚举值都不匹配。应为以下之一: %{enum_values}"
275+
zh_HK: "值 '%{value}' 與任何枚舉值都不匹配。應為以下之一: %{enum_values}"

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"$ref": "#/$defs/EmmyrcInlayHint",
6464
"default": {
6565
"enable": true,
66+
"enumParamHint": false,
6667
"indexHint": true,
6768
"localHint": true,
6869
"metaCallHint": true,
@@ -401,6 +402,11 @@
401402
"description": "require-module-not-visible",
402403
"type": "string",
403404
"const": "require-module-not-visible"
405+
},
406+
{
407+
"description": "enum-value-mismatch",
408+
"type": "string",
409+
"const": "enum-value-mismatch"
404410
}
405411
]
406412
},
@@ -632,6 +638,11 @@
632638
"type": "boolean",
633639
"default": true
634640
},
641+
"enumParamHint": {
642+
"description": "Whether to enable enum parameter hints.",
643+
"type": "boolean",
644+
"default": false
645+
},
635646
"indexHint": {
636647
"description": "Whether to enable index hints.",
637648
"type": "boolean",

crates/emmylua_code_analysis/src/compilation/analyzer/doc/field_or_operator_def_tags.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ pub fn analyze_field(analyzer: &mut DocAnalyzer, tag: LuaDocTagField) -> Option<
144144
}
145145

146146
if !description.is_empty() {
147+
// 不需要传入`owner`, 当前`owner`的效果是判断是否为`signature`, 如果是则不移除`['#', '@']`首字符
148+
// 但以`field`定义的必须移除首字符
149+
let description = preprocess_description(&description, None);
147150
analyzer.db.get_property_index_mut().add_description(
148151
analyzer.file_id,
149152
property_owner.clone(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ impl<'a> DocAnalyzer<'a> {
9393
}
9494

9595
pub fn preprocess_description(mut description: &str, owner: Option<&LuaSemanticDeclId>) -> String {
96-
let has_remove_start_char = if let Some(owner) = owner {
96+
let need_remove_start_char = if let Some(owner) = owner {
9797
!matches!(owner, LuaSemanticDeclId::Signature(_))
9898
} else {
9999
true
100100
};
101-
if has_remove_start_char {
101+
if need_remove_start_char {
102102
if description.starts_with(['#', '@']) {
103103
description = description.trim_start_matches(|c| c == '#' || c == '@');
104104
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub fn infer_for_range_iter_expr_func(
155155
cache,
156156
substitutor: &mut substitutor,
157157
root: root,
158+
call_expr: None,
158159
};
159160
let params = doc_function
160161
.get_params()

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,10 @@ pub fn analyze_table_field(analyzer: &mut LuaAnalyzer, field: LuaTableField) ->
445445
let value_expr = field.get_value_expr()?;
446446
let member_id = LuaMemberId::new(field.get_syntax_id(), analyzer.file_id);
447447
let value_type = match analyzer.infer_expr(&value_expr.clone().into()) {
448-
Ok(value_type) => value_type,
448+
Ok(value_type) => match value_type {
449+
LuaType::Def(ref_id) => LuaType::Ref(ref_id),
450+
_ => value_type,
451+
},
449452
Err(InferFailReason::None) => LuaType::Unknown,
450453
Err(reason) => {
451454
let unresolve = UnResolveMember {

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,66 @@ mod test {
125125
));
126126
}
127127

128+
#[test]
129+
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);
136+
ws.def(
137+
r#"
138+
---@class State
139+
---@field a string
140+
141+
---@class StateMachine<T: State>
142+
---@field aaa T
143+
---@field new fun(self: self): self
144+
StateMachine = {}
145+
146+
---@return self
147+
function StateMachine:abc()
148+
end
149+
150+
151+
---@return self
152+
function StateMachine:__init()
153+
end
154+
"#,
155+
);
156+
{
157+
ws.def(
158+
r#"
159+
A = StateMachine:new()
160+
"#,
161+
);
162+
let ty = ws.expr_ty("A");
163+
let expected = ws.ty("StateMachine<State>");
164+
assert_eq!(ty, expected);
165+
}
166+
{
167+
ws.def(
168+
r#"
169+
B = StateMachine:abc()
170+
"#,
171+
);
172+
let ty = ws.expr_ty("B");
173+
let expected = ws.ty("StateMachine<State>");
174+
assert_eq!(ty, expected);
175+
}
176+
{
177+
ws.def(
178+
r#"
179+
C = StateMachine:abc()
180+
"#,
181+
);
182+
let ty = ws.expr_ty("C");
183+
let expected = ws.ty("StateMachine<State>");
184+
assert_eq!(ty, expected);
185+
}
186+
}
187+
128188
#[test]
129189
fn test_type_return_usage() {
130190
let mut ws = VirtualWorkspace::new();

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,4 +1258,30 @@ end
12581258
"#,
12591259
));
12601260
}
1261+
1262+
#[test]
1263+
fn test_self_1() {
1264+
let mut ws = VirtualWorkspace::new();
1265+
ws.def(
1266+
r#"
1267+
---@class Node
1268+
---@field parent? Node
1269+
1270+
---@class Subject<T>: Node
1271+
---@field package root? Node
1272+
Subject = {}
1273+
"#,
1274+
);
1275+
ws.def(
1276+
r#"
1277+
function Subject:add()
1278+
if self == self.parent then
1279+
A = self
1280+
end
1281+
end
1282+
"#,
1283+
);
1284+
let a = ws.expr_ty("A");
1285+
assert_eq!(ws.humanize_type(a), "Node");
1286+
}
12611287
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,33 @@ mod test {
6767
let expected = ws.ty("fun(b:string, c:table)");
6868
assert_eq!(bar_ty, expected);
6969
}
70+
71+
#[test]
72+
fn test_generic_params() {
73+
let mut ws = VirtualWorkspace::new();
74+
ws.def(
75+
r#"
76+
---@class Observable<T>
77+
---@class Subject<T>: Observable<T>
78+
79+
---@generic T
80+
---@param ... Observable<T>
81+
---@return Observable<T>
82+
function concat(...)
83+
end
84+
"#,
85+
);
86+
87+
ws.def(
88+
r#"
89+
---@type Subject<number>
90+
local s1
91+
A = concat(s1)
92+
"#,
93+
);
94+
95+
let a_ty = ws.expr_ty("A");
96+
let expected = ws.ty("Observable<number>");
97+
assert_eq!(a_ty, expected);
98+
}
7099
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub struct EmmyrcInlayHint {
2323
/// Whether to enable meta __call operator hints.
2424
#[serde(default = "default_true")]
2525
pub meta_call_hint: bool,
26+
/// Whether to enable enum parameter hints.
27+
#[serde(default = "default_false")]
28+
pub enum_param_hint: bool,
2629
}
2730

2831
impl Default for EmmyrcInlayHint {
@@ -34,10 +37,15 @@ impl Default for EmmyrcInlayHint {
3437
local_hint: default_true(),
3538
override_hint: default_true(),
3639
meta_call_hint: default_true(),
40+
enum_param_hint: default_false(),
3741
}
3842
}
3943
}
4044

4145
fn default_true() -> bool {
4246
true
4347
}
48+
49+
fn default_false() -> bool {
50+
false
51+
}

0 commit comments

Comments
 (0)