diff --git a/CMakeLists.txt b/CMakeLists.txt index 57cb91a..bf6a0ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ add_qtc_plugin(QodeAssist qodeassist.cpp QodeAssistConstants.hpp QodeAssisttr.h + CompletionProgressHandler.hpp CompletionProgressHandler.cpp LLMClientInterface.hpp LLMClientInterface.cpp templates/Templates.hpp templates/CodeLlamaFim.hpp diff --git a/CompletionProgressHandler.cpp b/CompletionProgressHandler.cpp new file mode 100644 index 0000000..8b21f1f --- /dev/null +++ b/CompletionProgressHandler.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2024 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 . + */ + +#include "CompletionProgressHandler.hpp" +#include +#include +#include + +namespace QodeAssist { + +void CompletionProgressHandler::showProgress(TextEditor::TextEditorWidget *widget) +{ + m_widget = widget; + m_isActive = true; + + if (m_widget) { + // Используем тот же метод получения позиции что и в TextSuggestion + 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 progressLabel = new QLabel; + progressLabel->setPixmap(QPixmap(":/resources/images/qoderassist-icon.png") + .scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + + QPoint showPoint = point; + showPoint.ry() -= progressLabel->sizeHint().height(); + + Utils::ToolTip::show(showPoint, progressLabel, editorWidget); +} + +} // namespace QodeAssist diff --git a/CompletionProgressHandler.hpp b/CompletionProgressHandler.hpp new file mode 100644 index 0000000..85948fb --- /dev/null +++ b/CompletionProgressHandler.hpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2024 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 . + */ + +#pragma once + +#include +#include + +namespace QodeAssist { + +class CompletionProgressHandler : public TextEditor::BaseHoverHandler +{ +public: + void showProgress(TextEditor::TextEditorWidget *widget); + void hideProgress(); + void abort() override {} + +protected: + void identifyMatch( + TextEditor::TextEditorWidget *editorWidget, int pos, ReportPriority report) override; + void operateTooltip(TextEditor::TextEditorWidget *editorWidget, const QPoint &point) override; + +private: + QPointer m_widget; + QPoint m_iconPosition; + bool m_isActive = false; +}; + +} // namespace QodeAssist diff --git a/QodeAssistClient.cpp b/QodeAssistClient.cpp index c3446c0..0841389 100644 --- a/QodeAssistClient.cpp +++ b/QodeAssistClient.cpp @@ -151,6 +151,7 @@ void QodeAssistClient::requestCompletions(TextEditor::TextEditorWidget *editor) {TextDocumentIdentifier(hostPathToServerUri(filePath)), documentVersion(filePath), Position(cursor.mainCursor())}}; + m_progressHandler.showProgress(editor); request.setResponseCallback([this, editor = QPointer(editor)]( const GetCompletionRequest::Response &response) { QTC_ASSERT(editor, return); @@ -237,6 +238,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(suggestions, editor->document())); @@ -248,6 +250,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); } diff --git a/QodeAssistClient.hpp b/QodeAssistClient.hpp index 0e8260a..330021f 100644 --- a/QodeAssistClient.hpp +++ b/QodeAssistClient.hpp @@ -24,6 +24,7 @@ #pragma once +#include "CompletionProgressHandler.hpp" #include "LLMClientInterface.hpp" #include "LSPCompletion.hpp" #include @@ -60,6 +61,7 @@ private: QElapsedTimer m_typingTimer; int m_recentCharCount; + CompletionProgressHandler m_progressHandler; }; } // namespace QodeAssist