BfAutoComplete.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #pragma once
  2. #include "BfCompiler.h"
  3. #include "BfSourceClassifier.h"
  4. #include "BfResolvePass.h"
  5. NS_BF_BEGIN
  6. class BfMethodInstance;
  7. class BfLocalVariable;
  8. class AutoCompleteEntry
  9. {
  10. public:
  11. const char* mEntryType;
  12. const char* mDisplay;
  13. const char* mDocumentation;
  14. mutable int8 mNamePrefixCount;
  15. mutable int mScore;
  16. mutable uint8* mMatches;
  17. mutable uint8 mMatchesLength;
  18. public:
  19. AutoCompleteEntry()
  20. {
  21. mNamePrefixCount = 0;
  22. mMatches = nullptr;
  23. mMatchesLength = 0;
  24. }
  25. AutoCompleteEntry(const char* entryType, const char* display)
  26. {
  27. mEntryType = entryType;
  28. mDisplay = display;
  29. mDocumentation = NULL;
  30. mNamePrefixCount = 0;
  31. mScore = 0;
  32. mMatches = nullptr;
  33. mMatchesLength = 0;
  34. }
  35. AutoCompleteEntry(const char* entryType, const StringImpl& display)
  36. {
  37. mEntryType = entryType;
  38. mDisplay = display.c_str();
  39. mDocumentation = NULL;
  40. mNamePrefixCount = 0;
  41. mScore = 0;
  42. mMatches = nullptr;
  43. mMatchesLength = 0;
  44. }
  45. AutoCompleteEntry(const char* entryType, const StringImpl& display, int namePrefixCount)
  46. {
  47. mEntryType = entryType;
  48. mDisplay = display.c_str();
  49. mDocumentation = NULL;
  50. mNamePrefixCount = (int8)namePrefixCount;
  51. mScore = 0;
  52. mMatches = nullptr;
  53. mMatchesLength = 0;
  54. }
  55. bool operator==(const AutoCompleteEntry& other) const
  56. {
  57. return strcmp(mDisplay, other.mDisplay) == 0;
  58. }
  59. };
  60. NS_BF_END
  61. template <>
  62. struct BeefHash<Beefy::AutoCompleteEntry>
  63. {
  64. size_t operator()(const Beefy::AutoCompleteEntry& val)
  65. {
  66. intptr hash = 0;
  67. const char* curPtr = val.mDisplay;
  68. while (true)
  69. {
  70. char c = *(curPtr++);
  71. if (c == 0)
  72. break;
  73. hash = ((hash ^ (intptr)c) << 5) - hash;
  74. }
  75. return hash;
  76. }
  77. };
  78. NS_BF_BEGIN
  79. class AutoCompleteBase
  80. {
  81. public:
  82. class EntryLess
  83. {
  84. public:
  85. bool operator()(const AutoCompleteEntry& left, const AutoCompleteEntry& right)
  86. {
  87. auto result = _stricmp(left.mDisplay, right.mDisplay);
  88. if (result == 0)
  89. result = strcmp(left.mDisplay, right.mDisplay);
  90. return result < 0;
  91. }
  92. };
  93. public:
  94. BumpAllocator mAlloc;
  95. HashSet<AutoCompleteEntry> mEntriesSet;
  96. bool mIsGetDefinition;
  97. bool mIsAutoComplete;
  98. bool mDoFuzzyAutoComplete;
  99. int mInsertStartIdx;
  100. int mInsertEndIdx;
  101. bool DoesFilterMatch(const char* entry, const char* filter, int& score, uint8* matches, int maxMatches);
  102. AutoCompleteEntry* AddEntry(const AutoCompleteEntry& entry, const StringImpl& filter);
  103. AutoCompleteEntry* AddEntry(const AutoCompleteEntry& entry, const char* filter);
  104. AutoCompleteEntry* AddEntry(const AutoCompleteEntry& entry);
  105. AutoCompleteBase();
  106. virtual ~AutoCompleteBase();
  107. void Clear();
  108. };
  109. class BfAutoComplete : public AutoCompleteBase
  110. {
  111. public:
  112. class MethodMatchEntry
  113. {
  114. public:
  115. BfMethodDef* mMethodDef;
  116. BfFieldInstance* mPayloadEnumField;
  117. BfTypeInstance* mTypeInstance;
  118. BfTypeVector mGenericArguments;
  119. BfMethodInstance* mCurMethodInstance;
  120. bool mIsMatch;
  121. int mArgMatchCount;
  122. MethodMatchEntry()
  123. {
  124. mMethodDef = NULL;
  125. mPayloadEnumField = NULL;
  126. mTypeInstance = NULL;
  127. mCurMethodInstance = NULL;
  128. mIsMatch = false;
  129. mArgMatchCount = false;
  130. }
  131. };
  132. class MethodMatchInfo
  133. {
  134. public:
  135. BfTypeInstance* mCurTypeInstance;
  136. BfMethodInstance* mCurMethodInstance;
  137. Array<MethodMatchEntry> mInstanceList;
  138. int mInvocationSrcIdx;
  139. int mBestIdx;
  140. int mPrevBestIdx;
  141. bool mHadExactMatch;
  142. int mMostParamsMatched;
  143. Array<int> mSrcPositions; // start, commas, end
  144. public:
  145. MethodMatchInfo()
  146. {
  147. mInvocationSrcIdx = -1;
  148. mCurTypeInstance = NULL;
  149. mCurMethodInstance = NULL;
  150. mBestIdx = 0;
  151. mPrevBestIdx = -1;
  152. mHadExactMatch = false;
  153. mMostParamsMatched = 0;
  154. }
  155. ~MethodMatchInfo()
  156. {
  157. }
  158. };
  159. public:
  160. BfModule* mModule;
  161. BfCompiler* mCompiler;
  162. BfSystem* mSystem;
  163. MethodMatchInfo* mMethodMatchInfo;
  164. bool mIsCapturingMethodMatchInfo;
  165. String mDefaultSelection;
  166. String mResultString;
  167. String mDocumentationEntryName;
  168. BfAstNode* mGetDefinitionNode;
  169. BfResolveType mResolveType;
  170. BfTypeInstance* mShowAttributeProperties;
  171. BfAstNode* mIdentifierUsed;
  172. bool mIgnoreFixits;
  173. bool mHasFriendSet;
  174. bool mUncertain; // May be an unknown identifier, do not aggressively autocomplete
  175. bool mForceAllowNonStatic;
  176. int mCursorLineStart;
  177. int mCursorLineEnd;
  178. int mReplaceLocalId;
  179. BfMethodDef* mDefMethod;
  180. BfTypeDef* mDefType;
  181. BfFieldDef* mDefField;
  182. BfPropertyDef* mDefProp;
  183. BfAtomComposite mDefNamespace;
  184. int mDefMethodGenericParamIdx;
  185. int mDefTypeGenericParamIdx;
  186. public:
  187. BfProject* GetActiveProject();
  188. bool WantsEntries();
  189. bool CheckProtection(BfProtection protection, BfTypeDef* typeDef, bool allowProtected, bool allowPrivate);
  190. String GetFilter(BfAstNode* node);
  191. const char* GetTypeName(BfType* type);
  192. int GetCursorIdx(BfAstNode* node);
  193. bool IsAutocompleteNode(BfAstNode* node, int lengthAdd = 0, int startAdd = 0);
  194. bool IsAutocompleteNode(BfAstNode* startNode, BfAstNode* endNode, int lengthAdd = 0, int startAdd = 0);
  195. bool IsAutocompleteLineNode(BfAstNode* node);
  196. BfTypedValue LookupTypeRefOrIdentifier(BfAstNode* node, bool* isStatic, BfEvalExprFlags evalExprFlags = BfEvalExprFlags_None, BfType* expectingType = NULL);
  197. void SetDefinitionLocation(BfAstNode* astNode, bool force = false);
  198. bool IsAttribute(BfTypeInstance* typeInst);
  199. void AddMethod(BfTypeInstance* typeInstance, BfMethodDef* methodDef, BfMethodInstance* methodInstance, BfMethodDeclaration* methodDecl, const StringImpl& methodName, const StringImpl& filter);
  200. void AddField(BfTypeInstance* typeInst, BfFieldDef* fieldDef, BfFieldInstance* fieldInstance, const StringImpl& filter);
  201. void AddProp(BfTypeInstance* typeInst, BfPropertyDef* propDef, const StringImpl& filter);
  202. void AddTypeDef(BfTypeDef* typeDef, const StringImpl& filter, bool onlyAttribute = false);
  203. void AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& filter, BfTypeInstance* startType, bool allowProtected, bool allowPrivate);
  204. void AddCurrentTypes(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate, bool onlyAttribute);
  205. void AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bool addNonStatic, const StringImpl& filter, BfTypeInstance* startType, bool allowInterfaces, bool allowImplicitThis, bool checkOuterType);
  206. void AddSelfResultTypeMembers(BfTypeInstance* typeInst, BfTypeInstance* selfType, const StringImpl& filter, bool allowPrivate);
  207. bool InitAutocomplete(BfAstNode* dotNode, BfAstNode* nameNode, String& filter);
  208. void AddEnumTypeMembers(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate);
  209. void AddExtensionMethods(BfTypeInstance* targetType, BfTypeInstance* extensionContainer, const StringImpl& filter, bool allowProtected, bool allowPrivate);
  210. void AddTopLevelNamespaces(BfAstNode* identifierNode);
  211. void AddTopLevelTypes(BfAstNode* identifierNode, bool onlyAttribute = false);
  212. void AddOverrides(const StringImpl& filter, bool forceAll = false);
  213. void AddCtorPassthroughs();
  214. void UpdateReplaceData();
  215. void AddTypeInstanceEntry(BfTypeInstance* typeInst);
  216. bool CheckDocumentation(AutoCompleteEntry* entry, BfCommentNode* documentation);
  217. bool GetMethodInfo(BfMethodInstance* methodInst, StringImpl* methodName, StringImpl* insertString, bool isImplementing, bool isExplicitInterface);
  218. void FixitGetParamString(const BfTypeVector& paramTypes, StringImpl& outStr);
  219. int FixitGetMemberInsertPos(BfTypeDef* typeDef);
  220. String FixitGetLocation(BfParserData* parserData, int insertPos);
  221. String ConstantToString(BfIRConstHolder* constHolder, BfTypedValue typedValue);
  222. public:
  223. BfAutoComplete(BfResolveType resolveType = BfResolveType_Autocomplete, bool doFuzzyAutoComplete = false);
  224. ~BfAutoComplete();
  225. void SetModule(BfModule* module);
  226. void Clear();
  227. void RemoveMethodMatchInfo();
  228. void ClearMethodMatchEntries();
  229. void CheckIdentifier(BfAstNode* identifierNode, bool isInExpression = false, bool isUsingDirective = false);
  230. bool CheckMemberReference(BfAstNode* target, BfAstNode* dotToken, BfAstNode* memberName, bool onlyShowTypes = false, BfType* expectingType = NULL, bool isUsingDirective = false, bool onlyAttribute = false);
  231. bool CheckExplicitInterface(BfTypeInstance* interfaceType, BfAstNode* dotToken, BfAstNode* memberName);
  232. void CheckTypeRef(BfTypeReference* typeRef, bool mayBeIdentifier, bool isInExpression = false, bool onlyAttribute = false);
  233. void CheckAttributeTypeRef(BfTypeReference* typeRef);
  234. void CheckInvocation(BfAstNode* invocationNode, BfTokenNode* openParen, BfTokenNode* closeParen, const BfSizedArray<BfTokenNode*>& commas);
  235. void CheckNode(BfAstNode* node, bool mayBeIdentifier, bool isInExpression = false);
  236. void CheckMethod(BfMethodDeclaration* methodDeclaration, bool isLocalMethod);
  237. void CheckProperty(BfPropertyDeclaration* propertyDeclaration);
  238. void CheckVarResolution(BfAstNode* varTypeRef, BfType* resolvedTypeRef);
  239. void CheckResult(BfAstNode* node, const BfTypedValue& typedValue);
  240. void CheckLocalDef(BfAstNode* identifierNode, BfLocalVariable* varDecl);
  241. void CheckLocalRef(BfAstNode* identifierNode, BfLocalVariable* varDecl);
  242. void CheckFieldRef(BfAstNode* identifierNode, BfFieldInstance* fieldInst);
  243. void CheckLabel(BfIdentifierNode* identifierNode, BfAstNode* precedingNode, BfScopeData* scopeData);
  244. void CheckNamespace(BfAstNode* node, const BfAtomComposite& namespaceName);
  245. void CheckEmptyStart(BfAstNode* prevNode, BfType* type);
  246. bool CheckFixit(BfAstNode* node);
  247. void CheckInterfaceFixit(BfTypeInstance* typeInstance, BfAstNode* node);
  248. void FixitAddMember(BfTypeInstance* typeInst, BfType* fieldType, const StringImpl& fieldName, bool isStatic, BfTypeInstance* referencedFrom);
  249. void FixitAddCase(BfTypeInstance * typeInst, const StringImpl & caseName, const BfTypeVector & fieldTypes);
  250. void FixitAddMethod(BfTypeInstance* typeInst, const StringImpl& methodName, BfType* returnType, const BfTypeVector& paramTypes, bool wantStatic);
  251. void FixitAddNamespace(BfAstNode* refNode, const StringImpl& namespacStr);
  252. void FixitCheckNamespace(BfTypeDef* activeTypeDef, BfAstNode* typeRef, BfTokenNode* nextDotToken);
  253. void FixitAddConstructor(BfTypeInstance* typeInstance);
  254. void FixitAddFullyQualify(BfAstNode* refNode, const StringImpl& findName, const SizedArrayImpl<BfUsingFieldData::MemberRef>& foundList);
  255. void AddResultTypeKind(BfType* type);
  256. void SetResultStringType(BfType* type);
  257. };
  258. NS_BF_END