Skip to content

Commit

Permalink
feat: add check scope kind (#1057)
Browse files Browse the repository at this point in the history
* docs: polish readme documents (#1046)

Signed-off-by: peefy <[email protected]>
Signed-off-by: he1pa <[email protected]>

* feat: add check scope kind in schema stmt

Signed-off-by: he1pa <[email protected]>

---------

Signed-off-by: peefy <[email protected]>
Signed-off-by: he1pa <[email protected]>
Co-authored-by: Peefy <[email protected]>
  • Loading branch information
He1pa and Peefy authored Feb 20, 2024
1 parent c047aa0 commit 229cb55
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kclvm/loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 @@ -236,6 +237,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:

0 comments on commit 229cb55

Please sign in to comment.