From cb8da0ea7c6d0cf02757b95af83c8c2fe72009e4 Mon Sep 17 00:00:00 2001 From: peefy Date: Thu, 23 Nov 2023 10:24:34 +0800 Subject: [PATCH] chore: remove un-used code Signed-off-by: peefy --- kclvm/tools/src/LSP/src/find_refs.rs | 4 +- kclvm/tools/src/LSP/src/goto_def.rs | 402 +-------------------------- kclvm/tools/src/LSP/src/util.rs | 68 +---- 3 files changed, 12 insertions(+), 462 deletions(-) diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index 2a35fab43..a84b86c17 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -14,10 +14,10 @@ use std::collections::HashMap; use std::sync::Arc; pub(crate) fn find_refs Result<(), anyhow::Error>>( - program: &Program, + _program: &Program, kcl_pos: &KCLPos, include_declaration: bool, - prog_scope: &ProgramScope, + _prog_scope: &ProgramScope, word_index_map: Arc>>>>, vfs: Option>>, logger: F, diff --git a/kclvm/tools/src/LSP/src/goto_def.rs b/kclvm/tools/src/LSP/src/goto_def.rs index 4a669493a..cf2b59223 100644 --- a/kclvm/tools/src/LSP/src/goto_def.rs +++ b/kclvm/tools/src/LSP/src/goto_def.rs @@ -7,31 +7,14 @@ //! + schema attr //! + attr type -use indexmap::{IndexMap, IndexSet}; -use kclvm_ast::pos::{ContainsPos, GetPos}; - -use kclvm_ast::ast::{Expr, Identifier, ImportStmt, Node, Program, Stmt}; +use crate::to_lsp::lsp_location; +use indexmap::IndexSet; +use kclvm_ast::ast::Program; use kclvm_error::Position as KCLPos; - -use kclvm_sema::builtin::{get_system_member_function_ty, STRING_MEMBER_FUNCTIONS}; use kclvm_sema::core::global_state::GlobalState; use kclvm_sema::core::symbol::SymbolRef; -use kclvm_sema::pkgpath_without_prefix; -use kclvm_sema::resolver::scope::{ - builtin_scope, ProgramScope, Scope, ScopeObject, ScopeObjectKind, -}; -use kclvm_sema::ty::{DictType, SchemaType}; +use kclvm_sema::resolver::scope::ProgramScope; use lsp_types::GotoDefinitionResponse; -use std::cell::RefCell; -use std::path::Path; -use std::rc::Rc; -use std::sync::Arc; - -use crate::to_lsp::lsp_location; -use crate::util::{ - fix_missing_identifier, get_pkg_scope, get_pos_from_real_path, get_real_path_from_external, - inner_most_expr_in_stmt, -}; // Navigates to the definition of an identifier. pub(crate) fn goto_definition_with_gs( @@ -97,361 +80,6 @@ pub(crate) fn find_def_with_gs( } } -// Navigates to the definition of an identifier. -#[deprecated = "Wait for migrate to new sema model"] -pub(crate) fn goto_definition( - program: &Program, - kcl_pos: &KCLPos, - prog_scope: &ProgramScope, -) -> Option { - match program.pos_to_stmt(kcl_pos) { - Some(node) => match node.node { - Stmt::Import(stmt) => goto_def_for_import(&stmt, kcl_pos, prog_scope, program), - _ => match find_def(node.clone(), kcl_pos, prog_scope) { - Some(def) => positions_to_goto_def_resp(&def.get_positions()), - None => None, - }, - }, - None => None, - } -} - -#[deprecated = "Wait for migrate to new sema model"] -#[derive(Debug)] -pub enum Definition { - Object(ScopeObject, String), - Scope(Scope, String), -} - -#[deprecated = "Wait for migrate to new sema model"] -impl Definition { - pub(crate) fn get_positions(&self) -> IndexSet<(KCLPos, KCLPos)> { - let mut positions = IndexSet::new(); - match self { - Definition::Object(obj, _) => { - positions.insert((obj.start.clone(), obj.end.clone())); - } - Definition::Scope(scope, _) => match &scope.kind { - kclvm_sema::resolver::scope::ScopeKind::Package(filenames) => { - for file in filenames { - let dummy_pos = KCLPos { - filename: file.clone(), - line: 1, - column: None, - }; - positions.insert((dummy_pos.clone(), dummy_pos)); - } - } - _ => { - positions.insert((scope.start.clone(), scope.end.clone())); - } - }, - } - positions - } - pub(crate) fn get_name(&self) -> String { - match self { - Definition::Object(_, name) => name.clone(), - Definition::Scope(_, name) => name.clone(), - } - } -} - -#[deprecated = "Wait for migrate to new sema model"] -pub(crate) fn find_def( - node: Node, - kcl_pos: &KCLPos, - prog_scope: &ProgramScope, -) -> Option { - fn pre_process_identifier(id: Node, pos: &KCLPos) -> Identifier { - if !id.contains_pos(pos) && id.node.names.is_empty() { - return id.node.clone(); - } - - let mut id = id.node.clone(); - let mut names = vec![]; - for name in id.names { - names.push(name.clone()); - if name.contains_pos(pos) { - break; - } - } - id.names = fix_missing_identifier(&names); - if !id.pkgpath.is_empty() { - id.names[0].node = pkgpath_without_prefix!(id.pkgpath); - } - id - } - - let (inner_expr, parent) = inner_most_expr_in_stmt(&node.node, kcl_pos, None); - if let Some(expr) = inner_expr { - match expr.node { - Expr::Identifier(id) => { - let id_node = Node::node_with_pos( - id.clone(), - ( - expr.filename, - expr.line, - expr.column, - expr.end_line, - expr.end_column, - ), - ); - let id = pre_process_identifier(id_node, kcl_pos); - match parent { - Some(schema_expr) => { - if let Expr::Schema(schema_expr) = schema_expr.node { - let schema_def = - find_def(node, &schema_expr.name.get_end_pos(), prog_scope); - if let Some(schema) = schema_def { - match schema { - Definition::Object(obj, _) => match &obj.ty.kind { - kclvm_sema::ty::TypeKind::Schema(schema_type) => { - return find_attr_in_schema( - &schema_type, - &id.names, - &prog_scope.scope_map, - ) - } - _ => {} - }, - Definition::Scope(_, _) => {} - } - } - } - } - None => { - if let Some(inner_most_scope) = prog_scope.inner_most_scope(kcl_pos) { - return resolve_var( - &id.names, - &inner_most_scope, - &prog_scope.scope_map, - ); - } - } - } - } - Expr::Selector(select_expr) => { - if select_expr.attr.contains_pos(kcl_pos) { - let value_def = find_def(node, &select_expr.value.get_end_pos(), prog_scope); - let id = select_expr.attr; - match value_def { - Some(def) => match def { - Definition::Object(obj, _) => match &obj.ty.kind { - kclvm_sema::ty::TypeKind::Schema(schema_type) => { - return find_attr_in_schema( - &schema_type, - &id.node.names, - &prog_scope.scope_map, - ) - } - _ => {} - }, - Definition::Scope(_, _) => {} - }, - None => { - if let Some(inner_most_scope) = prog_scope.inner_most_scope(kcl_pos) { - return resolve_var( - &id.node.names, - &inner_most_scope, - &prog_scope.scope_map, - ); - } - } - } - } - } - Expr::Config(_) | Expr::ConfigIfEntry(_) => match parent { - Some(schema_expr) => { - if let Expr::Schema(schema_expr) = schema_expr.node { - return find_def(node, &schema_expr.name.get_end_pos(), prog_scope); - } - } - None => {} - }, - _ => {} - } - } - None -} - -/// Similar to vars.rs/resolver_var, find a ScopeObj corresponding to the definition of identifier -#[deprecated = "Wait for migrate to new sema model"] -pub(crate) fn resolve_var( - node_names: &[Node], - current_scope: &Scope, - scope_map: &IndexMap>>, -) -> Option { - let names = node_names - .iter() - .map(|node| node.node.clone()) - .collect::>(); - match names.len() { - 0 => None, - 1 => { - let name = names[0].clone(); - match current_scope.lookup(&name) { - Some(obj) => match &obj.borrow().kind { - kclvm_sema::resolver::scope::ScopeObjectKind::Module(_) => { - match &obj.borrow().ty.kind { - kclvm_sema::ty::TypeKind::Module(module_ty) => match module_ty.kind { - kclvm_sema::ty::ModuleKind::User => scope_map - .get(&pkgpath_without_prefix!(module_ty.pkgpath)) - .map(|scope| Definition::Scope(scope.borrow().clone(), name)), - kclvm_sema::ty::ModuleKind::System => { - Some(Definition::Object(obj.borrow().clone(), name)) - } - kclvm_sema::ty::ModuleKind::Plugin => None, - }, - _ => None, - } - } - _ => Some(Definition::Object(obj.borrow().clone(), name)), - }, - None => match builtin_scope().lookup(&name) { - Some(obj) => { - let mut obj = obj.borrow().clone(); - let doc = { - match &obj.ty.kind { - kclvm_sema::ty::TypeKind::Function(func) => Some(func.doc.clone()), - _ => None, - } - }; - obj.kind = ScopeObjectKind::FunctionCall; - obj.doc = doc; - obj.start = node_names[0].get_pos(); - obj.end = node_names[0].get_end_pos(); - Some(Definition::Object(obj, name)) - } - None => None, - }, - } - } - _ => { - let name = names[0].clone(); - match current_scope.lookup(&name) { - Some(obj) => match &obj.borrow().ty.kind { - kclvm_sema::ty::TypeKind::Schema(schema_type) => { - find_attr_in_schema(schema_type, &node_names[1..], scope_map) - } - kclvm_sema::ty::TypeKind::Module(module_ty) => match module_ty.kind { - kclvm_sema::ty::ModuleKind::User => { - match scope_map.get(&pkgpath_without_prefix!(module_ty.pkgpath)) { - Some(scope) => { - return resolve_var( - &node_names[1..], - &scope.borrow(), - scope_map, - ); - } - None => None, - } - } - kclvm_sema::ty::ModuleKind::System => { - if node_names.len() == 2 { - let func_name_node = node_names[1].clone(); - let func_name = func_name_node.node.clone(); - let ty = get_system_member_function_ty(&name, &func_name); - match &ty.kind { - kclvm_sema::ty::TypeKind::Function(func_ty) => { - return Some(Definition::Object( - ScopeObject { - name: func_name.clone(), - start: func_name_node.get_pos(), - end: func_name_node.get_end_pos(), - ty: ty.clone(), - kind: ScopeObjectKind::FunctionCall, - doc: Some(func_ty.doc.clone()), - }, - func_name, - )) - } - _ => return None, - } - } - None - } - kclvm_sema::ty::ModuleKind::Plugin => None, - }, - kclvm_sema::ty::TypeKind::Dict(DictType { attrs, .. }) => { - let key_name = names[1].clone(); - match attrs.get(&key_name) { - Some(attr) => { - let start_pos = attr.range.0.clone(); - for (_, scope) in scope_map { - match scope.borrow().inner_most(&start_pos) { - Some(inner_most_scope) => { - return resolve_var( - &node_names[1..], - &inner_most_scope, - scope_map, - ) - } - None => continue, - } - } - None - } - None => None, - } - } - kclvm_sema::ty::TypeKind::Str => { - if names.len() == 2 { - let func_name_node = node_names[1].clone(); - let func_name = func_name_node.node.clone(); - if let Some(ty) = STRING_MEMBER_FUNCTIONS.get(&func_name) { - match &ty.kind { - kclvm_sema::ty::TypeKind::Function(func_ty) => { - return Some(Definition::Object( - ScopeObject { - name: func_name.clone(), - start: func_name_node.get_pos(), - end: func_name_node.get_end_pos(), - ty: Arc::new(ty.clone()), - kind: ScopeObjectKind::FunctionCall, - doc: Some(func_ty.doc.clone()), - }, - func_name, - )) - } - // unreachable - _ => {} - } - } - } - None - } - _ => None, - }, - None => None, - } - } - } -} - -#[deprecated = "Wait for migrate to new sema model"] -pub fn find_attr_in_schema( - schema_type: &SchemaType, - names: &[Node], - scope_map: &IndexMap>>, -) -> Option { - let schema_pkg_scope = get_pkg_scope(&schema_type.pkgpath, scope_map); - let names = if schema_type.pkgpath.is_empty() { - &names[1..] - } else { - names - }; - for child in &schema_pkg_scope.children { - let child_scope = child.borrow(); - if let kclvm_sema::resolver::scope::ScopeKind::Schema(schema_name) = &child_scope.kind { - if schema_name == &schema_type.name { - return resolve_var(names, &child_scope, scope_map); - } - } - } - None -} - // Convert kcl position to GotoDefinitionResponse. This function will convert to // None, Scalar or Array according to the number of positions fn positions_to_goto_def_resp( @@ -475,28 +103,6 @@ fn positions_to_goto_def_resp( } } -#[deprecated = "Wait for migrate to new sema model"] -fn goto_def_for_import( - stmt: &ImportStmt, - _kcl_pos: &KCLPos, - _prog_scope: &ProgramScope, - program: &Program, -) -> Option { - let pkgpath = &stmt.path; - let mut real_path = - Path::new(&program.root).join(pkgpath.replace('.', &std::path::MAIN_SEPARATOR.to_string())); - let mut positions = get_pos_from_real_path(&real_path); - - if positions.is_empty() && !real_path.exists() { - real_path = - get_real_path_from_external(&stmt.pkg_name, pkgpath, program.root.clone().into()); - } - - positions = get_pos_from_real_path(&real_path); - - positions_to_goto_def_resp(&positions) -} - #[cfg(test)] mod tests { use super::goto_definition_with_gs; diff --git a/kclvm/tools/src/LSP/src/util.rs b/kclvm/tools/src/LSP/src/util.rs index d6f9e5c40..2af1f0ad5 100644 --- a/kclvm/tools/src/LSP/src/util.rs +++ b/kclvm/tools/src/LSP/src/util.rs @@ -1,10 +1,9 @@ -use indexmap::{IndexMap, IndexSet}; +use indexmap::IndexSet; use kclvm_ast::ast::{ ConfigEntry, Expr, Identifier, Node, NodeRef, PosTuple, Program, SchemaStmt, Stmt, Type, }; use kclvm_ast::pos::ContainsPos; -use kclvm_ast::MAIN_PKG; -use kclvm_config::modfile::KCL_FILE_EXTENSION; + use kclvm_driver::kpm_metadata::fetch_metadata; use kclvm_driver::{get_kcl_files, lookup_compile_unit}; use kclvm_error::Diagnostic; @@ -14,20 +13,20 @@ use kclvm_parser::{load_program, ParseSession}; use kclvm_sema::advanced_resolver::AdvancedResolver; use kclvm_sema::core::global_state::GlobalState; use kclvm_sema::namer::Namer; -use kclvm_sema::pkgpath_without_prefix; + use kclvm_sema::resolver::resolve_program_with_opts; use kclvm_sema::resolver::scope::ProgramScope; -use kclvm_sema::resolver::scope::Scope; + use kclvm_span::symbol::reserved; use kclvm_utils::pkgpath::rm_external_pkg_name; use lsp_types::{Location, Position, Range, Url}; use parking_lot::{RwLock, RwLockReadGuard}; use ra_ap_vfs::{FileId, Vfs}; use serde::{de::DeserializeOwned, Serialize}; -use std::cell::RefCell; + use std::collections::HashMap; use std::path::{Path, PathBuf}; -use std::rc::Rc; + use std::{fs, sync::Arc}; use crate::from_lsp; @@ -696,39 +695,6 @@ fn build_identifier_from_ty_string(ty: &NodeRef, pos: &KCLPos) -> Option IndexSet<(kclvm_error::Position, kclvm_error::Position)> { - let mut positions = IndexSet::new(); - let mut k_file = real_path.clone(); - k_file.set_extension(KCL_FILE_EXTENSION); - - if k_file.is_file() { - let start = KCLPos { - filename: k_file.to_str().unwrap().to_string(), - line: 1, - column: None, - }; - let end = start.clone(); - positions.insert((start, end)); - } else if real_path.is_dir() { - if let Ok(files) = get_kcl_files(real_path, false) { - positions.extend(files.iter().map(|file| { - let start = KCLPos { - filename: file.clone(), - line: 1, - column: None, - }; - let end = start.clone(); - (start, end) - })) - } - } - positions -} - /// [`get_real_path_from_external`] will ask for the local path for [`pkg_name`] with subdir [`pkgpath`] from `kpm`. /// If the external package, whose [`pkg_name`] is 'my_package', is stored in '\user\my_package_v0.0.1'. /// The [`pkgpath`] is 'my_package.examples.apps'. @@ -761,28 +727,6 @@ pub(crate) fn get_real_path_from_external( real_path } -/// Error recovery may generate an Identifier with an empty string at the end, e.g., -/// a. => vec["a", ""]. -/// When analyzing in LSP, the empty string needs to be removed and find definition of the second last name("a"). -pub(crate) fn fix_missing_identifier(names: &[Node]) -> Vec> { - if !names.is_empty() && names.last().unwrap().node.is_empty() { - names[..names.len() - 1].to_vec() - } else { - names.to_vec() - } -} - -pub(crate) fn get_pkg_scope( - pkgpath: &String, - scope_map: &IndexMap>>, -) -> Scope { - scope_map - .get(&pkgpath_without_prefix!(pkgpath)) - .unwrap_or(scope_map.get(MAIN_PKG).unwrap()) - .borrow() - .clone() -} - pub(crate) fn build_word_index_for_file_paths( paths: &[String], prune: bool,