Copilot: remove trailing whitespaces of completion text

The Copilot language server likes to include trailing new lines in the
completion items. This can move the whole editor content below the
suggestion needlesly. Additionally the new lines are not always
sensible (https://www.youtube.com/watch?v=kQzpvHPeBQo&t=402s).

Change-Id: I75a4a0f2d30a7c757361851dae5c0265777dfea9
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
David Schulz
2023-06-14 14:44:49 +02:00
parent ea8182016b
commit c617c2afa1
2 changed files with 18 additions and 1 deletions

View File

@@ -183,8 +183,24 @@ void CopilotClient::handleCompletions(const GetCompletionRequest::Response &resp
auto isValidCompletion = [](const Completion &completion) {
return completion.isValid() && !completion.text().trimmed().isEmpty();
};
const QList<Completion> completions = Utils::filtered(result->completions().toListOrEmpty(),
QList<Completion> completions = Utils::filtered(result->completions().toListOrEmpty(),
isValidCompletion);
// remove trailing whitespaces from the end of the completions
for (Completion &completion : completions) {
const LanguageServerProtocol::Range range = completion.range();
if (range.start().line() != range.end().line())
continue; // do not remove trailing whitespaces for multi-line replacements
const QString completionText = completion.text();
const int end = int(completionText.size()) - 1; // empty strings have been removed above
int delta = 0;
while (delta <= end && completionText[end - delta].isSpace())
++delta;
if (delta > 0)
completion.setText(completionText.chopped(delta));
}
if (completions.isEmpty())
return;
editor->insertSuggestion(

View File

@@ -27,6 +27,7 @@ public:
return typedValue<LanguageServerProtocol::Range>(LanguageServerProtocol::rangeKey);
}
QString text() const { return typedValue<QString>(LanguageServerProtocol::textKey); }
void setText(const QString &text) { insert(LanguageServerProtocol::textKey, text); }
QString uuid() const { return typedValue<QString>(uuidKey); }
bool isValid() const override