mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-06-04 01:28:58 -04:00
Add multiline insert support
This commit is contained in:
parent
4d9adf75ff
commit
753365ea52
@ -45,9 +45,9 @@ add_qtc_plugin(QodeAssist
|
|||||||
QodeAssist.qrc
|
QodeAssist.qrc
|
||||||
LSPCompletion.hpp
|
LSPCompletion.hpp
|
||||||
LLMSuggestion.hpp LLMSuggestion.cpp
|
LLMSuggestion.hpp LLMSuggestion.cpp
|
||||||
QodeAssistHoverHandler.hpp QodeAssistHoverHandler.cpp
|
|
||||||
QodeAssistClient.hpp QodeAssistClient.cpp
|
QodeAssistClient.hpp QodeAssistClient.cpp
|
||||||
QodeAssistUtils.hpp
|
QodeAssistUtils.hpp
|
||||||
DocumentContextReader.hpp DocumentContextReader.cpp
|
DocumentContextReader.hpp DocumentContextReader.cpp
|
||||||
QodeAssistData.hpp
|
QodeAssistData.hpp
|
||||||
|
utils/CounterTooltip.hpp utils/CounterTooltip.cpp
|
||||||
)
|
)
|
||||||
|
@ -19,13 +19,17 @@
|
|||||||
|
|
||||||
#include "LLMSuggestion.hpp"
|
#include "LLMSuggestion.hpp"
|
||||||
|
|
||||||
|
#include <QTextCursor>
|
||||||
|
#include <QtWidgets/qtoolbar.h>
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
#include <utils/stringutils.h>
|
#include <utils/stringutils.h>
|
||||||
|
#include <utils/tooltip/tooltip.h>
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
LLMSuggestion::LLMSuggestion(const Completion &completion, QTextDocument *origin)
|
LLMSuggestion::LLMSuggestion(const Completion &completion, QTextDocument *origin)
|
||||||
: m_completion(completion)
|
: m_completion(completion)
|
||||||
|
, m_linesCount(0)
|
||||||
{
|
{
|
||||||
int startPos = completion.range().start().toPositionInDocument(origin);
|
int startPos = completion.range().start().toPositionInDocument(origin);
|
||||||
int endPos = completion.range().end().toPositionInDocument(origin);
|
int endPos = completion.range().end().toPositionInDocument(origin);
|
||||||
@ -71,24 +75,30 @@ bool LLMSuggestion::applyWord(TextEditor::TextEditorWidget *widget)
|
|||||||
|
|
||||||
bool LLMSuggestion::applyNextLine(TextEditor::TextEditorWidget *widget)
|
bool LLMSuggestion::applyNextLine(TextEditor::TextEditorWidget *widget)
|
||||||
{
|
{
|
||||||
QTextCursor currentCursor = widget->textCursor();
|
|
||||||
const QString text = m_completion.text();
|
const QString text = m_completion.text();
|
||||||
|
QStringList lines = text.split('\n');
|
||||||
|
|
||||||
int endPos = currentCursor.position();
|
if (m_linesCount < lines.size())
|
||||||
while (endPos < text.length() && !text[endPos].isSpace()) {
|
m_linesCount++;
|
||||||
++endPos;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString wordToInsert = text.left(endPos);
|
showTooltip(widget, m_linesCount);
|
||||||
|
|
||||||
wordToInsert = wordToInsert.split('\n').first();
|
return m_linesCount == lines.size() && !Utils::ToolTip::isVisible();
|
||||||
|
}
|
||||||
|
|
||||||
if (!wordToInsert.isEmpty()) {
|
void LLMSuggestion::onCounterFinished(int count)
|
||||||
currentCursor.insertText(wordToInsert);
|
{
|
||||||
widget->setTextCursor(currentCursor);
|
Utils::ToolTip::hide();
|
||||||
}
|
m_linesCount = 0;
|
||||||
|
QTextCursor cursor = m_completion.range().toSelection(m_start.document());
|
||||||
|
cursor.beginEditBlock();
|
||||||
|
cursor.removeSelectedText();
|
||||||
|
|
||||||
return false;
|
QStringList lines = m_completion.text().split('\n');
|
||||||
|
QString textToInsert = lines.mid(0, count).join('\n');
|
||||||
|
|
||||||
|
cursor.insertText(textToInsert);
|
||||||
|
cursor.endEditBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LLMSuggestion::reset()
|
void LLMSuggestion::reset()
|
||||||
@ -101,4 +111,14 @@ int LLMSuggestion::position()
|
|||||||
return m_start.position();
|
return m_start.position();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LLMSuggestion::showTooltip(TextEditor::TextEditorWidget *widget, int count)
|
||||||
|
{
|
||||||
|
Utils::ToolTip::hide();
|
||||||
|
QPoint pos = widget->mapToGlobal(widget->cursorRect().topRight());
|
||||||
|
pos += QPoint(-10, -50);
|
||||||
|
m_counterTooltip = new CounterTooltip(count);
|
||||||
|
Utils::ToolTip::show(pos, m_counterTooltip, widget);
|
||||||
|
connect(m_counterTooltip, &CounterTooltip::finished, this, &LLMSuggestion::onCounterFinished);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist
|
} // namespace QodeAssist
|
||||||
|
@ -19,14 +19,17 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include "LSPCompletion.hpp"
|
||||||
#include <texteditor/textdocumentlayout.h>
|
#include <texteditor/textdocumentlayout.h>
|
||||||
|
|
||||||
#include "LSPCompletion.hpp"
|
#include "utils/CounterTooltip.hpp"
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
class LLMSuggestion final : public TextEditor::TextSuggestion
|
class LLMSuggestion final : public QObject, public TextEditor::TextSuggestion
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
LLMSuggestion(const Completion &completion, QTextDocument *origin);
|
LLMSuggestion(const Completion &completion, QTextDocument *origin);
|
||||||
|
|
||||||
@ -38,9 +41,15 @@ public:
|
|||||||
|
|
||||||
const Completion &completion() const { return m_completion; }
|
const Completion &completion() const { return m_completion; }
|
||||||
|
|
||||||
|
void showTooltip(TextEditor::TextEditorWidget *widget, int count);
|
||||||
|
void onCounterFinished(int count);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Completion m_completion;
|
Completion m_completion;
|
||||||
QTextCursor m_start;
|
QTextCursor m_start;
|
||||||
|
int m_linesCount;
|
||||||
|
|
||||||
|
CounterTooltip *m_counterTooltip = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist
|
} // namespace QodeAssist
|
||||||
|
@ -43,6 +43,10 @@ public:
|
|||||||
{
|
{
|
||||||
return typedValue<LanguageServerProtocol::Position>(LanguageServerProtocol::positionKey);
|
return typedValue<LanguageServerProtocol::Position>(LanguageServerProtocol::positionKey);
|
||||||
}
|
}
|
||||||
|
void setRange(const LanguageServerProtocol::Range &range)
|
||||||
|
{
|
||||||
|
insert(LanguageServerProtocol::rangeKey, range);
|
||||||
|
}
|
||||||
LanguageServerProtocol::Range range() const
|
LanguageServerProtocol::Range range() const
|
||||||
{
|
{
|
||||||
return typedValue<LanguageServerProtocol::Range>(LanguageServerProtocol::rangeKey);
|
return typedValue<LanguageServerProtocol::Range>(LanguageServerProtocol::rangeKey);
|
||||||
|
@ -190,7 +190,6 @@ void QodeAssistClient::handleCompletions(const GetCompletionRequest::Response &r
|
|||||||
return;
|
return;
|
||||||
editor->insertSuggestion(
|
editor->insertSuggestion(
|
||||||
std::make_unique<LLMSuggestion>(completions.first(), editor->document()));
|
std::make_unique<LLMSuggestion>(completions.first(), editor->document()));
|
||||||
editor->addHoverHandler(&m_hoverHandler);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,11 +236,6 @@ void QodeAssistClient::cleanupConnections()
|
|||||||
disconnect(m_documentOpenedConnection);
|
disconnect(m_documentOpenedConnection);
|
||||||
disconnect(m_documentClosedConnection);
|
disconnect(m_documentClosedConnection);
|
||||||
|
|
||||||
for (IEditor *editor : DocumentModel::editorsForOpenedDocuments()) {
|
|
||||||
if (auto textEditor = qobject_cast<BaseTextEditor *>(editor))
|
|
||||||
textEditor->editorWidget()->removeHoverHandler(&m_hoverHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
qDeleteAll(m_scheduledRequests);
|
qDeleteAll(m_scheduledRequests);
|
||||||
m_scheduledRequests.clear();
|
m_scheduledRequests.clear();
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
#include <languageclient/client.h>
|
#include <languageclient/client.h>
|
||||||
|
|
||||||
#include "LSPCompletion.hpp"
|
#include "LSPCompletion.hpp"
|
||||||
#include "QodeAssistHoverHandler.hpp"
|
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
@ -54,7 +53,6 @@ private:
|
|||||||
|
|
||||||
QHash<TextEditor::TextEditorWidget *, GetCompletionRequest> m_runningRequests;
|
QHash<TextEditor::TextEditorWidget *, GetCompletionRequest> m_runningRequests;
|
||||||
QHash<TextEditor::TextEditorWidget *, QTimer *> m_scheduledRequests;
|
QHash<TextEditor::TextEditorWidget *, QTimer *> m_scheduledRequests;
|
||||||
QodeAssistHoverHandler m_hoverHandler;
|
|
||||||
QMetaObject::Connection m_documentOpenedConnection;
|
QMetaObject::Connection m_documentOpenedConnection;
|
||||||
QMetaObject::Connection m_documentClosedConnection;
|
QMetaObject::Connection m_documentClosedConnection;
|
||||||
};
|
};
|
||||||
|
@ -1,128 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2023 The Qt Company Ltd.
|
|
||||||
* Copyright (C) 2024 Petr Mironychev
|
|
||||||
*
|
|
||||||
* This file is part of Qode Assist.
|
|
||||||
*
|
|
||||||
* The Qt Company portions:
|
|
||||||
* SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
|
||||||
*
|
|
||||||
* Petr Mironychev portions:
|
|
||||||
* 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 "QodeAssistHoverHandler.hpp"
|
|
||||||
|
|
||||||
#include <QPushButton>
|
|
||||||
#include <QScopeGuard>
|
|
||||||
#include <QToolBar>
|
|
||||||
#include <QToolButton>
|
|
||||||
|
|
||||||
#include <texteditor/textdocument.h>
|
|
||||||
#include <texteditor/textdocumentlayout.h>
|
|
||||||
#include <texteditor/texteditor.h>
|
|
||||||
|
|
||||||
#include <utils/tooltip/tooltip.h>
|
|
||||||
#include <utils/utilsicons.h>
|
|
||||||
|
|
||||||
#include "LLMSuggestion.hpp"
|
|
||||||
#include "LSPCompletion.hpp"
|
|
||||||
#include "QodeAssisttr.h"
|
|
||||||
|
|
||||||
using namespace TextEditor;
|
|
||||||
using namespace LanguageServerProtocol;
|
|
||||||
using namespace Utils;
|
|
||||||
|
|
||||||
namespace QodeAssist {
|
|
||||||
|
|
||||||
class QodeAssistCompletionToolTip : public QToolBar
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
QodeAssistCompletionToolTip(TextEditorWidget *editor)
|
|
||||||
: m_editor(editor)
|
|
||||||
{
|
|
||||||
auto apply = addAction(Tr::tr("Apply (%1)").arg(QKeySequence(Qt::Key_Tab).toString()));
|
|
||||||
connect(apply, &QAction::triggered, this, &QodeAssistCompletionToolTip::apply);
|
|
||||||
|
|
||||||
auto applyWord = addAction(Tr::tr("Apply Next Line (%1)")
|
|
||||||
.arg(QKeySequence(QKeySequence::MoveToNextLine).toString()));
|
|
||||||
connect(applyWord, &QAction::triggered, this, &QodeAssistCompletionToolTip::applyWord);
|
|
||||||
qDebug() << "applyWord sequence" << applyWord->shortcut();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void apply()
|
|
||||||
{
|
|
||||||
if (auto *suggestion = dynamic_cast<LLMSuggestion *>(m_editor->currentSuggestion())) {
|
|
||||||
if (!suggestion->apply())
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ToolTip::hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
void applyWord()
|
|
||||||
{
|
|
||||||
if (auto *suggestion = dynamic_cast<LLMSuggestion *>(m_editor->currentSuggestion())) {
|
|
||||||
if (!suggestion->applyWord(m_editor))
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ToolTip::hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
TextEditorWidget *m_editor;
|
|
||||||
};
|
|
||||||
|
|
||||||
void QodeAssistHoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget,
|
|
||||||
int pos,
|
|
||||||
ReportPriority report)
|
|
||||||
{
|
|
||||||
QScopeGuard cleanup([&] { report(Priority_None); });
|
|
||||||
if (!editorWidget->suggestionVisible())
|
|
||||||
return;
|
|
||||||
|
|
||||||
QTextCursor cursor(editorWidget->document());
|
|
||||||
cursor.setPosition(pos);
|
|
||||||
m_block = cursor.block();
|
|
||||||
auto *suggestion = dynamic_cast<LLMSuggestion *>(TextDocumentLayout::suggestion(m_block));
|
|
||||||
|
|
||||||
if (!suggestion)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const Completion completion = suggestion->completion();
|
|
||||||
if (completion.text().isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
cleanup.dismiss();
|
|
||||||
report(Priority_Suggestion);
|
|
||||||
}
|
|
||||||
|
|
||||||
void QodeAssistHoverHandler::operateTooltip(TextEditor::TextEditorWidget *editorWidget,
|
|
||||||
const QPoint &point)
|
|
||||||
{
|
|
||||||
Q_UNUSED(point)
|
|
||||||
auto *suggestion = dynamic_cast<LLMSuggestion *>(TextDocumentLayout::suggestion(m_block));
|
|
||||||
|
|
||||||
if (!suggestion)
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto tooltipWidget = new QodeAssistCompletionToolTip(editorWidget);
|
|
||||||
|
|
||||||
const QRect cursorRect = editorWidget->cursorRect(editorWidget->textCursor());
|
|
||||||
QPoint pos = editorWidget->viewport()->mapToGlobal(cursorRect.topLeft())
|
|
||||||
- Utils::ToolTip::offsetFromPosition();
|
|
||||||
pos.ry() -= tooltipWidget->sizeHint().height();
|
|
||||||
ToolTip::show(pos, tooltipWidget, editorWidget);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace QodeAssist
|
|
48
utils/CounterTooltip.cpp
Normal file
48
utils/CounterTooltip.cpp
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "CounterTooltip.hpp"
|
||||||
|
|
||||||
|
namespace QodeAssist {
|
||||||
|
|
||||||
|
CounterTooltip::CounterTooltip(int count)
|
||||||
|
: m_count(count)
|
||||||
|
{
|
||||||
|
m_label = new QLabel(this);
|
||||||
|
addWidget(m_label);
|
||||||
|
updateLabel();
|
||||||
|
|
||||||
|
m_timer = new QTimer(this);
|
||||||
|
m_timer->setSingleShot(true);
|
||||||
|
m_timer->setInterval(2000);
|
||||||
|
|
||||||
|
connect(m_timer, &QTimer::timeout, this, [this] { emit finished(m_count); });
|
||||||
|
|
||||||
|
m_timer->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
CounterTooltip::~CounterTooltip() {}
|
||||||
|
|
||||||
|
void CounterTooltip::updateLabel()
|
||||||
|
{
|
||||||
|
const auto hotkey = QKeySequence(QKeySequence::MoveToNextWord).toString();
|
||||||
|
m_label->setText(QString("Insert Next %1 line(s) (%2)").arg(m_count).arg(hotkey));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace QodeAssist
|
@ -1,13 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2023 The Qt Company Ltd.
|
|
||||||
* Copyright (C) 2024 Petr Mironychev
|
* Copyright (C) 2024 Petr Mironychev
|
||||||
*
|
*
|
||||||
* This file is part of Qode Assist.
|
* This file is part of QodeAssist.
|
||||||
*
|
*
|
||||||
* The Qt Company portions:
|
|
||||||
* SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
|
||||||
*
|
|
||||||
* Petr Mironychev portions:
|
|
||||||
* QodeAssist is free software: you can redistribute it and/or modify
|
* QodeAssist is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
@ -24,24 +19,30 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QTextBlock>
|
#include <QLabel>
|
||||||
#include <texteditor/basehoverhandler.h>
|
#include <QTimer>
|
||||||
|
#include <QToolBar>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
class QodeAssistHoverHandler : public TextEditor::BaseHoverHandler
|
class CounterTooltip : public QToolBar
|
||||||
{
|
{
|
||||||
public:
|
Q_OBJECT
|
||||||
QodeAssistHoverHandler() = default;
|
|
||||||
|
|
||||||
protected:
|
public:
|
||||||
void identifyMatch(TextEditor::TextEditorWidget *editorWidget,
|
CounterTooltip(int count);
|
||||||
int pos,
|
~CounterTooltip();
|
||||||
ReportPriority report) final;
|
|
||||||
void operateTooltip(TextEditor::TextEditorWidget *editorWidget, const QPoint &point) final;
|
signals:
|
||||||
|
void finished(int count);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QTextBlock m_block;
|
void updateLabel();
|
||||||
|
|
||||||
|
QLabel *m_label;
|
||||||
|
QTimer *m_timer;
|
||||||
|
int m_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist
|
} // namespace QodeAssist
|
Loading…
x
Reference in New Issue
Block a user