feat: Add code completion request progress animation (#153)

This commit is contained in:
Petr Mironychev 2025-04-02 21:00:45 +02:00 committed by GitHub
parent 79218d8412
commit ffaf6bd61b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 310 additions and 1 deletions

View File

@ -101,6 +101,8 @@ add_qtc_plugin(QodeAssist
ConfigurationManager.hpp ConfigurationManager.cpp
CodeHandler.hpp CodeHandler.cpp
UpdateStatusWidget.hpp UpdateStatusWidget.cpp
widgets/CompletionProgressHandler.hpp widgets/CompletionProgressHandler.cpp
widgets/ProgressWidget.hpp widgets/ProgressWidget.cpp
)
get_target_property(QtCreatorCorePath QtCreator::Core LOCATION)

View File

@ -151,6 +151,9 @@ void QodeAssistClient::requestCompletions(TextEditor::TextEditorWidget *editor)
{TextDocumentIdentifier(hostPathToServerUri(filePath)),
documentVersion(filePath),
Position(cursor.mainCursor())}};
if (Settings::codeCompletionSettings().showProgressWidget()) {
m_progressHandler.showProgress(editor);
}
request.setResponseCallback([this, editor = QPointer<TextEditorWidget>(editor)](
const GetCompletionRequest::Response &response) {
QTC_ASSERT(editor, return);
@ -237,6 +240,7 @@ void QodeAssistClient::handleCompletions(
Text::Position pos{toTextPos(c.position())};
return TextSuggestion::Data{range, pos, c.text()};
});
m_progressHandler.hideProgress();
if (completions.isEmpty())
return;
editor->insertSuggestion(std::make_unique<LLMSuggestion>(suggestions, editor->document()));
@ -248,6 +252,7 @@ void QodeAssistClient::cancelRunningRequest(TextEditor::TextEditorWidget *editor
const auto it = m_runningRequests.constFind(editor);
if (it == m_runningRequests.constEnd())
return;
m_progressHandler.hideProgress();
cancelRequest(it->id());
m_runningRequests.erase(it);
}

View File

@ -26,6 +26,7 @@
#include "LLMClientInterface.hpp"
#include "LSPCompletion.hpp"
#include "widgets/CompletionProgressHandler.hpp"
#include <languageclient/client.h>
#include <llmcore/IPromptProvider.hpp>
#include <llmcore/IProviderRegistry.hpp>
@ -60,6 +61,7 @@ private:
QElapsedTimer m_typingTimer;
int m_recentCharCount;
CompletionProgressHandler m_progressHandler;
};
} // namespace QodeAssist

View File

