Skip to content

Commit

Permalink
feat: lsp completion of pkg, var and schema attr
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Jul 25, 2023
1 parent 2f6abbd commit 269986a
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 125 deletions.
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 {
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> {
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

0 comments on commit 269986a

Please sign in to comment.