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

Refactor: refactor find def in LSP #606

Merged
merged 5 commits into from
Jul 18, 2023
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
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ impl Into<(Position, Position)> for Pos {

/// Node is the file, line and column number information
/// that all AST nodes need to contain.
/// In fact, column and end_column are the counts of character,
/// For example, `\t` is counted as 1 character, so it is recorded as 1 here, but generally col is 4.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Node<T> {
pub node: T,
Expand Down
7 changes: 2 additions & 5 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 All @@ -222,9 +222,6 @@ impl<'ctx> Resolver<'ctx> {
self.ctx.pkgpath = pkgpath.to_string();
self.ctx.filename = filename.to_string();
let scope = self.scope_map.get(pkgpath).unwrap().clone();
if let ScopeKind::Package(files) = &mut scope.borrow_mut().kind {
files.push(filename.to_string())
}
self.scope = scope;
}
}
3 changes: 3 additions & 0 deletions kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ impl<'ctx> Resolver<'ctx> {
Some(modules) => {
for module in modules {
self.ctx.filename = module.filename.to_string();
if let scope::ScopeKind::Package(files) = &mut self.scope.borrow_mut().kind {
He1pa marked this conversation as resolved.
Show resolved Hide resolved
files.insert(module.filename.to_string());
}
for stmt in &module.body {
self.stmt(&stmt);
}
Expand Down
16 changes: 3 additions & 13 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 @@ -113,17 +113,7 @@ impl ContainsPos for Scope {
/// Check if current scope contains a position
fn contains_pos(&self, pos: &Position) -> bool {
match &self.kind {
ScopeKind::Package(files) => {
if files.contains(&pos.filename) {
self.children.iter().any(|s| s.borrow().contains_pos(pos))
|| self
.elems
.iter()
.any(|(_, child)| child.borrow().contains_pos(pos))
} else {
false
}
}
ScopeKind::Package(files) => files.contains(&pos.filename),
_ => self.start.less_equal(pos) && pos.less_equal(&self.end),
}
}
Expand All @@ -132,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));
}
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kclvm-parser = {path = "../../../parser"}
kclvm-sema = {path = "../../../sema"}
kclvm-ast = {path = "../../../ast"}
kclvm-utils = {path = "../../../utils"}
kclvm-compiler = {path = "../../../compiler"}
compiler_base_session = {path = "../../../../compiler_base/session"}

lsp-server = { version = "0.6.0", default-features = false }
Expand Down
65 changes: 41 additions & 24 deletions kclvm/tools/src/LSP/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{fs, path::Path};

use indexmap::IndexSet;
use kclvm_ast::ast::{Expr, ImportStmt, Program, Stmt};
use kclvm_compiler::pkgpath_without_prefix;
use kclvm_config::modfile::KCL_FILE_EXTENSION;

use kclvm_error::Position as KCLPos;
Expand All @@ -22,8 +23,9 @@ use kclvm_sema::builtin::{
use kclvm_sema::resolver::scope::ProgramScope;
use lsp_types::CompletionItem;

use crate::goto_def::get_identifier_last_name;
use crate::{goto_def::find_objs_in_program_scope, util::inner_most_expr_in_stmt};
use crate::goto_def::{get_identifier_last_name, resolve_var};
use crate::util::inner_most_expr_in_stmt;
use crate::util::{fix_missing_identifier, get_pkg_scope};

/// Computes completions at the given position.
pub(crate) fn completion(
Expand Down Expand Up @@ -115,7 +117,7 @@ fn get_completion_items(expr: &Expr, prog_scope: &ProgramScope) -> IndexSet<Stri
let mut items = IndexSet::new();
match expr {
Expr::Identifier(id) => {
let name = get_identifier_last_name(id);
let name = get_identifier_last_name(&id);
if !id.pkgpath.is_empty() {
// standard system module
if STANDARD_SYSTEM_MODULES.contains(&name.as_str()) {
Expand All @@ -126,7 +128,10 @@ fn get_completion_items(expr: &Expr, prog_scope: &ProgramScope) -> IndexSet<Stri
)
}
// user module
if let Some(scope) = prog_scope.scope_map.get(&id.pkgpath) {
if let Some(scope) = prog_scope
.scope_map
.get(&pkgpath_without_prefix!(id.pkgpath))
{
let scope = scope.borrow();
for (name, obj) in &scope.elems {
if obj.borrow().ty.is_module() {
Expand All @@ -138,38 +143,50 @@ fn get_completion_items(expr: &Expr, prog_scope: &ProgramScope) -> IndexSet<Stri
return items;
}

let objs = find_objs_in_program_scope(&name, prog_scope);
for obj in objs {
match &obj.ty.kind {
// builtin (str) functions
kclvm_sema::ty::TypeKind::Str => {
let binding = STRING_MEMBER_FUNCTIONS;
for k in binding.keys() {
items.insert(format!("{}{}", k, "()"));
}
}
// schema attrs
kclvm_sema::ty::TypeKind::Schema(schema) => {
for k in schema.attrs.keys() {
if k != "__settings__" {
items.insert(k.clone());
let def = resolve_var(
&fix_missing_identifier(&id.names),
He1pa marked this conversation as resolved.
Show resolved Hide resolved
&get_pkg_scope(&id.pkgpath, &prog_scope.scope_map),
&prog_scope.scope_map,
);

if let Some(def) = def {
match def {
crate::goto_def::Definition::Object(obj) => {
match &obj.ty.kind {
// builtin (str) functions
kclvm_sema::ty::TypeKind::Str => {
let binding = STRING_MEMBER_FUNCTIONS;
for k in binding.keys() {
items.insert(format!("{}{}", k, "()"));
}
}
// schema attrs
kclvm_sema::ty::TypeKind::Schema(schema) => {
for k in schema.attrs.keys() {
if k != "__settings__" {
items.insert(k.clone());
}
}
}
_ => {}
}
}
_ => {}
crate::goto_def::Definition::Scope(_) => {
// todo
}
}
}
}
Expr::Selector(select_expr) => {
let res = get_completion_items(&select_expr.value.node, prog_scope);
items.extend(res);
}
Expr::StringLit(_) => {
let binding = STRING_MEMBER_FUNCTIONS;
for k in binding.keys() {
items.insert(format!("{}{}", k, "()"));
}
}
Expr::Selector(select_expr) => {
let res = get_completion_items(&select_expr.value.node, prog_scope);
items.extend(res);
}
_ => {}
}
items
Expand Down
Loading