Path: blob/master/Analysis/include/Luau/FragmentAutocomplete.h
2727 views
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details1#pragma once23#include "Luau/Ast.h"4#include "Luau/Parser.h"5#include "Luau/AutocompleteTypes.h"6#include "Luau/DenseHash.h"7#include "Luau/Module.h"8#include "Luau/Frontend.h"910#include <memory>11#include <vector>1213namespace Luau14{15struct FrontendOptions;1617enum class FragmentAutocompleteWaypoint18{19ParseFragmentEnd,20CloneModuleStart,21CloneModuleEnd,22DfgBuildEnd,23CloneAndSquashScopeStart,24CloneAndSquashScopeEnd,25ConstraintSolverStart,26ConstraintSolverEnd,27TypecheckFragmentEnd,28AutocompleteEnd,29COUNT,30};3132class IFragmentAutocompleteReporter33{34public:35virtual void reportWaypoint(FragmentAutocompleteWaypoint) = 0;36virtual void reportFragmentString(std::string_view) = 0;37};3839enum class FragmentTypeCheckStatus40{41SkipAutocomplete,42Success,43};4445struct FragmentAutocompleteAncestryResult46{47DenseHashMap<AstName, AstLocal*> localMap{AstName()};48std::vector<AstLocal*> localStack;49std::vector<AstNode*> ancestry;50AstStat* nearestStatement = nullptr;51AstStatBlock* parentBlock = nullptr;52Location fragmentSelectionRegion;53};5455struct FragmentParseResult56{57std::string fragmentToParse;58AstStatBlock* root = nullptr;59std::vector<AstNode*> ancestry;60AstStat* nearestStatement = nullptr;61std::vector<Comment> commentLocations;62std::unique_ptr<Allocator> alloc = std::make_unique<Allocator>();63Position scopePos{0, 0};64};6566struct FragmentTypeCheckResult67{68ModulePtr incrementalModule = nullptr;69ScopePtr freshScope;70std::vector<AstNode*> ancestry;71};7273struct FragmentAutocompleteResult74{75ModulePtr incrementalModule;76Scope* freshScope;77AutocompleteResult acResults;78};7980struct FragmentRegion81{82Location fragmentLocation;83AstStat* nearestStatement = nullptr; // used for tests84AstStatBlock* parentBlock = nullptr; // used for scope detection85};8687std::optional<Position> blockDiffStart(AstStatBlock* blockOld, AstStatBlock* blockNew, AstStat* nearestStatementNewAst);88FragmentRegion getFragmentRegion(AstStatBlock* root, const Position& cursorPosition);89FragmentAutocompleteAncestryResult findAncestryForFragmentParse(AstStatBlock* stale, const Position& cursorPos, AstStatBlock* lastGoodParse);90FragmentAutocompleteAncestryResult findAncestryForFragmentParse_DEPRECATED(AstStatBlock* root, const Position& cursorPos);9192std::optional<FragmentParseResult> parseFragment_DEPRECATED(93AstStatBlock* root,94AstNameTable* names,95std::string_view src,96const Position& cursorPos,97std::optional<Position> fragmentEndPosition98);99100std::optional<FragmentParseResult> parseFragment(101AstStatBlock* stale,102AstStatBlock* mostRecentParse,103AstNameTable* names,104std::string_view src,105const Position& cursorPos,106std::optional<Position> fragmentEndPosition107);108109std::pair<FragmentTypeCheckStatus, FragmentTypeCheckResult> typecheckFragment(110Frontend& frontend,111const ModuleName& moduleName,112const Position& cursorPos,113std::optional<FrontendOptions> opts,114std::string_view src,115std::optional<Position> fragmentEndPosition,116AstStatBlock* recentParse = nullptr,117IFragmentAutocompleteReporter* reporter = nullptr118);119120FragmentAutocompleteResult fragmentAutocomplete(121Frontend& frontend,122std::string_view src,123const ModuleName& moduleName,124Position cursorPosition,125std::optional<FrontendOptions> opts,126StringCompletionCallback callback,127std::optional<Position> fragmentEndPosition = std::nullopt,128AstStatBlock* recentParse = nullptr,129IFragmentAutocompleteReporter* reporter = nullptr,130bool isInHotComment = false131);132133enum class FragmentAutocompleteStatus134{135Success,136FragmentTypeCheckFail,137InternalIce138};139140struct FragmentAutocompleteStatusResult141{142FragmentAutocompleteStatus status;143std::optional<FragmentAutocompleteResult> result;144};145146struct FragmentContext147{148std::string_view newSrc;149const ParseResult& freshParse;150std::optional<FrontendOptions> opts;151std::optional<Position> DEPRECATED_fragmentEndPosition;152IFragmentAutocompleteReporter* reporter = nullptr;153};154155/**156* @brief Attempts to compute autocomplete suggestions from the fragment context.157*158* This function computes autocomplete suggestions using outdated frontend typechecking data159* by patching the fragment context of the new script source content.160*161* @param frontend The Luau Frontend data structure, which may contain outdated typechecking data.162*163* @param moduleName The name of the target module, specifying which script the caller wants to request autocomplete for.164*165* @param cursorPosition The position in the script where the caller wants to trigger autocomplete.166*167* @param context The fragment context that this API will use to patch the outdated typechecking data.168*169* @param stringCompletionCB A callback function that provides autocomplete suggestions for string contexts.170*171* @return172* The status indicating whether `fragmentAutocomplete` ran successfully or failed, along with the reason for failure.173* Also includes autocomplete suggestions if the status is successful.174*175* @usage176* FragmentAutocompleteStatusResult acStatusResult;177* if (shouldFragmentAC)178* acStatusResult = Luau::tryFragmentAutocomplete(...);179*180* if (acStatusResult.status != Successful)181* {182* frontend.check(moduleName, options);183* acStatusResult.acResult = Luau::autocomplete(...);184* }185* return convertResultWithContext(acStatusResult.acResult);186*/187FragmentAutocompleteStatusResult tryFragmentAutocomplete(188Frontend& frontend,189const ModuleName& moduleName,190Position cursorPosition,191FragmentContext context,192StringCompletionCallback stringCompletionCB193);194195} // namespace Luau196197198