Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add check scope kind #1057

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kclvm/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum ScopeKind {
SchemaDef,
SchemaConfig,
Value,
Check,
}

/// load_package provides users with the ability to parse kcl program and sematic model
Expand Down Expand Up @@ -208,6 +209,7 @@ impl From<LocalSymbolScopeKind> for ScopeKind {
LocalSymbolScopeKind::SchemaDef => ScopeKind::SchemaDef,
LocalSymbolScopeKind::SchemaConfig => ScopeKind::SchemaConfig,
LocalSymbolScopeKind::Value => ScopeKind::Value,
LocalSymbolScopeKind::Check => ScopeKind::Check,
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion kclvm/sema/src/advanced_resolver/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,12 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
};
}

let mut last_end_pos = start.clone();

self.enter_local_scope(
&self.ctx.current_filename.clone().unwrap(),
start,
end,
end.clone(),
LocalSymbolScopeKind::SchemaDef,
);
let cur_scope = *self.ctx.scopes.last().unwrap();
Expand Down Expand Up @@ -247,6 +249,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
if let Some(mixin) = self.walk_identifier_expr(mixin) {
mixins.push(mixin);
}
last_end_pos = mixin.get_end_pos();
}
self.gs
.get_symbols_mut()
Expand All @@ -257,6 +260,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {

if let Some(args) = &schema_stmt.args {
self.walk_arguments(&args.node);
last_end_pos = args.get_end_pos();
}
if let Some(index_signature) = &schema_stmt.index_signature {
if let Some(key_name) = &index_signature.node.key_name {
Expand Down Expand Up @@ -285,6 +289,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
self.expr(value);
};
}
last_end_pos = index_signature.get_end_pos();
}
for stmt in schema_stmt.body.iter() {
if let Some(attribute_symbol) = self.stmt(&stmt) {
Expand All @@ -302,11 +307,27 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> {
.attributes
.insert(name, attribute_symbol);
}
last_end_pos = stmt.get_end_pos();
}

let has_check = !schema_stmt.checks.is_empty();
if has_check {
self.enter_local_scope(
&self.ctx.current_filename.clone().unwrap(),
last_end_pos,
end,
LocalSymbolScopeKind::Check,
);
}

for check_expr in schema_stmt.checks.iter() {
self.walk_check_expr(&check_expr.node);
}

if has_check {
self.leave_scope();
}

self.leave_scope();

Some(schema_symbol)
Expand Down
1 change: 1 addition & 0 deletions kclvm/sema/src/core/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ pub enum LocalSymbolScopeKind {
SchemaDef,
SchemaConfig,
Value,
Check,
}

impl Scope for LocalSymbolScope {
Expand Down
32 changes: 32 additions & 0 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,4 +1513,36 @@ mod tests {
CompletionResponse::List(_) => panic!("test failed"),
}
}

#[test]
#[bench_test]
fn check_scope_completion() {
let (file, program, _, _, gs) =
compile_test_file("src/test_data/completion_test/check/check.k");

let pos = KCLPos {
filename: file.to_owned(),
line: 4,
column: Some(10),
};

let got = completion(Some(':'), &program, &pos, &gs);
assert!(got.is_none());

let pos = KCLPos {
filename: file.to_owned(),
line: 5,
column: Some(9),
};

let got = completion(None, &program, &pos, &gs).unwrap();
match got {
CompletionResponse::Array(arr) => {
assert_eq!(arr.len(), 3);
let labels: Vec<String> = arr.iter().map(|item| item.label.clone()).collect();
assert!(labels.contains(&"name".to_string()));
}
CompletionResponse::List(_) => panic!("test failed"),
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
schema Person:
name: str

check:

Loading