Skip to content

Commit

Permalink
Ensure service imports are always after hot comment
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Mar 23, 2023
1 parent fa1ab99 commit dba3089
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed

- Fixed server crash when auto require imports is enabled and there is a type-asserted require present in the file (`require(location) :: any`)
- Fixed additional automatic service imports when completing an automatic require import being placed before a hot comment (such as `--!strict`)

## [1.18.0] - 2023-03-20

Expand Down
13 changes: 8 additions & 5 deletions src/include/LSP/LuauExt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,12 @@ struct FindServicesVisitor : public Luau::AstVisitor
std::optional<size_t> lastServiceDefinitionLine = std::nullopt;
std::map<std::string, Luau::AstStatLocal*> serviceLineMap{};

size_t findBestLine(const std::string& serviceName, std::optional<size_t> minimumLineNumber = std::nullopt)
size_t findBestLine(const std::string& serviceName, size_t minimumLineNumber)
{
size_t lineNumber = minimumLineNumber.value_or(firstServiceDefinitionLine.value_or(0));
if (firstServiceDefinitionLine)
minimumLineNumber = *firstServiceDefinitionLine > minimumLineNumber ? *firstServiceDefinitionLine : minimumLineNumber;

size_t lineNumber = minimumLineNumber;
for (auto& [definedService, stat] : serviceLineMap)
{
auto location = stat->location.begin.line;
Expand Down Expand Up @@ -180,7 +183,8 @@ struct FindRequiresVisitor : public Luau::AstVisitor

bool contains(const std::string& module)
{
for (auto& map : requiresMap) {
for (auto& map : requiresMap)
{
if (map.find(module) != map.end())
return true;
}
Expand All @@ -202,8 +206,7 @@ struct FindRequiresVisitor : public Luau::AstVisitor

if (isRequire(expr))
{
firstRequireLine =
!firstRequireLine.has_value() || firstRequireLine.value() >= line ? line : firstRequireLine.value();
firstRequireLine = !firstRequireLine.has_value() || firstRequireLine.value() >= line ? line : firstRequireLine.value();


// If the requires are too many lines away, treat it as a new group
Expand Down
16 changes: 6 additions & 10 deletions src/operations/Completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,34 +285,29 @@ void WorkspaceFolder::suggestImports(const Luau::ModuleName& moduleName, const L
return;

// Place after any hot comments
size_t minimumLineNumber = 0;
size_t hotCommentsLineNumber = 0;
for (const auto& hotComment : sourceModule->hotcomments)
{
if (!hotComment.header)
continue;
if (hotComment.location.begin.line >= minimumLineNumber)
minimumLineNumber = hotComment.location.begin.line + 1U;
if (hotComment.location.begin.line >= hotCommentsLineNumber)
hotCommentsLineNumber = hotComment.location.begin.line + 1U;
}


FindServicesVisitor serviceVisitor;
serviceVisitor.visit(sourceModule->root);

// If in roblox mode - suggest services
if (config.types.roblox)
{
if (serviceVisitor.firstServiceDefinitionLine)
minimumLineNumber =
*serviceVisitor.firstServiceDefinitionLine > minimumLineNumber ? *serviceVisitor.firstServiceDefinitionLine : minimumLineNumber;

auto services = getServiceNames(frontend.globalsForAutocomplete.globalScope);
for (auto& service : services)
{
// ASSUMPTION: if the service was defined, it was defined with the exact same name
if (serviceVisitor.serviceLineMap.find(service) != serviceVisitor.serviceLineMap.end())
continue;

size_t lineNumber = serviceVisitor.findBestLine(service, minimumLineNumber);
size_t lineNumber = serviceVisitor.findBestLine(service, hotCommentsLineNumber);
result.emplace_back(createSuggestService(service, lineNumber));
}
}
Expand All @@ -321,6 +316,7 @@ void WorkspaceFolder::suggestImports(const Luau::ModuleName& moduleName, const L
FindRequiresVisitor visitor;
visitor.visit(sourceModule->root);

size_t minimumLineNumber = hotCommentsLineNumber;
if (serviceVisitor.lastServiceDefinitionLine)
minimumLineNumber =
*serviceVisitor.lastServiceDefinitionLine >= minimumLineNumber ? (*serviceVisitor.lastServiceDefinitionLine + 1) : minimumLineNumber;
Expand Down Expand Up @@ -387,7 +383,7 @@ void WorkspaceFolder::suggestImports(const Luau::ModuleName& moduleName, const L
if (serviceVisitor.serviceLineMap.find(service) == serviceVisitor.serviceLineMap.end())
{
// If we haven't imported the service, then we auto-import it
textEdits.emplace_back(createServiceTextEdit(service, serviceVisitor.findBestLine(service)));
textEdits.emplace_back(createServiceTextEdit(service, serviceVisitor.findBestLine(service, hotCommentsLineNumber)));

// Increment the require line number to account for the new service import
lineNumber += 1;
Expand Down

0 comments on commit dba3089

Please sign in to comment.