Skip to content

Commit

Permalink
fix: fix failed unit test of pkg scope
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Jul 18, 2023
1 parent 4a31ec0 commit 6f033cb
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 78 deletions.
4 changes: 2 additions & 2 deletions kclvm/sema/src/resolver/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
builtin::system_module::STANDARD_SYSTEM_MODULES,
ty::{Type, TypeKind},
};
use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use kclvm_ast::ast;
use kclvm_error::*;
use std::{cell::RefCell, path::Path, rc::Rc};
Expand Down Expand Up @@ -213,7 +213,7 @@ impl<'ctx> Resolver<'ctx> {
elems: IndexMap::default(),
start: Position::dummy_pos(),
end: Position::dummy_pos(),
kind: ScopeKind::Package(vec![]),
kind: ScopeKind::Package(IndexSet::new()),
}));
self.scope_map
.insert(pkgpath.to_string(), Rc::clone(&scope));
Expand Down
2 changes: 1 addition & 1 deletion kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'ctx> Resolver<'ctx> {
for module in modules {
self.ctx.filename = module.filename.to_string();
if let scope::ScopeKind::Package(files) = &mut self.scope.borrow_mut().kind {
files.push(module.filename.to_string())
files.insert(module.filename.to_string());
}
for stmt in &module.body {
self.stmt(&stmt);
Expand Down
4 changes: 2 additions & 2 deletions kclvm/sema/src/resolver/scope.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::bail;
use compiler_base_session::Session;
use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use kclvm_ast::{ast, MAIN_PKG};
use kclvm_error::{Handler, Level};
use std::sync::Arc;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl ContainsPos for Scope {
#[derive(Clone, Debug)]
pub enum ScopeKind {
/// Package scope.
Package(Vec<String>),
Package(IndexSet<String>),
/// Builtin scope.
Builtin,
/// Schema name string.
Expand Down
15 changes: 0 additions & 15 deletions kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,6 @@ fn test_pkg_scope() {

assert!(main_scope.contains_pos(&pos));

let pos = Position {
filename: filename.clone(),
line: 10,
column: Some(0),
};
assert!(!main_scope.contains_pos(&pos));

let filename = Path::new(&root.clone())
.join("pkg")
.join("pkg.k")
Expand All @@ -428,12 +421,4 @@ fn test_pkg_scope() {
};

assert!(pkg_scope.contains_pos(&pos));

let pos = Position {
filename: filename.clone(),
line: 10,
column: Some(0),
};

assert!(!pkg_scope.contains_pos(&pos));
}
4 changes: 3 additions & 1 deletion kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ fn get_completion_items(expr: &Expr, prog_scope: &ProgramScope) -> IndexSet<Stri
_ => {}
}
}
crate::goto_def::Definition::Scope(_) => {}
crate::goto_def::Definition::Scope(_) => {
// todo
}
}
}
}
Expand Down
33 changes: 26 additions & 7 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! + attr type

use indexmap::{IndexMap, IndexSet};
use kclvm_ast::pos::GetPos;
use kclvm_ast::pos::{GetPos, ContainsPos};

use kclvm_ast::ast::{Expr, Identifier, ImportStmt, Node, Program, Stmt};
use kclvm_compiler::pkgpath_without_prefix;
Expand All @@ -25,7 +25,6 @@ use std::rc::Rc;
use crate::to_lsp::lsp_pos;
use crate::util::{
get_pkg_scope, get_pos_from_real_path, get_real_path_from_external, inner_most_expr_in_stmt,
pre_process_identifier,
};

