Skip to content

Commit

Permalink
Snip old code for intersection-based Instance.new/GetService
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Sep 17, 2023
1 parent dc3a0df commit 1ae1522
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
41 changes: 11 additions & 30 deletions src/LuauExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,46 +576,27 @@ Luau::LoadDefinitionFileResult registerDefinitions(Luau::Frontend& frontend, Lua
}

// Attach onto Instance.new()
if (auto instanceGlobal = globals.globalScope->lookup(Luau::AstName("Instance")))
if (auto ttv = Luau::get<Luau::TableType>(instanceGlobal.value()))
if (auto newFunction = ttv->props.find("new"); newFunction != ttv->props.end())
{
if (metadata.has_value() && !metadata->CREATABLE_INSTANCES.empty() && Luau::get<Luau::FunctionType>(newFunction->second.type()))
if (metadata.has_value() && !metadata->CREATABLE_INSTANCES.empty())
if (auto instanceGlobal = globals.globalScope->lookup(Luau::AstName("Instance")))
if (auto ttv = Luau::get<Luau::TableType>(instanceGlobal.value()))
if (auto newFunction = ttv->props.find("new");
newFunction != ttv->props.end() && Luau::get<Luau::FunctionType>(newFunction->second.type()))
{

Luau::attachTag(newFunction->second.type(), "CreatableInstances");
Luau::attachMagicFunction(
newFunction->second.type(), createMagicFunctionTypeLookup(metadata->CREATABLE_INSTANCES, "Invalid class name"));
}
else
{
// TODO: snip old code and move metadata check above
if (auto itv = Luau::get<Luau::IntersectionType>(newFunction->second.type()))
for (auto& part : itv->parts)
if (auto ftv = Luau::get<Luau::FunctionType>(part))
if (auto it = Luau::begin(ftv->argTypes); it != Luau::end(ftv->argTypes))
if (Luau::isPrim(*it, Luau::PrimitiveType::String))
Luau::attachMagicFunction(part, magicFunctionInstanceNew);
}
}

// Attach onto `game:GetService()`
if (auto serviceProviderType = globals.globalScope->lookupType("ServiceProvider"))
if (auto* ctv = Luau::getMutable<Luau::ClassType>(serviceProviderType->type))
{
if (metadata.has_value() && !metadata->CREATABLE_INSTANCES.empty() && Luau::get<Luau::FunctionType>(ctv->props["GetService"].type()))
if (metadata.has_value() && !metadata->SERVICES.empty())
if (auto serviceProviderType = globals.globalScope->lookupType("ServiceProvider"))
if (auto* ctv = Luau::getMutable<Luau::ClassType>(serviceProviderType->type);
ctv && Luau::get<Luau::FunctionType>(ctv->props["GetService"].type()))
{
Luau::attachTag(ctv->props["GetService"].type(), "Services");
Luau::attachMagicFunction(ctv->props["GetService"].type(), createMagicFunctionTypeLookup(metadata->SERVICES, "Invalid service name"));
}
else
{
// TODO: snip old code and move metadata check above
// Mark `game:GetService()` with a tag so we can prioritise services when autocompleting
// :GetService is an intersection of function types, so we assign a tag on the first intersection
if (auto* itv = Luau::getMutable<Luau::IntersectionType>(ctv->props["GetService"].type()); itv && !itv->parts.empty())
Luau::attachTag(itv->parts[0], "PrioritiseCommonServices");
}
}

// Move Enums over as imported type bindings
std::unordered_map<Luau::Name, Luau::TypeFun> enumTypes{};
Expand Down Expand Up @@ -1315,4 +1296,4 @@ bool isRequire(const Luau::AstExpr* expr)
}

return false;
}
}
7 changes: 0 additions & 7 deletions src/operations/Completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,6 @@ std::vector<lsp::CompletionItem> WorkspaceFolder::completion(const lsp::Completi

return result;
}
else if (tag == "PrioritiseCommonServices")
{
// TODO: snip old code
// We are autocompleting a `game:GetService("$1")` call, so we set a flag to
// highlight this so that we can prioritise common services first in the list
isGetService = true;
}

return std::nullopt;
});
Expand Down

0 comments on commit 1ae1522

Please sign in to comment.