From fc0ca71b4dbe4e13b856687e95280a923f8cef54 Mon Sep 17 00:00:00 2001 From: uvos Date: Thu, 2 Nov 2023 21:40:24 +0100 Subject: [PATCH] reformat in uvosstyle --- kateai.cpp | 287 ++++++++++++++++++++++--------------------- kateai.h | 73 ++++++----- kateaiconfigpage.cpp | 56 ++++----- kateaiconfigpage.h | 34 ++--- 4 files changed, 225 insertions(+), 225 deletions(-) diff --git a/kateai.cpp b/kateai.cpp index 0202f28..58c7cfe 100644 --- a/kateai.cpp +++ b/kateai.cpp @@ -26,218 +26,219 @@ K_PLUGIN_FACTORY_WITH_JSON(KateAiPluginFactory, "kateai.json", registerPlugin();) KateAiPlugin::KateAiPlugin(QObject *parent, const QList &) - : KTextEditor::Plugin(parent), m_serverUrl(QStringLiteral("ws://localhost:8642")) + : KTextEditor::Plugin(parent), m_serverUrl(QStringLiteral("ws://localhost:8642")) { - connect(&m_webSocket, &QWebSocket::connected, this, &KateAiPlugin::onConnected); - readConfig(); + connect(&m_webSocket, &QWebSocket::connected, this, &KateAiPlugin::onConnected); + readConfig(); } void KateAiPlugin::onConnected() { - qDebug()<<__func__< webSocket, bool instruct) - : QObject(plugin) - , m_mainWindow(mainwindow) - , m_webSocket(webSocket) - , m_useInstruct(instruct) +KateAiPluginView::KateAiPluginView(KateAiPlugin *plugin, KTextEditor::MainWindow *mainwindow, + QPointer webSocket, bool instruct) + : QObject(plugin) + , m_mainWindow(mainwindow) + , m_webSocket(webSocket) + , m_useInstruct(instruct) { - KXMLGUIClient::setComponentName(QStringLiteral("kateaiplugin"), QStringLiteral("Git Blame")); - setXMLFile(QStringLiteral("ui.rc")); - QAction *generateAction = actionCollection()->addAction(QStringLiteral("ai_generate")); - generateAction->setText(QStringLiteral("Generate text using AI")); - actionCollection()->setDefaultShortcut(generateAction, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_A)); - m_mainWindow->guiFactory()->addClient(this); + KXMLGUIClient::setComponentName(QStringLiteral("kateaiplugin"), QStringLiteral("Git Blame")); + setXMLFile(QStringLiteral("ui.rc")); + QAction *generateAction = actionCollection()->addAction(QStringLiteral("ai_generate")); + generateAction->setText(QStringLiteral("Generate text using AI")); + actionCollection()->setDefaultShortcut(generateAction, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_A)); + m_mainWindow->guiFactory()->addClient(this); - connect(generateAction, &QAction::triggered, this, &KateAiPluginView::generate); - connect(m_webSocket, &QWebSocket::textMessageReceived, this, &KateAiPluginView::socketMessage); + connect(generateAction, &QAction::triggered, this, &KateAiPluginView::generate); + connect(m_webSocket, &QWebSocket::textMessageReceived, this, &KateAiPluginView::socketMessage); } QPointer KateAiPluginView::activeDocument() const { - KTextEditor::View *view = m_mainWindow->activeView(); - if(view && view->document()) - return view->document(); - return nullptr; + KTextEditor::View *view = m_mainWindow->activeView(); + if(view && view->document()) + return view->document(); + return nullptr; } KTextEditor::Cursor KateAiPluginView::getCurrentCursor() const { - KTextEditor::View *view = m_mainWindow->activeView(); - if(view) - return view->cursorPosition(); - return KTextEditor::Cursor(); + KTextEditor::View *view = m_mainWindow->activeView(); + if(view) + return view->cursorPosition(); + return KTextEditor::Cursor(); } void KateAiPluginView::socketMessage(const QString& message) { - QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8()); - QJsonValue idVal = jsonDocument[QStringLiteral("request_id")]; - if(!idVal.isDouble()) - { - qDebug()<<"Got invalid response on socket"; - return; - } - int id = idVal.toInt(); + QJsonDocument jsonDocument = QJsonDocument::fromJson(message.toUtf8()); + QJsonValue idVal = jsonDocument[QStringLiteral("request_id")]; + if(!idVal.isDouble()) + { + qDebug()<<"Got invalid response on socket"; + return; + } + int id = idVal.toInt(); - QHash::iterator it = m_requests.find(id); - if(it != m_requests.end()) - { - QJsonValue responseValue = jsonDocument[QStringLiteral("response")]; - if(!responseValue.isString()) - { - qDebug()<<"Got invalid response on socket"; - return; - } - if(it.value().document) - it.value().document->insertText(it.value().cursor, responseValue.toString()); - m_requests.erase(it); - } + QHash::iterator it = m_requests.find(id); + if(it != m_requests.end()) + { + QJsonValue responseValue = jsonDocument[QStringLiteral("response")]; + if(!responseValue.isString()) + { + qDebug()<<"Got invalid response on socket"; + return; + } + if(it.value().document) + it.value().document->insertText(it.value().cursor, responseValue.toString()); + m_requests.erase(it); + } } QStringList KateAiPluginView::getIncludePaths(const QString& text) { - QStringList lines = text.split(U'\n'); - QStringList paths; + QStringList lines = text.split(U'\n'); + QStringList paths; - for(const QString& line : lines) - { - if(line.trimmed().startsWith(QStringLiteral("#include"))) - { - int start = line.indexOf(U'<'); - int end; - if(start != -1) - { - end = line.indexOf(U'>', start+1); - if(end == -1) - continue; - } - else - { - start = line.indexOf(U'"'); - if(start == -1) - continue; + for(const QString& line : lines) + { + if(line.trimmed().startsWith(QStringLiteral("#include"))) + { + int start = line.indexOf(U'<'); + int end; + if(start != -1) + { + end = line.indexOf(U'>', start+1); + if(end == -1) + continue; + } + else + { + start = line.indexOf(U'"'); + if(start == -1) + continue; - end = line.indexOf(U'"', start+1); - if(end == -1) - continue; - } - paths.push_back(line.mid(start+1, (end-(start+1))).trimmed()); - } - } - return paths; + end = line.indexOf(U'"', start+1); + if(end == -1) + continue; + } + paths.push_back(line.mid(start+1, (end-(start+1))).trimmed()); + } + } + return paths; } QString KateAiPluginView::assembleContext(QPointer document, const KTextEditor::Cursor& cursor) { - QString mime = document->mimeType(); - QString context; - QString baseText; + QString mime = document->mimeType(); + QString context; + QString baseText; - if(!m_useInstruct) - baseText = document->text(KTextEditor::Range(KTextEditor::Cursor(0, 0), cursor)); - else - baseText = document->text(); - if(mime == QStringLiteral("text/x-c++src") || mime == QStringLiteral("text/x-csrc")) - { - QFileInfo documentFileInfo(document->url().path()); - QString directory = documentFileInfo.absolutePath(); - QStringList paths = getIncludePaths(baseText); - qDebug()<<__func__<<"Directory:"<text(KTextEditor::Range(KTextEditor::Cursor(0, 0), cursor)); + else + baseText = document->text(); + if(mime == QStringLiteral("text/x-c++src") || mime == QStringLiteral("text/x-csrc")) + { + QFileInfo documentFileInfo(document->url().path()); + QString directory = documentFileInfo.absolutePath(); + QStringList paths = getIncludePaths(baseText); + qDebug()<<__func__<<"Directory:"<mimeType(); - if(m_webSocket && m_webSocket->isValid()) - { - KTextEditor::Cursor cursor = getCurrentCursor(); - QPointer document = activeDocument(); - QString text = assembleContext(document, cursor); - int id = QRandomGenerator::global()->bounded(0, std::numeric_limits::max()); + qDebug()<mimeType(); + if(m_webSocket && m_webSocket->isValid()) + { + KTextEditor::Cursor cursor = getCurrentCursor(); + QPointer document = activeDocument(); + QString text = assembleContext(document, cursor); + int id = QRandomGenerator::global()->bounded(0, std::numeric_limits::max()); - QJsonObject json; - json[QStringLiteral("action")] = QStringLiteral("infer"); - json[QStringLiteral("request_id")] = id; - json[QStringLiteral("text")] = text; - json[QStringLiteral("max_new_tokens")] = 50; - json[QStringLiteral("stream")] = false; + QJsonObject json; + json[QStringLiteral("action")] = QStringLiteral("infer"); + json[QStringLiteral("request_id")] = id; + json[QStringLiteral("text")] = text; + json[QStringLiteral("max_new_tokens")] = 50; + json[QStringLiteral("stream")] = false; - QJsonDocument jsonDocument(json); - QString requestText = QString::fromUtf8(jsonDocument.toJson(QJsonDocument::JsonFormat::Compact)); - qDebug()<<__func__<<' '<sendTextMessage(requestText); - m_requests.insert(id, {cursor, document}); - } - else - { - QMessageBox box; - box.setText(i18n("The AI server is not connected.")); - box.setInformativeText(i18n("would you like to try and reconnect?")); - box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); - box.setDefaultButton(QMessageBox::Yes); - int ret = box.exec(); - if(ret == QMessageBox::Yes) - reconnect(); - } + QJsonDocument jsonDocument(json); + QString requestText = QString::fromUtf8(jsonDocument.toJson(QJsonDocument::JsonFormat::Compact)); + qDebug()<<__func__<<' '<sendTextMessage(requestText); + m_requests.insert(id, {cursor, document}); + } + else + { + QMessageBox box; + box.setText(i18n("The AI server is not connected.")); + box.setInformativeText(i18n("would you like to try and reconnect?")); + box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + box.setDefaultButton(QMessageBox::Yes); + int ret = box.exec(); + if(ret == QMessageBox::Yes) + reconnect(); + } } KateAiPluginView::~KateAiPluginView() { - m_mainWindow->guiFactory()->removeClient(this); + m_mainWindow->guiFactory()->removeClient(this); } void KateAiPluginView::setInstruct(bool instruct) { - m_useInstruct = instruct; + m_useInstruct = instruct; } #include "kateai.moc" diff --git a/kateai.h b/kateai.h index 4321d2b..bbc6f94 100644 --- a/kateai.h +++ b/kateai.h @@ -15,62 +15,61 @@ class KateAiPlugin : public KTextEditor::Plugin { - Q_OBJECT + Q_OBJECT private: - QWebSocket m_webSocket; - QUrl m_serverUrl; + QWebSocket m_webSocket; + QUrl m_serverUrl; private: - void onConnected(); + void onConnected(); - int configPages() const override - { - return 1; - } + int configPages() const override + { + return 1; + } - KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override; + KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override; public: - explicit KateAiPlugin(QObject *parent = nullptr, const QList & = QList()); - ~KateAiPlugin() override; + explicit KateAiPlugin(QObject *parent = nullptr, const QList & = QList()); + ~KateAiPlugin() override; - QObject *createView(KTextEditor::MainWindow *mainWindow) override; + QObject *createView(KTextEditor::MainWindow *mainWindow) override; - void readConfig(); - void reconnect(); - Q_SIGNAL void instructChanged(); + void readConfig(); + void reconnect(); + Q_SIGNAL void instructChanged(); }; class KateAiPluginView : public QObject, public KXMLGUIClient { - Q_OBJECT + Q_OBJECT private: - struct Request - { - KTextEditor::Cursor cursor; - QPointer document; - }; - - KTextEditor::MainWindow *m_mainWindow; - QPointer m_webSocket; - QHash m_requests; - bool m_useInstruct = false; + struct Request + { + KTextEditor::Cursor cursor; + QPointer document; + }; + KTextEditor::MainWindow *m_mainWindow; + QPointer m_webSocket; + QHash m_requests; + bool m_useInstruct = false; private: - void generate(); - void socketMessage(const QString& message); - static QStringList getIncludePaths(const QString& text); - QString assembleContext(QPointer document, const KTextEditor::Cursor& cursor); - - QPointer activeDocument() const; - KTextEditor::Cursor getCurrentCursor() const; + void generate(); + void socketMessage(const QString& message); + static QStringList getIncludePaths(const QString& text); + QString assembleContext(QPointer document, const KTextEditor::Cursor& cursor); + QPointer activeDocument() const; + KTextEditor::Cursor getCurrentCursor() const; public: - KateAiPluginView(KateAiPlugin *plugin, KTextEditor::MainWindow *mainwindow, QPointer webSocket, bool instruct = false); - ~KateAiPluginView() override; - void setInstruct(bool instruct); + KateAiPluginView(KateAiPlugin *plugin, KTextEditor::MainWindow *mainwindow, QPointer webSocket, + bool instruct = false); + ~KateAiPluginView() override; + void setInstruct(bool instruct); - Q_SIGNAL void reconnect(); + Q_SIGNAL void reconnect(); }; diff --git a/kateaiconfigpage.cpp b/kateaiconfigpage.cpp index 45df86a..ed1d92a 100644 --- a/kateaiconfigpage.cpp +++ b/kateaiconfigpage.cpp @@ -9,57 +9,57 @@ #include KateAiConfigPage::KateAiConfigPage(QWidget *parent, KateAiPlugin *plugin) - : KTextEditor::ConfigPage(parent) - , m_plugin(plugin) + : KTextEditor::ConfigPage(parent) + , m_plugin(plugin) { - QVBoxLayout* layout = new QVBoxLayout(this); - layout->setContentsMargins(0, 0, 0, 0); + QVBoxLayout* layout = new QVBoxLayout(this); + layout->setContentsMargins(0, 0, 0, 0); - QHBoxLayout* lineLayout = new QHBoxLayout(this); - QLabel* lineEditLabel = new QLabel(i18n("Url for the WebSockets ExLlama Ai server:"), this); - lineEditLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); - lineLayout->addWidget(lineEditLabel); - lineLayout->addWidget(&lineUrl); - layout->addLayout(lineLayout); + QHBoxLayout* lineLayout = new QHBoxLayout(this); + QLabel* lineEditLabel = new QLabel(i18n("Url for the WebSockets ExLlama Ai server:"), this); + lineEditLabel->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); + lineLayout->addWidget(lineEditLabel); + lineLayout->addWidget(&lineUrl); + layout->addLayout(lineLayout); - btnCompletion.setText(i18n("Use the Ai to generate a completion")); - btnInstruct.setText(i18n("Use the Ai to insert a response to a instruction")); - layout->addWidget(&btnCompletion); - layout->addWidget(&btnInstruct); - layout->addStretch(); + btnCompletion.setText(i18n("Use the Ai to generate a completion")); + btnInstruct.setText(i18n("Use the Ai to insert a response to a instruction")); + layout->addWidget(&btnCompletion); + layout->addWidget(&btnInstruct); + layout->addStretch(); - reset(); + reset(); } QString KateAiConfigPage::name() const { - return i18n("Ai"); + return i18n("Ai"); } QString KateAiConfigPage::fullName() const { - return i18n("Ai Settings"); + return i18n("Ai Settings"); } QIcon KateAiConfigPage::icon() const { - return QIcon::fromTheme(QStringLiteral("text-x-generic")); + return QIcon::fromTheme(QStringLiteral("text-x-generic")); } void KateAiConfigPage::apply() { - KConfigGroup config(KSharedConfig::openConfig(), "Ai"); - config.writeEntry("Url", lineUrl.text()); - config.writeEntry("Instruct", btnInstruct.isChecked()); + KConfigGroup config(KSharedConfig::openConfig(), "Ai"); + config.writeEntry("Url", lineUrl.text()); + config.writeEntry("Instruct", btnInstruct.isChecked()); - config.sync(); - m_plugin->readConfig(); + config.sync(); + m_plugin->readConfig(); } void KateAiConfigPage::reset() { - KConfigGroup config(KSharedConfig::openConfig(), "Ai"); - lineUrl.setText(config.readEntry("Url", "ws://localhost:8642")); - btnInstruct.setChecked(config.readEntry("Instruct", false)); - btnCompletion.setChecked(!btnInstruct.isChecked()); + KConfigGroup config(KSharedConfig::openConfig(), "Ai"); + lineUrl.setText(config.readEntry("Url", "ws://localhost:8642")); + btnInstruct.setChecked(config.readEntry("Instruct", false)); + btnCompletion.setChecked(!btnInstruct.isChecked()); } diff --git a/kateaiconfigpage.h b/kateaiconfigpage.h index 3e31b2b..ab179f4 100644 --- a/kateaiconfigpage.h +++ b/kateaiconfigpage.h @@ -8,26 +8,26 @@ class KateAiConfigPage : public KTextEditor::ConfigPage { - Q_OBJECT + Q_OBJECT private: - QLineEdit lineUrl; - QRadioButton btnCompletion; - QRadioButton btnInstruct; - KateAiPlugin* m_plugin; + QLineEdit lineUrl; + QRadioButton btnCompletion; + QRadioButton btnInstruct; + KateAiPlugin* m_plugin; public: - explicit KateAiConfigPage(QWidget *parent = nullptr, KateAiPlugin *plugin = nullptr); - ~KateAiConfigPage() override - { - } + explicit KateAiConfigPage(QWidget *parent = nullptr, KateAiPlugin *plugin = nullptr); + ~KateAiConfigPage() override + { + } - QString name() const override; - QString fullName() const override; - QIcon icon() const override; + QString name() const override; + QString fullName() const override; + QIcon icon() const override; - void apply() override; - void reset() override; - void defaults() override - { - } + void apply() override; + void reset() override; + void defaults() override + { + } };