From d9f7206b43b0a04141e188445339296b2f0f13dc Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Jul 2024 16:24:42 +0200 Subject: [PATCH] Fix autocomplete end not working for non-local functions Fixes #554 --- CHANGELOG.md | 1 + src/operations/Completion.cpp | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59a0ee1d..7866357e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Fix static linking with MSVC Runtime for release binaries - Fixed clients that do not support pull diagnostics erroring with "server not yet received configuration for diagnostics" - Don't show children in autocomplete for `:GetPropertyChangedSignal("")` +- Fixed autocomplete end not working for non-local functions ## [1.31.1] - 2024-07-07 diff --git a/src/operations/Completion.cpp b/src/operations/Completion.cpp index b1aaa468..a076f55f 100644 --- a/src/operations/Completion.cpp +++ b/src/operations/Completion.cpp @@ -30,19 +30,23 @@ void WorkspaceFolder::endAutocompletion(const lsp::CompletionParams& params) if (ancestry.size() < 2) return; - // Search backwards for the first node that is not an Error node - size_t currentNodeIndex = ancestry.size() - 1; - while (ancestry.at(currentNodeIndex)->is() || ancestry.at(currentNodeIndex)->is()) - { - currentNodeIndex--; - if (currentNodeIndex < 1) - return; - } + // Remove error nodes from end of ancestry chain + while (ancestry.size() > 0 && (ancestry.back()->is() || ancestry.back()->is())) + ancestry.pop_back(); - Luau::AstNode* currentNode = ancestry.at(currentNodeIndex); - if (!currentNode) + if (ancestry.size() == 0) return; + Luau::AstNode* currentNode = ancestry.back(); + + // TODO: https://github.com/luau-lang/luau/issues/1328 causes the ast ancestry to be shorter than expected + if (auto globalFunc = currentNode->as()) + { + ancestry.push_back(globalFunc->func); + ancestry.push_back(globalFunc->func->body); + currentNode = globalFunc->func->body; + } + // We should only apply it if the line just above us is the start of the unclosed statement // Otherwise, we insert ends in weird places if theirs an unclosed stat a while away if (!currentNode->is())