|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use emmylua_code_analysis::{ |
| 4 | + LuaCompilation, LuaDeclId, LuaMemberId, LuaSemanticDeclId, LuaType, LuaTypeDeclId, |
| 5 | + SemanticDeclLevel, SemanticModel, |
| 6 | +}; |
| 7 | +use emmylua_parser::{ |
| 8 | + LuaAstNode, LuaDocTagField, LuaIndexExpr, LuaStat, LuaSyntaxNode, LuaSyntaxToken, |
| 9 | +}; |
| 10 | +use lsp_types::Location; |
| 11 | + |
| 12 | +pub fn search_implementations( |
| 13 | + semantic_model: &SemanticModel, |
| 14 | + compilation: &LuaCompilation, |
| 15 | + token: LuaSyntaxToken, |
| 16 | +) -> Option<Vec<Location>> { |
| 17 | + let mut result = Vec::new(); |
| 18 | + if let Some(semantic_decl) = |
| 19 | + semantic_model.find_decl(token.clone().into(), SemanticDeclLevel::NoTrace) |
| 20 | + { |
| 21 | + match semantic_decl { |
| 22 | + LuaSemanticDeclId::TypeDecl(type_decl_id) => { |
| 23 | + search_type_implementations(semantic_model, compilation, type_decl_id, &mut result); |
| 24 | + } |
| 25 | + LuaSemanticDeclId::Member(member_id) => { |
| 26 | + search_member_implementations(semantic_model, compilation, member_id, &mut result); |
| 27 | + } |
| 28 | + LuaSemanticDeclId::LuaDecl(decl_id) => { |
| 29 | + search_decl_implementations(semantic_model, compilation, decl_id, &mut result); |
| 30 | + } |
| 31 | + _ => {} |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + Some(result) |
| 36 | +} |
| 37 | + |
| 38 | +pub fn search_member_implementations( |
| 39 | + semantic_model: &SemanticModel, |
| 40 | + compilation: &LuaCompilation, |
| 41 | + member_id: LuaMemberId, |
| 42 | + result: &mut Vec<Location>, |
| 43 | +) -> Option<()> { |
| 44 | + let member = semantic_model |
| 45 | + .get_db() |
| 46 | + .get_member_index() |
| 47 | + .get_member(&member_id)?; |
| 48 | + let key = member.get_key(); |
| 49 | + let index_references = semantic_model |
| 50 | + .get_db() |
| 51 | + .get_reference_index() |
| 52 | + .get_index_references(&key)?; |
| 53 | + |
| 54 | + let mut semantic_cache = HashMap::new(); |
| 55 | + |
| 56 | + let property_owner = LuaSemanticDeclId::Member(member_id); |
| 57 | + for in_filed_syntax_id in index_references { |
| 58 | + let semantic_model = |
| 59 | + if let Some(semantic_model) = semantic_cache.get_mut(&in_filed_syntax_id.file_id) { |
| 60 | + semantic_model |
| 61 | + } else { |
| 62 | + let semantic_model = compilation.get_semantic_model(in_filed_syntax_id.file_id)?; |
| 63 | + semantic_cache.insert(in_filed_syntax_id.file_id, semantic_model); |
| 64 | + semantic_cache.get_mut(&in_filed_syntax_id.file_id)? |
| 65 | + }; |
| 66 | + let root = semantic_model.get_root(); |
| 67 | + let node = in_filed_syntax_id.value.to_node_from_root(root.syntax())?; |
| 68 | + |
| 69 | + if check_member_reference(&semantic_model, node.clone()).is_none() { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + if !semantic_model.is_reference_to( |
| 74 | + node, |
| 75 | + property_owner.clone(), |
| 76 | + SemanticDeclLevel::default(), |
| 77 | + ) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + let document = semantic_model.get_document(); |
| 82 | + let range = in_filed_syntax_id.value.get_range(); |
| 83 | + let location = document.to_lsp_location(range)?; |
| 84 | + result.push(location); |
| 85 | + } |
| 86 | + Some(()) |
| 87 | +} |
| 88 | + |
| 89 | +/// 检查成员引用是否符合实现 |
| 90 | +fn check_member_reference(semantic_model: &SemanticModel, node: LuaSyntaxNode) -> Option<()> { |
| 91 | + match &node { |
| 92 | + expr_node if LuaIndexExpr::can_cast(expr_node.kind().into()) => { |
| 93 | + let expr = LuaIndexExpr::cast(expr_node.clone())?; |
| 94 | + let prefix_type = semantic_model |
| 95 | + .infer_expr(expr.get_prefix_expr()?.into()) |
| 96 | + .ok()?; |
| 97 | + match prefix_type { |
| 98 | + LuaType::Ref(_) => { |
| 99 | + return None; |
| 100 | + } |
| 101 | + _ => {} |
| 102 | + }; |
| 103 | + // 往上寻找 stat 节点 |
| 104 | + let stat = expr.ancestors::<LuaStat>().next()?; |
| 105 | + match stat { |
| 106 | + LuaStat::FuncStat(_) => { |
| 107 | + return Some(()); |
| 108 | + } |
| 109 | + LuaStat::AssignStat(assign_stat) => { |
| 110 | + // 判断是否在左侧 |
| 111 | + let (vars, _) = assign_stat.get_var_and_expr_list(); |
| 112 | + for var in vars { |
| 113 | + if var |
| 114 | + .syntax() |
| 115 | + .text_range() |
| 116 | + .contains(node.text_range().start()) |
| 117 | + { |
| 118 | + return Some(()); |
| 119 | + } |
| 120 | + } |
| 121 | + return None; |
| 122 | + } |
| 123 | + _ => { |
| 124 | + return None; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + tag_field_node if LuaDocTagField::can_cast(tag_field_node.kind().into()) => { |
| 129 | + return Some(()); |
| 130 | + } |
| 131 | + _ => {} |
| 132 | + } |
| 133 | + |
| 134 | + Some(()) |
| 135 | +} |
| 136 | +pub fn search_type_implementations( |
| 137 | + semantic_model: &SemanticModel, |
| 138 | + compilation: &LuaCompilation, |
| 139 | + type_decl_id: LuaTypeDeclId, |
| 140 | + result: &mut Vec<Location>, |
| 141 | +) -> Option<()> { |
| 142 | + let db = semantic_model.get_db(); |
| 143 | + let type_index = db.get_type_index(); |
| 144 | + let type_decl = type_index.get_type_decl(&type_decl_id)?; |
| 145 | + let locations = type_decl.get_locations(); |
| 146 | + let mut semantic_cache = HashMap::new(); |
| 147 | + for location in locations { |
| 148 | + let semantic_model = if let Some(semantic_model) = semantic_cache.get_mut(&location.file_id) |
| 149 | + { |
| 150 | + semantic_model |
| 151 | + } else { |
| 152 | + let semantic_model = compilation.get_semantic_model(location.file_id)?; |
| 153 | + semantic_cache.insert(location.file_id, semantic_model); |
| 154 | + semantic_cache.get_mut(&location.file_id)? |
| 155 | + }; |
| 156 | + let document = semantic_model.get_document(); |
| 157 | + let range = location.range; |
| 158 | + let location = document.to_lsp_location(range)?; |
| 159 | + result.push(location); |
| 160 | + } |
| 161 | + |
| 162 | + Some(()) |
| 163 | +} |
| 164 | + |
| 165 | +pub fn search_decl_implementations( |
| 166 | + semantic_model: &SemanticModel, |
| 167 | + compilation: &LuaCompilation, |
| 168 | + decl_id: LuaDeclId, |
| 169 | + result: &mut Vec<Location>, |
| 170 | +) -> Option<()> { |
| 171 | + let decl = semantic_model |
| 172 | + .get_db() |
| 173 | + .get_decl_index() |
| 174 | + .get_decl(&decl_id)?; |
| 175 | + |
| 176 | + if decl.is_local() { |
| 177 | + let document = semantic_model.get_document(); |
| 178 | + let range = decl.get_range(); |
| 179 | + let location = document.to_lsp_location(range)?; |
| 180 | + result.push(location); |
| 181 | + return Some(()); |
| 182 | + } else { |
| 183 | + let name = decl.get_name(); |
| 184 | + let global_decl_ids = semantic_model |
| 185 | + .get_db() |
| 186 | + .get_global_index() |
| 187 | + .get_global_decl_ids(name)?; |
| 188 | + |
| 189 | + let mut semantic_cache = HashMap::new(); |
| 190 | + |
| 191 | + for global_decl_id in global_decl_ids { |
| 192 | + let semantic_model = |
| 193 | + if let Some(semantic_model) = semantic_cache.get_mut(&global_decl_id.file_id) { |
| 194 | + semantic_model |
| 195 | + } else { |
| 196 | + let semantic_model = compilation.get_semantic_model(global_decl_id.file_id)?; |
| 197 | + semantic_cache.insert(global_decl_id.file_id, semantic_model); |
| 198 | + semantic_cache.get_mut(&global_decl_id.file_id)? |
| 199 | + }; |
| 200 | + let Some(decl) = semantic_model |
| 201 | + .get_db() |
| 202 | + .get_decl_index() |
| 203 | + .get_decl(&global_decl_id) |
| 204 | + else { |
| 205 | + continue; |
| 206 | + }; |
| 207 | + |
| 208 | + let document = semantic_model.get_document(); |
| 209 | + let range = decl.get_range(); |
| 210 | + let location = document.to_lsp_location(range)?; |
| 211 | + result.push(location); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + Some(()) |
| 216 | +} |
0 commit comments