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: lsp completion of pkg, var and schema attr #614

Merged
merged 2 commits into from
Jul 25, 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
30 changes: 16 additions & 14 deletions kclvm/sema/src/lint/lints_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,22 @@ impl LintPass for UnusedImport {
let scope_objs = &scope.elems;
for (_, scope_obj) in scope_objs {
let scope_obj = scope_obj.borrow();
if scope_obj.kind == ScopeObjectKind::Module && scope_obj.used == false {
handler.add_warning(
WarningKind::UnusedImportWarning,
&[Message {
pos: Position {
filename: scope_obj.start.filename.clone(),
line: scope_obj.start.line,
column: None,
},
style: Style::Line,
message: format!("Module '{}' imported but unused", scope_obj.name),
note: Some("Consider removing this statement".to_string()),
}],
);
if let ScopeObjectKind::Module(_) = scope_obj.kind {
if !scope_obj.used {
handler.add_warning(
WarningKind::UnusedImportWarning,
&[Message {
pos: Position {
filename: scope_obj.start.filename.clone(),
line: scope_obj.start.line,
column: None,
},
style: Style::Line,
message: format!("Module '{}' imported but unused", scope_obj.name),
note: Some("Consider removing this statement".to_string()),
}],
);
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion kclvm/sema/src/resolver/import.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::plugin::PLUGIN_MODULE_PREFIX;
use crate::resolver::scope::Module;
use crate::resolver::Resolver;
use crate::ty::ModuleKind;
use crate::{
Expand Down Expand Up @@ -161,7 +162,12 @@ impl<'ctx> Resolver<'ctx> {
start,
end,
ty: Rc::new(ty),
kind: ScopeObjectKind::Module,
kind: ScopeObjectKind::Module(Module {
path: import_stmt.path.clone(),
rawpath: import_stmt.rawpath.clone(),
name: import_stmt.name.clone(),
asname: import_stmt.asname.clone(),
}),
used: false,
doc: None,
})),
Expand Down
46 changes: 45 additions & 1 deletion kclvm/sema/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ pub enum ScopeObjectKind {
Definition,
Parameter,
TypeAlias,
Module,
Module(Module),
}

/// A scope object of module type represents an import stmt on an AST and
/// is used to record information on the AST
#[derive(PartialEq, Clone, Debug)]
pub struct Module {
He1pa marked this conversation as resolved.
Show resolved Hide resolved
pub path: String,
pub rawpath: String,
pub name: String,
pub asname: Option<String>,
}

/// A Scope maintains a set of objects and links to its containing
Expand Down Expand Up @@ -96,6 +106,29 @@ impl Scope {
}
}

/// Get all usable scope objects in current and parent scope.
pub fn all_usable_objects(&self) -> IndexMap<String, Rc<RefCell<ScopeObject>>> {
let mut res = match &self.parent {
Some(parent) => match parent.upgrade() {
Some(parent) => parent.borrow().all_usable_objects(),
None => IndexMap::new(),
},
None => IndexMap::new(),
};

for (name, obj) in &self.elems {
match &obj.borrow().kind {
ScopeObjectKind::Module(module) => {
res.insert(module.name.clone(), obj.clone());
}
_ => {
res.insert(name.clone(), obj.clone());
}
}
}
res
}

/// Set a type by name to existed object, return true if found.
pub fn set_ty(&mut self, name: &str, ty: Rc<Type>) -> bool {
match self.elems.get_mut(name) {
Expand Down Expand Up @@ -267,6 +300,17 @@ impl ProgramScope {
};
emit_error().map_err(|e| e.to_string())
}

/// Returns the inner most scope on the position.
pub fn inner_most_scope(&self, pos: &Position) -> Option<Scope> {
He1pa marked this conversation as resolved.
Show resolved Hide resolved
for (_, scope) in &self.scope_map {
match scope.borrow().inner_most(&pos) {
Some(scope) => return Some(scope),
None => continue,
}
}
None
}
}

/// Construct a builtin scope
Expand Down
2 changes: 1 addition & 1 deletion kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn test_record_used_module() {
.clone();
for (_, obj) in main_scope.elems {
let obj = obj.borrow_mut().clone();
if obj.kind == ScopeObjectKind::Module {
if let ScopeObjectKind::Module(_) = obj.kind {
if obj.name == "math" {
assert!(!obj.used);
} else {
Expand Down
Loading
Loading