@ -202,6 +202,10 @@ CodeCompletionSettings::CodeCompletionSettings()
"(space-separated), file extensions (space-separated)"));
customLanguages.setDefaultValue({{"cmake,#,cmake,CMakeLists.txt"}, {"qmake,#,qmake,pro pri"}});
showProgressWidget.setSettingsKey(Constants::CC_SHOW_PROGRESS_WIDGET);
showProgressWidget.setLabelText(Tr::tr("Show progress indicator during code completion"));
showProgressWidget.setDefaultValue(true);
useProjectChangesCache.setSettingsKey(Constants::CC_USE_PROJECT_CHANGES_CACHE);
useProjectChangesCache.setDefaultValue(true);
useProjectChangesCache.setLabelText(Tr::tr("Max Changes Cache Size:"));
@ -281,7 +285,8 @@ CodeCompletionSettings::CodeCompletionSettings()
Row{autoCompletionCharThreshold,
autoCompletionTypingInterval,
startSuggestionTimer,
Stretch{1}}}},
Stretch{1}},
showProgressWidget}},
Space{8},
Group{
title(Tr::tr("General Parameters")),

View File

@ -44,6 +44,8 @@ public:
Utils::StringListAspect customLanguages{this};
Utils::BoolAspect showProgressWidget{this};
// General Parameters Settings
Utils::DoubleAspect temperature{this};
Utils::IntegerAspect maxTokens{this};

View File

@ -55,6 +55,7 @@ const char CC_PRESET1_URL_HISTORY[] = "QodeAssist.ccPreset1UrlHistory";
// settings
const char ENABLE_QODE_ASSIST[] = "QodeAssist.enableQodeAssist";
const char CC_AUTO_COMPLETION[] = "QodeAssist.ccAutoCompletion";
const char CC_SHOW_PROGRESS_WIDGET[] = "QodeAssist.ccShowProgressWidget";
const char ENABLE_LOGGING[] = "QodeAssist.enableLogging";
const char ENABLE_CHECK_UPDATE[] = "QodeAssist.enableCheckUpdate";
const char ENABLE_CHAT[] = "QodeAssist.enableChat";

View File

@ -0,0 +1,86 @@
/*
* Copyright (C) 2025 Petr Mironychev
*
* This file is part of QodeAssist.
*
* QodeAssist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QodeAssist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
*/
#include "CompletionProgressHandler.hpp"
#include <QGraphicsBlurEffect>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QLabel>
#include <QPainter>
#include <QVBoxLayout>
#include <texteditor/texteditor.h>
#include <utils/progressindicator.h>
#include <utils/theme/theme.h>
#include <utils/tooltip/tooltip.h>
#include "ProgressWidget.hpp"
namespace QodeAssist {
void CompletionProgressHandler::showProgress(TextEditor::TextEditorWidget *widget)
{
m_widget = widget;
m_isActive = true;
if (m_widget) {
const QRect cursorRect = m_widget->cursorRect(m_widget->textCursor());
m_iconPosition = m_widget->viewport()->mapToGlobal(cursorRect.topLeft())
- Utils::ToolTip::offsetFromPosition();
identifyMatch(m_widget, m_widget->textCursor().position(), [this](auto priority) {
if (priority != Priority_None)
operateTooltip(m_widget, m_iconPosition);
});
}
}
void CompletionProgressHandler::hideProgress()
{
m_isActive = false;
Utils::ToolTip::hide();
}
void CompletionProgressHandler::identifyMatch(
TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report)
{
if (!m_isActive || !editorWidget) {
report(Priority_None);
return;
}
report(Priority_Tooltip);
}
void CompletionProgressHandler::operateTooltip(
TextEditor::TextEditorWidget *editorWidget, const QPoint &point)
{
if (!m_isActive || !editorWidget)
return;
auto progressWidget = new ProgressWidget(editorWidget);
QPoint showPoint = point;
showPoint.ry() -= progressWidget->height();
Utils::ToolTip::show(showPoint, progressWidget, editorWidget);
}
} // namespace QodeAssist

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2025 Petr Mironychev
*
* This file is part of QodeAssist.
*
* QodeAssist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QodeAssist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <texteditor/basehoverhandler.h>
#include <QPointer>
namespace QodeAssist {
class CompletionProgressHandler : public TextEditor::BaseHoverHandler
{
public:
void showProgress(TextEditor::TextEditorWidget *widget);
void hideProgress();
protected:
void identifyMatch(
TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report) override;
void operateTooltip(TextEditor::TextEditorWidget *editorWidget, const QPoint &point) override;
private:
QPointer<TextEditor::TextEditorWidget> m_widget;
QPoint m_iconPosition;
bool m_isActive = false;
};
} // namespace QodeAssist

114
widgets/ProgressWidget.cpp Normal file
View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2025 Petr Mironychev
*
* This file is part of QodeAssist.
*
* QodeAssist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QodeAssist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ProgressWidget.hpp"
namespace QodeAssist {
ProgressWidget::ProgressWidget(QWidget *parent)
: QWidget(parent)
{
m_dotPosition = 0;
m_timer.setInterval(300);
connect(&m_timer, &QTimer::timeout, this, [this]() {
m_dotPosition = (m_dotPosition + 1) % 4;
update();
});
m_timer.start();
m_textColor = Utils::creatorTheme()->color(Utils::Theme::TextColorNormal);
m_backgroundColor = Utils::creatorTheme()->color(Utils::Theme::BackgroundColorNormal);
m_logoPixmap = QPixmap(":/resources/images/qoderassist-icon.png");
if (!m_logoPixmap.isNull()) {
QImage image = m_logoPixmap.toImage();
image = image.convertToFormat(QImage::Format_ARGB32);
for (int y = 0; y < image.height(); ++y) {
for (int x = 0; x < image.width(); ++x) {
QColor pixelColor = QColor::fromRgba(image.pixel(x, y));
int brightness = (pixelColor.red() + pixelColor.green() + pixelColor.blue()) / 3;
if (brightness > 200) {
pixelColor.setAlpha(0);
image.setPixelColor(x, y, pixelColor);
} else if (pixelColor.alpha() > 0) {
int alpha = pixelColor.alpha();
pixelColor = m_textColor;
pixelColor.setAlpha(alpha);
image.setPixelColor(x, y, pixelColor);
}
}
}
m_logoPixmap = QPixmap::fromImage(image);
m_logoPixmap = m_logoPixmap.scaled(24, 24, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
setFixedSize(40, 40);
}
ProgressWidget::~ProgressWidget()
{
m_timer.stop();
}
void ProgressWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(rect(), m_backgroundColor);
if (!m_logoPixmap.isNull()) {
QRect logoRect(
(width() - m_logoPixmap.width()) / 2, 5, m_logoPixmap.width(), m_logoPixmap.height());
painter.drawPixmap(logoRect, m_logoPixmap);
}
int dotSpacing = 6;
int dotSize = 4;
int totalDotWidth = 3 * dotSize + 2 * dotSpacing;
int startX = (width() - totalDotWidth) / 2;
int dotY = height() - 8;
for (int i = 0; i < 3; ++i) {
QColor dotColor = m_textColor;
if (m_dotPosition == 0) {
dotColor.setAlpha(128);
} else {
if (i == m_dotPosition - 1) {
dotColor.setAlpha(255);
} else {
dotColor.setAlpha(80);
}
}
painter.setPen(Qt::NoPen);
painter.setBrush(dotColor);
int x = startX + i * (dotSize + dotSpacing);
painter.drawEllipse(x, dotY, dotSize, dotSize);
}
}
} // namespace QodeAssist

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2025 Petr Mironychev
*
* This file is part of QodeAssist.
*
* QodeAssist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QodeAssist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <QPainter>
#include <QTimer>
#include <QWidget>
#include <utils/progressindicator.h>
#include <utils/theme/theme.h>
namespace QodeAssist {
class ProgressWidget : public QWidget
{
public:
ProgressWidget(QWidget *parent = nullptr);
~ProgressWidget();
protected:
void paintEvent(QPaintEvent *) override;
private:
QTimer m_timer;
int m_dotPosition;
QColor m_textColor;
QColor m_backgroundColor;
QPixmap m_logoPixmap;
};
} // namespace QodeAssist