1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_LSP_CLASS_HIERARCHY_INFO_H 17 #define ES2PANDA_LSP_CLASS_HIERARCHY_INFO_H 18 19 #include <memory> 20 #include <unordered_map> 21 #include <vector> 22 #include "class_hierarchy_item.h" 23 #include "public/es2panda_lib.h" 24 25 namespace ark::es2panda::lsp { 26 class FunctionParamStyle { 27 public: FunctionParamStyle(std::string paramName,std::string paramKind)28 FunctionParamStyle(std::string paramName, std::string paramKind) 29 : name_(std::move(paramName)), kind_(std::move(paramKind)) 30 { 31 } 32 GetParamName()33 std::string GetParamName() const 34 { 35 return name_; 36 } 37 GetParamKind()38 std::string GetParamKind() const 39 { 40 return kind_; 41 } 42 43 private: 44 std::string name_; 45 std::string kind_; 46 }; 47 48 class ClassHierarchyInfo { 49 public: 50 ClassHierarchyInfo() = default; 51 52 virtual ~ClassHierarchyInfo() = default; 53 54 ClassHierarchyInfo(const ClassHierarchyInfo &other) = default; 55 ClassHierarchyInfo(ClassHierarchyInfo &&other) = default; 56 ClassHierarchyInfo &operator=(const ClassHierarchyInfo &other) = default; 57 ClassHierarchyInfo &operator=(ClassHierarchyInfo &&other) = default; 58 SetClassName(const std::string & className)59 void SetClassName(const std::string &className) 60 { 61 if (className.empty()) { 62 return; 63 } 64 className_ = className; 65 } 66 GetClassName()67 const std::string &GetClassName() const 68 { 69 return className_; 70 } 71 GetMethodItemList()72 const std::unordered_map<std::string, std::shared_ptr<ClassMethodItem>> &GetMethodItemList() const 73 { 74 return methodItems_; 75 } 76 GetPropertyItemList()77 const std::unordered_map<std::string, std::shared_ptr<ClassPropertyItem>> &GetPropertyItemList() const 78 { 79 return propertyItems_; 80 } 81 AddItemToMethodList(const std::shared_ptr<ClassMethodItem> & item)82 bool AddItemToMethodList(const std::shared_ptr<ClassMethodItem> &item) 83 { 84 if (item == nullptr) { 85 return false; 86 } 87 auto detail = item->GetDetail(); 88 auto result = methodItems_.try_emplace(detail, item); 89 return result.second; 90 } 91 AddItemToPropertyList(const std::shared_ptr<ClassPropertyItem> & item)92 bool AddItemToPropertyList(const std::shared_ptr<ClassPropertyItem> &item) 93 { 94 if (item == nullptr) { 95 return false; 96 } 97 auto detail = item->GetDetail(); 98 auto result = propertyItems_.try_emplace(detail, item); 99 return result.second; 100 } 101 DeleteTargetItemInMethodList(const std::shared_ptr<ClassMethodItem> & item)102 void DeleteTargetItemInMethodList(const std::shared_ptr<ClassMethodItem> &item) 103 { 104 if (item == nullptr) { 105 return; 106 } 107 auto detail = item->GetDetail(); 108 methodItems_.erase(detail); 109 } 110 DeleteTargetItemInPropertyList(const std::shared_ptr<ClassPropertyItem> & item)111 void DeleteTargetItemInPropertyList(const std::shared_ptr<ClassPropertyItem> &item) 112 { 113 if (item == nullptr) { 114 return; 115 } 116 auto detail = item->GetDetail(); 117 propertyItems_.erase(detail); 118 } 119 DeleteAllItemsInMethodList()120 void DeleteAllItemsInMethodList() 121 { 122 methodItems_.clear(); 123 } 124 DeleteAllItemsInPropertyList()125 void DeleteAllItemsInPropertyList() 126 { 127 propertyItems_.clear(); 128 } 129 IsItemExistInMethodList(const std::shared_ptr<ClassMethodItem> & item)130 bool IsItemExistInMethodList(const std::shared_ptr<ClassMethodItem> &item) const 131 { 132 if (item == nullptr) { 133 return false; 134 } 135 auto detail = item->GetDetail(); 136 auto iter = methodItems_.find(detail); 137 return iter != methodItems_.end(); 138 } 139 IsItemExistInPropertyList(const std::shared_ptr<ClassPropertyItem> & item)140 bool IsItemExistInPropertyList(const std::shared_ptr<ClassPropertyItem> &item) const 141 { 142 if (item == nullptr) { 143 return false; 144 } 145 auto detail = item->GetDetail(); 146 auto iter = propertyItems_.find(detail); 147 return iter != propertyItems_.end(); 148 } 149 150 private: 151 std::string className_; 152 std::unordered_map<std::string, std::shared_ptr<ClassMethodItem>> methodItems_; 153 std::unordered_map<std::string, std::shared_ptr<ClassPropertyItem>> propertyItems_; 154 }; 155 156 using ClassHierarchy = std::vector<ClassHierarchyInfo>; 157 158 /** 159 * Retrieve the list of undefined virtual functions and properties in the parent class. 160 * 161 * such as ets: 162 * class Animal { 163 * public body_: string = ''; 164 * 165 * public action(): void { 166 * console.log("need Animal action"); 167 * } 168 * public sleep(): void { 169 * console.log("need sleep"); 170 * } 171 * } 172 * 173 * class Bird extends Animal { 174 * action(): void { 175 * console.log("need Bird action"); 176 * } 177 * 178 * Drink(): void { 179 * console.log("need Bird Drink"); 180 * } 181 * } 182 * when clicking 'Bird'. ClassHierarchy is : 183 * [ { "Animal", { detail: body_: string, ClassDefinitionStyle FIELD, AccessModifierStyle: PUBLIC } }, 184 * { "Animal", { detail: sleep(): void, ClassDefinitionStyle: METHOD, AccessModifierStyle: PUBLIC } } ]. 185 */ 186 ClassHierarchy GetClassHierarchyInfoImpl(es2panda_Context *context, size_t position); 187 } // namespace ark::es2panda::lsp 188 189 #endif 190