Skip to content

Commit

Permalink
Propagate doc comment deprecated markers to autocomplete
Browse files Browse the repository at this point in the history
Closes #503
  • Loading branch information
JohnnyMorganz committed Nov 26, 2023
1 parent 9d40d1e commit 14242b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added

- Autocompletion items for items marked as `@deprecated` via documentation comments will now reflect their deprecated status

### Changed

- Made rename operation fully backed by find all references, to ensure both return results that are consistent with each other
Expand Down
16 changes: 14 additions & 2 deletions src/operations/Completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,18 @@ static bool canUseSnippets(const lsp::ClientCapabilities& capabilities)
capabilities.textDocument->completion->completionItem->snippetSupport;
}

static bool deprecated(const Luau::AutocompleteEntry& entry, std::optional<lsp::MarkupContent> documentation)
{
if (entry.deprecated)
return true;

if (documentation)
if (documentation->value.find("@deprecated") != std::string::npos)
return true;

return false;
}

static std::optional<lsp::CompletionItemKind> entryKind(const Luau::AutocompleteEntry& entry)
{
if (entry.type.has_value())
Expand Down Expand Up @@ -505,7 +517,7 @@ static std::optional<lsp::CompletionItemKind> entryKind(const Luau::Autocomplete
case Luau::AutocompleteEntryKind::GeneratedFunction:
return lsp::CompletionItemKind::Function;
}

return std::nullopt;
}

Expand Down Expand Up @@ -874,11 +886,11 @@ std::vector<lsp::CompletionItem> WorkspaceFolder::completion(const lsp::Completi
{
lsp::CompletionItem item;
item.label = name;
item.deprecated = entry.deprecated;

if (auto documentationString = getDocumentationForAutocompleteEntry(entry, result.ancestry, moduleName))
item.documentation = {lsp::MarkupKind::Markdown, documentationString.value()};

item.deprecated = deprecated(entry, item.documentation);
item.kind = entryKind(entry);
item.sortText = sortText(frontend, name, entry, isGetService);

Expand Down
25 changes: 23 additions & 2 deletions tests/Autocomplete.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

static std::pair<std::string, lsp::Position> sourceWithMarker(std::string source)
{
auto marker = source.find('@');
auto marker = source.find('|');
REQUIRE(marker != std::string::npos);

source.replace(marker, 1, "");
Expand Down Expand Up @@ -51,7 +51,7 @@ TEST_CASE_FIXTURE(Fixture, "function_autocomplete_has_documentation")
local function foo()
end
local x = @
local x = |
)");

auto uri = newDocument("foo.luau", source);
Expand All @@ -69,4 +69,25 @@ TEST_CASE_FIXTURE(Fixture, "function_autocomplete_has_documentation")
CHECK_EQ(item.documentation->value, "This is a function documentation comment");
}

TEST_CASE_FIXTURE(Fixture, "deprecated_marker_in_documentation_comment_applies_to_autocomplete_entry")
{
auto [source, marker] = sourceWithMarker(R"(
--- @deprecated Use `bar` instead
local function foo()
end
local x = |
)");

auto uri = newDocument("foo.luau", source);

lsp::CompletionParams params;
params.textDocument = lsp::TextDocumentIdentifier{uri};
params.position = marker;

auto result = workspace.completion(params);
auto item = getItem(result, "foo");
CHECK(item.deprecated);
}

TEST_SUITE_END();

0 comments on commit 14242b4

Please sign in to comment.