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: add loader API with state #1202

Merged
merged 1 commit into from
Apr 8, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ __kcl_test_main.k
/kcl-go*
/_build_dist
.kusion
._target

/venv/

Expand Down
43 changes: 31 additions & 12 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use kcl_language_server::rename;
use kclvm_config::settings::build_settings_pathbuf;
use kclvm_driver::canonicalize_input_files;
use kclvm_loader::option::list_options;
use kclvm_loader::{load_packages, LoadPackageOptions};
use kclvm_loader::{load_packages_with_cache, LoadPackageOptions};
use kclvm_parser::load_program;
use kclvm_parser::parse_file;
use kclvm_parser::KCLModuleCache;
Expand All @@ -22,6 +22,8 @@ use kclvm_query::query::get_full_schema_type;
use kclvm_query::query::CompilationOptions;
use kclvm_query::GetSchemaOption;
use kclvm_runner::{build_program, exec_artifact, exec_program};
use kclvm_sema::core::global_state::GlobalState;
use kclvm_sema::resolver::scope::KCLScopeCache;
use kclvm_sema::resolver::Options;
use kclvm_tools::format::{format, format_source, FormatOptions};
use kclvm_tools::lint::lint_files;
Expand Down Expand Up @@ -178,23 +180,40 @@ impl KclvmServiceImpl {
/// assert_eq!(result.fully_qualified_name_map.len(), 178);
/// assert_eq!(result.pkg_scope_map.len(), 3);
/// ```
#[inline]
pub fn load_package(&self, args: &LoadPackageArgs) -> anyhow::Result<LoadPackageResult> {
self.load_package_with_cache(args, KCLModuleCache::default(), KCLScopeCache::default())
}

/// load_package_with_cache provides users with the ability to parse kcl program and sematic model
/// information including symbols, types, definitions, etc.
pub fn load_package_with_cache(
&self,
args: &LoadPackageArgs,
module_cache: KCLModuleCache,
scope_cache: KCLScopeCache,
) -> anyhow::Result<LoadPackageResult> {
let mut package_maps = HashMap::new();
let parse_args = args.parse_args.clone().unwrap_or_default();
for p in &parse_args.external_pkgs {
package_maps.insert(p.pkg_name.to_string(), p.pkg_path.to_string());
}
let packages = load_packages(&LoadPackageOptions {
paths: parse_args.paths,
load_opts: Some(LoadProgramOptions {
k_code_list: parse_args.sources.clone(),
package_maps,
load_plugins: true,
..Default::default()
}),
resolve_ast: args.resolve_ast,
load_builtin: args.load_builtin,
})?;
let packages = load_packages_with_cache(
&LoadPackageOptions {
paths: parse_args.paths,
load_opts: Some(LoadProgramOptions {
k_code_list: parse_args.sources.clone(),
package_maps,
load_plugins: true,
..Default::default()
}),
resolve_ast: args.resolve_ast,
load_builtin: args.load_builtin,
},
module_cache,
scope_cache,
GlobalState::default(),
)?;
if args.with_ast_index {
// Thread local options
kclvm_ast::ast::set_should_serialize_id(true);
Expand Down
Loading