Skip to content

Commit 9bf87c1

Browse files
committed
support protected check for completion
1 parent 46037a5 commit 9bf87c1

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

crates/code_analysis/src/semantic/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use semantic_info::{
2323
infer_node_property_owner, infer_node_semantic_info, infer_token_property_owner,
2424
infer_token_semantic_info,
2525
};
26+
use type_compact::is_sub_type_of;
2627
use visibility::check_visibility;
2728

2829
use crate::LuaFunctionType;
@@ -154,6 +155,14 @@ impl<'a> SemanticModel<'a> {
154155
.unwrap_or(true)
155156
}
156157

158+
pub fn is_sub_type_of(
159+
&mut self,
160+
sub_type_ref_id: &LuaTypeDeclId,
161+
super_type_ref_id: &LuaTypeDeclId,
162+
) -> bool {
163+
is_sub_type_of(self.db, sub_type_ref_id, super_type_ref_id)
164+
}
165+
157166
pub fn get_emmyrc(&self) -> &Emmyrc {
158167
&self.emmyrc
159168
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod func_type;
2+
mod sub_type;
23

34
use func_type::infer_doc_func_type_compact;
45

@@ -8,6 +9,7 @@ use crate::db_index::{
89
};
910

1011
use super::{InferGuard, LuaInferConfig};
12+
pub use sub_type::is_sub_type_of;
1113

1214
#[allow(unused)]
1315
pub fn check_type_compact(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::{DbIndex, InferGuard, LuaType, LuaTypeDeclId};
2+
3+
pub fn is_sub_type_of(
4+
db: &DbIndex,
5+
sub_type_ref_id: &LuaTypeDeclId,
6+
super_type_ref_id: &LuaTypeDeclId,
7+
) -> bool {
8+
let mut infer_guard = InferGuard::new();
9+
check_sub_type_of(db, sub_type_ref_id, super_type_ref_id, &mut infer_guard).unwrap_or(false)
10+
}
11+
12+
fn check_sub_type_of(
13+
db: &DbIndex,
14+
sub_type_ref_id: &LuaTypeDeclId,
15+
super_type_ref_id: &LuaTypeDeclId,
16+
infer_guard: &mut InferGuard,
17+
) -> Option<bool> {
18+
infer_guard.check(super_type_ref_id)?;
19+
20+
let supers = db.get_type_index().get_super_types(sub_type_ref_id)?;
21+
for super_type in supers {
22+
if let LuaType::Ref(super_id) = super_type {
23+
if super_id == *super_type_ref_id {
24+
return Some(true);
25+
}
26+
27+
if check_sub_type_of(db, sub_type_ref_id, &super_id, infer_guard).unwrap_or(false) {
28+
return Some(true);
29+
}
30+
}
31+
}
32+
33+
Some(false)
34+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use emmylua_parser::{
55

66
use crate::{DbIndex, Emmyrc, FileId, LuaMemberOwner, LuaPropertyOwnerId, LuaType};
77

8-
use super::{infer_expr, LuaInferConfig};
8+
use super::{infer_expr, type_compact::is_sub_type_of, LuaInferConfig};
99

1010
pub fn check_visibility(
1111
db: &DbIndex,
@@ -94,7 +94,9 @@ fn check_block_visibility(
9494
return Some(true);
9595
}
9696

97-
// todo is subclass
97+
if is_sub_type_of(db, &left, &right) {
98+
return Some(true);
99+
}
98100
}
99101
_ => {}
100102
}

0 commit comments

Comments
 (0)