// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details #include "Luau/FragmentAutocomplete.h" #include "Luau/Ast.h" #include "Luau/AstQuery.h" namespace Luau { FragmentAutocompleteAncestryResult findAncestryForFragmentParse(AstStatBlock* root, const Position& cursorPos) { std::vector ancestry = findAncestryAtPositionForAutocomplete(root, cursorPos); DenseHashMap localMap{AstName()}; std::vector localStack; AstStat* nearestStatement = nullptr; for (AstNode* node : ancestry) { if (auto block = node->as()) { for (auto stat : block->body) { if (stat->location.begin <= cursorPos) nearestStatement = stat; if (stat->location.begin <= cursorPos) { // This statement precedes the current one if (auto loc = stat->as()) { for (auto v : loc->vars) { localStack.push_back(v); localMap[v->name] = v; } } else if (auto locFun = stat->as()) { localStack.push_back(locFun->name); localMap[locFun->name->name] = locFun->name; } } } } } return {std::move(localMap), std::move(localStack), std::move(ancestry), std::move(nearestStatement)}; } } // namespace Luau