Skip to content

Commit

Permalink
Correctly handle highlighting varargs in sig help
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Oct 28, 2022
1 parent 08088e4 commit a6f73dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fixed signature help highlighting of parameters named `_`
- Fixed documentation comments of parent function being attached to a nested function
- Use location to determine which parameter is active in signature help
- Correctly handle highlighting variadic arguments in signature help

## [1.12.1] - 2022-10-18

Expand Down
46 changes: 39 additions & 7 deletions src/operations/SignatureHelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,14 @@ std::optional<lsp::SignatureHelp> WorkspaceFolder::signatureHelp(const lsp::Sign
size_t idx = 0;
size_t previousParamPos = 0;

while (it != Luau::end(ftv->argTypes))
for (; it != Luau::end(ftv->argTypes); it++, idx++)
{
// If the function has self, and the caller has called as a method (i.e., :), then omit the self parameter
// TODO: hasSelf is not always specified, so we manually check for the "self" name (https://github.com/Roblox/luau/issues/551)
if (idx == 0 && (ftv->hasSelf || (ftv->argNames.size() > 0 && ftv->argNames[0].has_value() && ftv->argNames[0]->name == "self")) &&
candidate->self)
{
it++;
idx++;
continue;
}


// Show parameter documentation
// TODO: parse moonwave docs for param documentation?
Expand Down Expand Up @@ -126,8 +123,43 @@ std::optional<lsp::SignatureHelp> WorkspaceFolder::signatureHelp(const lsp::Sign
paramLabel = labelString;

parameters.push_back(lsp::ParameterInformation{paramLabel, parameterDocumentation});
it++;
idx++;
}

// Handle varargs
if (auto tp = it.tail())
{
if (auto vtp = Luau::get<Luau::VariadicTypePack>(*tp); !vtp || !vtp->hidden)
{
// Show parameter documentation
// TODO: parse moonwave docs for param documentation?
lsp::MarkupContent parameterDocumentation{lsp::MarkupKind::Markdown, ""};
if (baseDocumentationSymbol)
parameterDocumentation.value =
printDocumentation(client->documentation, *baseDocumentationSymbol + "/param/" + std::to_string(idx));

// Compute the label
// We attempt to search for the position in the string for this label, and if we don't find it,
// then we give up and just use the string label
std::variant<std::string, std::vector<size_t>> paramLabel;
std::string labelString = "...: ";

if (vtp)
labelString += Luau::toString(vtp->ty);
else
labelString += Luau::toString(*tp);

auto position = label.find(labelString, previousParamPos);
if (position != std::string::npos)
{
auto length = labelString.size();
previousParamPos = position + length;
paramLabel = std::vector{position, position + length};
}
else
paramLabel = labelString;

parameters.push_back(lsp::ParameterInformation{paramLabel, parameterDocumentation});
}
}

signatures.push_back(lsp::SignatureInformation{
Expand Down

0 comments on commit a6f73dd

Please sign in to comment.