Skip to content

Commit 3a452c6

Browse files
committed
fix version check
1 parent c674135 commit 3a452c6

File tree

11 files changed

+77
-89
lines changed

11 files changed

+77
-89
lines changed

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use emmylua_parser::{
2-
LuaAstNode, LuaAstToken, LuaComment, LuaDocAttribute, LuaDocTag, LuaDocTagAlias, LuaDocTagClass, LuaDocTagEnum, LuaDocTagMeta, LuaDocTagNamespace, LuaDocTagUsing
2+
LuaAstNode, LuaAstToken, LuaComment, LuaDocAttribute, LuaDocTag, LuaDocTagAlias,
3+
LuaDocTagClass, LuaDocTagEnum, LuaDocTagMeta, LuaDocTagNamespace, LuaDocTagUsing,
34
};
45
use flagset::FlagSet;
56

@@ -36,9 +37,7 @@ pub fn analyze_doc_tag_class(analyzer: &mut DeclAnalyzer, class: LuaDocTagClass)
3637
Some(())
3738
}
3839

39-
fn get_attrib_value(
40-
attrib: Option<LuaDocAttribute>,
41-
) -> Option<FlagSet<LuaTypeAttribute>> {
40+
fn get_attrib_value(attrib: Option<LuaDocAttribute>) -> Option<FlagSet<LuaTypeAttribute>> {
4241
let mut attr: FlagSet<LuaTypeAttribute> = LuaTypeAttribute::None.into();
4342

4443
for token in attrib?.get_attrib_tokens() {
@@ -165,22 +164,16 @@ pub fn analyze_doc_tag_meta(analyzer: &mut DeclAnalyzer, tag: LuaDocTagMeta) ->
165164
}
166165
})?;
167166

168-
let mut visible = false;
169-
let current_version = analyzer.emmyrc.runtime.version.to_lua_version_number();
167+
let mut version_conds = Vec::new();
170168
for doc_version in version_tag.get_version_list() {
171169
let version_condition = doc_version.get_version_condition()?;
172-
if version_condition.check(&current_version) {
173-
visible = true;
174-
break;
175-
}
170+
version_conds.push(version_condition);
176171
}
177172

178-
if !visible {
179-
analyzer
180-
.db
181-
.get_module_index_mut()
182-
.set_module_visibility(file_id, false);
183-
}
173+
analyzer
174+
.db
175+
.get_module_index_mut()
176+
.set_module_version_conds(file_id, version_conds);
184177

185178
Some(())
186179
}

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

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1+
use super::{tags::get_owner_id, DocAnalyzer};
12
use emmylua_parser::{
2-
BinaryOperator, LuaDocDescriptionOwner, LuaDocTagDeprecated, LuaDocTagSource, LuaDocTagVersion,
3+
LuaDocDescriptionOwner, LuaDocTagDeprecated, LuaDocTagSource, LuaDocTagVersion,
34
LuaDocTagVisibility,
45
};
56

6-
use crate::db_index::{LuaVersionCond, LuaVersionCondOp};
7-
8-
use super::{tags::get_owner_id, DocAnalyzer};
9-
107
pub fn analyze_visibility(
118
analyzer: &mut DocAnalyzer,
129
visibility: LuaDocTagVisibility,
1310
) -> Option<()> {
1411
let visibility_kind = visibility.get_visibility_token()?.get_visibility();
1512
let owner_id = get_owner_id(analyzer)?;
1613

17-
analyzer
18-
.db
19-
.get_property_index_mut()
20-
.add_visibility(analyzer.file_id, owner_id, visibility_kind);
14+
analyzer.db.get_property_index_mut().add_visibility(
15+
analyzer.file_id,
16+
owner_id,
17+
visibility_kind,
18+
);
2119

2220
Some(())
2321
}
@@ -66,24 +64,8 @@ pub fn analyze_version(analyzer: &mut DocAnalyzer, version: LuaDocTagVersion) ->
6664

