Skip to content

Commit

Permalink
Feat(LSP): load pkg file in LSP. If the input file of lsp is not in a…
Browse files Browse the repository at this point in the history
… stack environment, load all kcl files in the dir where the file is located as input
  • Loading branch information
He1pa committed Apr 6, 2023
1 parent fca0342 commit 04adc91
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 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/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ kclvm-runtime ={ path = "../runtime"}
kclvm-utils ={ path = "../utils"}
kclvm-parser ={ path = "../parser"}
kclvm-ast ={ path = "../ast"}
walkdir = "2"
anyhow = { version = "1.0.70", features = ["backtrace"] }
40 changes: 37 additions & 3 deletions kclvm/driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use kclvm_ast::ast;
use kclvm_config::{
modfile::KCL_MOD_PATH_ENV,
modfile::{KCL_FILE_EXTENSION, KCL_FILE_SUFFIX, KCL_MOD_PATH_ENV},
settings::{build_settings_pathbuf, DEFAULT_SETTING_FILE},
};
use kclvm_parser::LoadProgramOptions;
Expand All @@ -12,6 +13,7 @@ use std::{
io::{self, ErrorKind},
path::{Path, PathBuf},
};
use walkdir::WalkDir;

/// Normalize input files with the working directory and replace ${KCL_MOD} with the module root path.
pub fn canonicalize_input_files(
Expand Down Expand Up @@ -58,7 +60,10 @@ pub fn canonicalize_input_files(
}

/// Get compile uint(files and options) from a single file
pub fn lookup_compile_unit(file: &str) -> (Vec<String>, Option<LoadProgramOptions>) {
pub fn lookup_compile_unit(
file: &str,
load_pkg: bool,
) -> (Vec<String>, Option<LoadProgramOptions>) {
match lookup_compile_unit_path(file) {
Ok(dir) => {
let settings_files = lookup_setting_files(&dir);
Expand Down Expand Up @@ -111,7 +116,21 @@ pub fn lookup_compile_unit(file: &str) -> (Vec<String>, Option<LoadProgramOption
Err(_) => return (vec![file.to_string()], None),
}
}
Err(_) => return (vec![file.to_string()], None),
Err(_) => {
if load_pkg {
let path = Path::new(file);
if let Some(ext) = path.extension() {
if ext == KCL_FILE_EXTENSION && path.is_file() {
if let Some(parent) = path.parent() {
if let Ok(files) = get_kcl_files(parent, false) {
return (files, None);
}
}
}
}
}
return (vec![file.to_string()], None);
}
}
}

Expand Down Expand Up @@ -152,3 +171,18 @@ pub fn lookup_compile_unit_path(file: &str) -> io::Result<PathBuf> {
"Ran out of places to find kcl.yaml",
))
}

/// Get kcl files from path.
pub fn get_kcl_files<P: AsRef<Path>>(path: P, recursively: bool) -> Result<Vec<String>> {
let mut files = vec![];
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() {
let file = path.to_str().unwrap();
if file.ends_with(KCL_FILE_SUFFIX) && (recursively || entry.depth() == 1) {
files.push(file.to_string())
}
}
}
Ok(files)
}
5 changes: 4 additions & 1 deletion kclvm/tools/src/LSP/src/test_data/diagnostics.k
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
a =
b: str = 1
b: str = 1
c: Person = Person {
age: 1
}
2 changes: 2 additions & 0 deletions kclvm/tools/src/LSP/src/test_data/load_pkg_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema Person:
age: int
3 changes: 1 addition & 2 deletions kclvm/tools/src/LSP/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn parse_param_and_compile(
param: Param,
vfs: Option<Arc<RwLock<Vfs>>>,
) -> anyhow::Result<(Program, ProgramScope, IndexSet<Diagnostic>)> {
let (files, opt) = lookup_compile_unit(&param.file);
let (files, opt) = lookup_compile_unit(&param.file, true);
let files: Vec<&str> = files.iter().map(|s| s.as_str()).collect();
let mut opt = opt.unwrap_or_default();

Expand All @@ -60,7 +60,6 @@ pub(crate) fn parse_param_and_compile(
let mut k_code_list = load_files_code_from_vfs(&files, vfs)?;
opt.k_code_list.append(&mut k_code_list);
}

let sess = Arc::new(ParseSession::default());
let mut program = load_program(sess.clone(), &files, Some(opt)).unwrap();
let prog_scope = resolve_program(&mut program);
Expand Down

0 comments on commit 04adc91

Please sign in to comment.