Skip to content

Commit

Permalink
Parse documentation in doc file classes
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Dec 25, 2023
1 parent ecd199c commit 9eece40
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Added configuration `luau-lsp.bytecode.vectorLib`, `luau-lsp.bytecode.vectorCtor` and `luau-lsp.bytecode.vectorType` to configure compiler options when generating bytecode
- Custom editors should handle the `luau-lsp/bytecode` and `luau-lsp/compilerRemarks` LSP message to integrate compiler remarks info in their editor
- Added `luau-lsp.types.robloxSecurityLevel` to select what security level to use for the API types, out of: `None`, `LocalUserSecurity`, `PluginSecurity` and `RobloxScriptSecurity`
- Added support for documentation in definitions files

### Changed

Expand Down
47 changes: 39 additions & 8 deletions src/DocumentationParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,26 @@ struct AttachCommentsVisitor : public Luau::AstVisitor
return false;
}

bool visit(Luau::AstStatDeclareClass* klass) override
{
if (klass->location.begin >= pos)
return false;
if (klass->location.begin > closestPreviousNode)
closestPreviousNode = klass->location.begin;

for (const auto& item : klass->props)
{
if (item.ty->location.begin >= pos)
continue;
closestPreviousNode = std::max(closestPreviousNode, item.ty->location.begin);
item.ty->visit(this);
if (item.ty->location.end <= pos)
closestPreviousNode = std::max(closestPreviousNode, item.ty->location.end);
}

return false;
}

bool visit(Luau::AstStatBlock* block) override
{
// If the position is after the block, then it can be ignored
Expand Down Expand Up @@ -372,19 +392,30 @@ std::vector<Luau::Comment> getCommentLocations(const Luau::SourceModule* module,
/// Performs transformations so that the comments are normalised to lines inside of it (i.e., trimming whitespace, removing comment start/end)
std::vector<std::string> WorkspaceFolder::getComments(const Luau::ModuleName& moduleName, const Luau::Location& node)
{
auto sourceModule = frontend.getSourceModule(moduleName);
if (!sourceModule)
return {};
Luau::SourceModule* sourceModule;
TextDocumentPtr textDocument(nullptr);

if (auto sm = definitionsSourceModules.find(moduleName); sm != definitionsSourceModules.end())
{
sourceModule = &sm->second.second;
textDocument = TextDocumentPtr(&sm->second.first);
}
else
{
sourceModule = frontend.getSourceModule(moduleName);
if (!sourceModule)
return {};

// Get relevant text document
textDocument = fileResolver.getOrCreateTextDocumentFromModuleName(moduleName);
if (!textDocument)
return {};
}

auto commentLocations = getCommentLocations(sourceModule, node);
if (commentLocations.empty())
return {};

// Get relevant text document
auto textDocument = fileResolver.getOrCreateTextDocumentFromModuleName(moduleName);
if (!textDocument)
return {};

std::vector<std::string> comments{};
for (auto& comment : commentLocations)
{
Expand Down

0 comments on commit 9eece40

Please sign in to comment.