6765
let mut version_set = Vec::new();
6866
for version in version.get_version_list() {
69-
let version_number = if let Some(version_number) = version.get_version() {
70-
version_number.get_version_number()
71-
} else {
72-
continue;
73-
};
74-
75-
let version_op = if let Some(version_op) = version.get_op() {
76-
match version_op.get_op() {
77-
BinaryOperator::OpGt => LuaVersionCondOp::Gt,
78-
BinaryOperator::OpLt => LuaVersionCondOp::Lt,
79-
_ => LuaVersionCondOp::Eq,
80-
}
81-
} else {
82-
LuaVersionCondOp::Eq
83-
};
84-
85-
if let Some(version_number) = version_number {
86-
version_set.push(LuaVersionCond::new(version_number, version_op));
67+
if let Some(version_condition) = version.get_version_condition() {
68+
version_set.push(version_condition);
8769
}
8870
}
8971

@@ -104,4 +86,4 @@ pub fn analyze_async(analyzer: &mut DocAnalyzer) -> Option<()> {
10486
.add_async(analyzer.file_id, owner_id);
10587

10688
Some(())
107-
}
89+
}

crates/code_analysis/src/db_index/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ use meta::MetaFile;
2222
use module::LuaModuleIndex;
2323
pub use module::ModuleInfo;
2424
pub use operators::{LuaOperator, LuaOperatorId, LuaOperatorIndex, LuaOperatorMetaMethod};
25-
pub use property::{
26-
LuaPropertyId, LuaPropertyIndex, LuaPropertyOwnerId, LuaVersionCond, LuaVersionCondOp,
27-
};
25+
pub use property::{LuaPropertyId, LuaPropertyIndex, LuaPropertyOwnerId};
2826
pub use r#type::*;
2927
pub use reference::LuaReferenceIndex;
3028
pub use signature::*;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod module_info;
22
mod module_node;
33
mod test;
44

5+
use emmylua_parser::LuaVersionCondition;
56
use log::{error, info};
67
pub use module_info::ModuleInfo;
78
use module_node::{ModuleNode, ModuleNodeId};
@@ -129,6 +130,7 @@ impl LuaModuleIndex {
129130
module_id: parent_node_id,
130131
visible: true,
131132
export_type: None,
133+
version_conds: None,
132134
};
133135

134136
self.file_module_map.insert(file_id, module_info);
@@ -150,6 +152,16 @@ impl LuaModuleIndex {
150152
}
151153
}
152154