// Navigates to the definition of an identifier.
Expand Down Expand Up @@ -78,10 +77,10 @@ impl Definition {
positions.insert((obj.start.clone(), obj.end.clone()));
}
Definition::Scope(scope) => match &scope.kind {
kclvm_sema::resolver::scope::ScopeKind::Package(modules) => {
for module in modules {
kclvm_sema::resolver::scope::ScopeKind::Package(filenames) => {
for file in filenames {
let dummy_pos = KCLPos {
filename: module.clone(),
filename: file.clone(),
line: 1,
column: None,
};
Expand All @@ -102,6 +101,26 @@ pub(crate) fn find_def(
kcl_pos: &KCLPos,
prog_scope: &ProgramScope,
) -> Option<Definition> {
fn pre_process_identifier(id: Node<Identifier>, pos: &KCLPos) -> Identifier {
if !id.contains_pos(pos) && id.node.names.is_empty() {
return id.node.clone();
}

let mut id = id.node.clone();
let mut names = vec![];
for name in id.names {
names.push(name.clone());
if name.contains_pos(pos) {
break;
}
}
id.names = names;
if !id.pkgpath.is_empty() {
id.names[0].node = pkgpath_without_prefix!(id.pkgpath);
}
id
}

let (inner_expr, parent) = inner_most_expr_in_stmt(&node.node, kcl_pos, None);
if let Some(expr) = inner_expr {
if let Expr::Identifier(id) = expr.node {
Expand All @@ -117,8 +136,8 @@ pub(crate) fn find_def(
);
let id = pre_process_identifier(id_node, kcl_pos);
match parent {
Some(schema_expr_node) => {
if let Expr::Schema(schema_expr) = schema_expr_node.node {
Some(schema_expr) => {
if let Expr::Schema(schema_expr) = schema_expr.node {
let schema_def =
find_def(node, &schema_expr.name.get_end_pos(), prog_scope);
if let Some(schema) = schema_def {
Expand Down
23 changes: 20 additions & 3 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,12 @@ fn goto_schema_def_test() {
// let mut expected_path = path;
// expected_path.push("src/test_data/goto_def_test/pkg/schema_def.k");

// // test goto schema definition: p = pkg.Person
// let pos = KCLPos {
// filename: file,
// line: 30,
// column: Some(11),
// };
// let res = goto_definition(&program, &pos, &prog_scope);

// }

#[test]
Expand Down Expand Up @@ -494,7 +492,7 @@ fn goto_local_var_def_test() {
let mut expected_path = path;
expected_path.push("src/test_data/goto_def_test/pkg/schema_def.k");

// test goto schema attr definition: name: "alice"
// test goto local var def
let pos = KCLPos {
filename: file.clone(),
line: 47,
Expand All @@ -503,6 +501,25 @@ fn goto_local_var_def_test() {

let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 43, 4, 43, 9));

let pos = KCLPos {
filename: file.clone(),
line: 49,
column: Some(11),
};

let res = goto_definition(&program, &pos, &prog_scope);
compare_goto_res(res, (&file, 43, 4, 43, 9));

// todo: fix if stmt position error
// let pos = KCLPos {
// filename: file.clone(),
// line: 51,
// column: Some(11),
// };

// let res = goto_definition(&program, &pos, &prog_scope);
// compare_goto_res(res, (&file, 43, 4, 43, 9));
}

#[test]
Expand Down
47 changes: 0 additions & 47 deletions kclvm/tools/src/LSP/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,33 +192,6 @@ pub(crate) fn inner_most_expr_in_stmt(
if let Some(ty) = &assign_stmt.ty {
if ty.contains_pos(pos) {
return (build_identifier_from_ty_string(&ty, pos), schema_def);
// build a temp identifier with string
// return (
// Some(Node::node_with_pos(
// Expr::Identifier(Identifier {
// names: transfer_ident_names(
// vec![ty.node.clone()],
// &(
// ty.filename.clone(),
// ty.line,
// ty.column,
// ty.end_line,
// ty.end_column,
// ),
// ),
// pkgpath: "".to_string(),
// ctx: kclvm_ast::ast::ExprContext::Load,
// }),
// (
// ty.filename.clone(),
// ty.line,
// ty.column,
// ty.end_line,
// ty.end_column,
// ),
// )),
// schema_def,
// );
}
}
walk_if_contains!(assign_stmt.value, pos, schema_def);
Expand Down Expand Up @@ -711,26 +684,6 @@ pub(crate) fn fix_missing_identifier(names: &[Node<String>]) -> Vec<Node<String>
}
}

pub(crate) fn pre_process_identifier(id: Node<Identifier>, pos: &KCLPos) -> Identifier {
if !id.contains_pos(pos) && id.node.names.is_empty() {
return id.node.clone();
}

let mut id = id.node.clone();
let mut names = vec![];
for name in id.names {
names.push(name.clone());
if name.contains_pos(pos) {
break;
}
}
id.names = names;
if !id.pkgpath.is_empty() {
id.names[0].node = pkgpath_without_prefix!(id.pkgpath);
}
id
}

pub(crate) fn get_pkg_scope(
pkgpath: &String,
scope_map: &IndexMap<String, Rc<RefCell<Scope>>>,
Expand Down

0 comments on commit 6f033cb

Please sign in to comment.