diff --git a/kclvm/query/src/query.rs b/kclvm/query/src/query.rs index a931c4074..733debfe2 100644 --- a/kclvm/query/src/query.rs +++ b/kclvm/query/src/query.rs @@ -4,7 +4,7 @@ use anyhow::Result; use indexmap::IndexMap; use kclvm_parser::{load_program, LoadProgramOptions, ParseSession}; use kclvm_sema::{ - resolver::{resolve_program, scope::Scope}, + resolver::{resolve_program_with_opts, scope::Scope, Options}, ty::SchemaType, }; @@ -121,7 +121,13 @@ fn resolve_file(file: &str, code: Option<&str>) -> Result>> { return Err(anyhow::anyhow!("{err}")); } }; - let scope = resolve_program(&mut program); + let scope = resolve_program_with_opts( + &mut program, + Options { + resolve_val: true, + ..Default::default() + }, + ); match scope.main_scope() { Some(scope) => Ok(scope.clone()), None => Err(anyhow::anyhow!("main scope is not found")), diff --git a/kclvm/sema/src/resolver/global.rs b/kclvm/sema/src/resolver/global.rs index 5022922b9..34527a564 100644 --- a/kclvm/sema/src/resolver/global.rs +++ b/kclvm/sema/src/resolver/global.rs @@ -602,7 +602,11 @@ impl<'ctx> Resolver<'ctx> { let name = unification_stmt.value.node.name.node.get_name(); let ty = self.parse_ty_str_with_scope(&name, stmt.get_span_pos()); let is_optional = true; - let default = print_schema_expr(&unification_stmt.value.node); + let default = if self.options.resolve_val { + print_schema_expr(&unification_stmt.value.node) + } else { + "".to_string() + }; ( unification_stmt.target.node.get_name(), ty, @@ -619,10 +623,13 @@ impl<'ctx> Resolver<'ctx> { schema_attr.ty.get_span_pos(), ); let is_optional = schema_attr.is_optional; - let default = schema_attr - .value - .as_ref() - .map(|v| print_ast_node(ASTNode::Expr(v))); + let default = schema_attr.value.as_ref().map(|v| { + if self.options.resolve_val { + print_ast_node(ASTNode::Expr(v)) + } else { + "".to_string() + } + }); // Schema attribute decorators let decorators = self.resolve_decorators( &schema_attr.decorators, diff --git a/kclvm/sema/src/resolver/mod.rs b/kclvm/sema/src/resolver/mod.rs index a104bf53b..fdae6e513 100644 --- a/kclvm/sema/src/resolver/mod.rs +++ b/kclvm/sema/src/resolver/mod.rs @@ -136,25 +136,34 @@ pub struct Context { pub type_alias_mapping: IndexMap>, } -/// Resolve options -#[derive(Clone, Debug, Default)] +/// Resolve options. +/// - lint_check: whether to run lint passes +/// - resolve_val: whether to resolve and print their AST to value for some nodes. +#[derive(Clone, Debug)] pub struct Options { - pub raise_err: bool, - pub config_auto_fix: bool, pub lint_check: bool, + pub resolve_val: bool, } -/// Resolve program +impl Default for Options { + fn default() -> Self { + Self { + lint_check: true, + resolve_val: false, + } + } +} + +/// Resolve program with default options. +#[inline] pub fn resolve_program(program: &mut Program) -> ProgramScope { + resolve_program_with_opts(program, Options::default()) +} + +/// Resolve program with options. See [Options] +pub fn resolve_program_with_opts(program: &mut Program, opts: Options) -> ProgramScope { pre_process_program(program); - let mut resolver = Resolver::new( - program, - Options { - raise_err: true, - config_auto_fix: false, - lint_check: true, - }, - ); + let mut resolver = Resolver::new(program, opts); resolver.resolve_import(); let scope = resolver.check_and_lint(kclvm_ast::MAIN_PKG); let type_alias_mapping = resolver.ctx.type_alias_mapping.clone(); diff --git a/kclvm/sema/src/resolver/schema.rs b/kclvm/sema/src/resolver/schema.rs index 3cd178ef3..264a051ae 100644 --- a/kclvm/sema/src/resolver/schema.rs +++ b/kclvm/sema/src/resolver/schema.rs @@ -222,23 +222,27 @@ impl<'ctx> Resolver<'ctx> { args: &'ctx [ast::NodeRef], kwargs: &'ctx [ast::NodeRef], ) -> (Vec, HashMap) { - ( - args.iter() - .map(|a| print_ast_node(ASTNode::Expr(a))) - .collect(), - kwargs - .iter() - .map(|a| { - ( - a.node.arg.node.get_name(), - a.node - .value - .as_ref() - .map(|v| print_ast_node(ASTNode::Expr(v))) - .unwrap_or_default(), - ) - }) - .collect(), - ) + if self.options.resolve_val { + ( + args.iter() + .map(|a| print_ast_node(ASTNode::Expr(a))) + .collect(), + kwargs + .iter() + .map(|a| { + ( + a.node.arg.node.get_name(), + a.node + .value + .as_ref() + .map(|v| print_ast_node(ASTNode::Expr(v))) + .unwrap_or_default(), + ) + }) + .collect(), + ) + } else { + (vec![], HashMap::new()) + } } } diff --git a/kclvm/sema/src/resolver/tests.rs b/kclvm/sema/src/resolver/tests.rs index 4299240d9..40a3d5be5 100644 --- a/kclvm/sema/src/resolver/tests.rs +++ b/kclvm/sema/src/resolver/tests.rs @@ -258,14 +258,7 @@ fn test_lint() { let mut program = load_program(sess.clone(), &["./src/resolver/test_data/lint.k"], None).unwrap(); pre_process_program(&mut program); - let mut resolver = Resolver::new( - &program, - Options { - raise_err: true, - config_auto_fix: false, - lint_check: true, - }, - ); + let mut resolver = Resolver::new(&program, Options::default()); resolver.resolve_import(); resolver.check_and_lint(kclvm_ast::MAIN_PKG); diff --git a/kclvm/tools/src/LSP/src/request.rs b/kclvm/tools/src/LSP/src/request.rs index 556d3b328..68319eb3b 100644 --- a/kclvm/tools/src/LSP/src/request.rs +++ b/kclvm/tools/src/LSP/src/request.rs @@ -1,8 +1,8 @@ use crossbeam_channel::Sender; -use lsp_types::{CodeAction, CodeActionKind, CodeActionOrCommand, Position, Range, TextEdit}; +use lsp_types::TextEdit; use ra_ap_vfs::VfsPath; -use std::{collections::HashMap, ops::Index, time::Instant}; +use std::time::Instant; use crate::{ completion::completion,