155+
pub fn set_module_version_conds(
156+
&mut self,
157+
file_id: FileId,
158+
version_conds: Vec<LuaVersionCondition>,
159+
) {
160+
if let Some(module_info) = self.file_module_map.get_mut(&file_id) {
161+
module_info.version_conds = Some(Box::new(version_conds));
162+
}
163+
}
164+
153165
pub fn find_module(&self, module_path: &str) -> Option<&ModuleInfo> {
154166
let module_path = module_path.replace(['\\', '/'], ".");
155167
let module_parts: Vec<&str> = module_path.split('.').collect();

crates/code_analysis/src/db_index/module/module_info.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use emmylua_parser::{LuaVersionCondition, LuaVersionNumber};
2+
13
use crate::{db_index::LuaType, FileId};
24

35
use super::module_node::ModuleNodeId;
@@ -10,4 +12,25 @@ pub struct ModuleInfo {
1012
pub module_id: ModuleNodeId,
1113
pub visible: bool,
1214
pub export_type: Option<LuaType>,
13-
}
15+
pub version_conds: Option<Box<Vec<LuaVersionCondition>>>,
16+
}
17+
18+
impl ModuleInfo {
19+
pub fn is_visible(&self, version_number: &LuaVersionNumber) -> bool {
20+
if !self.visible {
21+
return false;
22+
}
23+
24+
if let Some(version_conds) = &self.version_conds {
25+
for cond in version_conds.iter() {
26+
if cond.check(version_number) {
27+
return true;
28+
}
29+
}
30+
31+
return false;
32+
}
33+
34+
true
35+
}
36+
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
mod property;
2-
mod version;
3-
42
use std::collections::{HashMap, HashSet};
53

6-
use emmylua_parser::VisibilityKind;
4+
use emmylua_parser::{LuaVersionCondition, VisibilityKind};
75
use property::LuaProperty;
86
pub use property::{LuaPropertyId, LuaPropertyOwnerId};
9-
pub use version::{LuaVersionCond, LuaVersionCondOp};
107

118
use crate::FileId;
129

@@ -134,7 +131,7 @@ impl LuaPropertyIndex {
134131
&mut self,
135132
file_id: FileId,
136133
owner_id: LuaPropertyOwnerId,
137-
version_conds: Vec<LuaVersionCond>,
134+
version_conds: Vec<LuaVersionCondition>,
138135
) {
139136
let property = self.get_or_create_property(owner_id.clone());
140137
property.version_conds = Some(Box::new(version_conds));

crates/code_analysis/src/db_index/property/property.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use emmylua_parser::VisibilityKind;
1+
use emmylua_parser::{LuaVersionCondition, VisibilityKind};
22
use serde::{Deserialize, Serialize};
33

44
use crate::db_index::{member::LuaMemberId, LuaDeclId, LuaSignatureId, LuaTypeDeclId};
55

6-
use super::version::LuaVersionCond;
7-
86
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
97
pub struct LuaProperty {
108
pub id: LuaPropertyId,
@@ -14,7 +12,7 @@ pub struct LuaProperty {
1412
pub is_nodiscard: bool,
1513
pub is_deprecated: bool,
1614
pub deprecated_message: Option<Box<String>>,
17-
pub version_conds: Option<Box<Vec<LuaVersionCond>>>,
15+
pub version_conds: Option<Box<Vec<LuaVersionCondition>>>,
1816
pub is_async: bool,
1917
}
2018

crates/code_analysis/src/db_index/property/version.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

crates/emmylua_ls/src/handlers/completion/add_completions/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use super::completion_builder::CompletionBuilder;
1515
fn check_visibility(builder: &CompletionBuilder, id: LuaPropertyOwnerId) -> Option<()> {
1616
match id {
1717
LuaPropertyOwnerId::Member(_) => {}
18+
LuaPropertyOwnerId::LuaDecl(_) => {}
1819
_ => return Some(()),
1920
}
2021

@@ -26,10 +27,16 @@ fn check_visibility(builder: &CompletionBuilder, id: LuaPropertyOwnerId) -> Opti
2627
if property.is_none() {
2728
return Some(());
2829
}
29-
// let decl = property.unwrap();
30-
// if let Some(visib) = &decl.visibility {
31-
// }
32-
// todo check
30+
31+
let property = property.unwrap();
32+
if let Some(version_conds) = &property.version_conds {
33+
let emmyrc = builder.semantic_model.get_emmyrc();
34+
let version_number = emmyrc.runtime.version.to_lua_version_number();
35+
let visiable = version_conds.iter().any(|cond| cond.check(&version_number));
36+
if !visiable {
37+
return None;
38+
}
39+
}
3340

3441
Some(())
3542
}

crates/emmylua_ls/src/handlers/completion/providers/auto_require_provider.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ pub fn add_completion(builder: &mut CompletionBuilder) -> Option<()> {
2020
let name_expr = LuaNameExpr::cast(builder.trigger_token.parent()?)?;
2121
// optimize for large project
2222
let prefix = name_expr.get_name_text()?.to_lowercase();
23-
let file_convension = builder
24-
.semantic_model
25-
.get_emmyrc()
23+
let emmyrc = builder.semantic_model.get_emmyrc();
24+
let file_convension = emmyrc
2625
.completion
2726
.auto_require_naming_convention;
27+
let version_number = emmyrc.runtime.version.to_lua_version_number();
2828
let file_id = builder.semantic_model.get_file_id();
2929
let module_infos = builder
3030
.semantic_model
@@ -37,7 +37,7 @@ pub fn add_completion(builder: &mut CompletionBuilder) -> Option<()> {
3737

3838
let mut completions = Vec::new();
3939
for module_info in module_infos {
40-
if module_info.visible
40+
if module_info.is_visible(&version_number)
4141
&& module_info.file_id != file_id
4242
&& module_info.export_type.is_some()
4343
{

0 commit comments

Comments
 (0)