fear: Add hint-trigger for call code completion (#266)

This commit is contained in:
Petr Mironychev
2025-11-17 22:24:04 +01:00
committed by GitHub
parent 86c6930c5f
commit bcdec96d92
13 changed files with 565 additions and 54 deletions

View File

@ -0,0 +1,72 @@
/*
* 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 "CompletionHintHandler.hpp"
#include "CompletionHintWidget.hpp"
#include <texteditor/texteditor.h>
namespace QodeAssist {
CompletionHintHandler::CompletionHintHandler() = default;
CompletionHintHandler::~CompletionHintHandler()
{
hideHint();
}
void CompletionHintHandler::showHint(TextEditor::TextEditorWidget *widget, QPoint position, int fontSize)
{
if (!widget) {
return;
}
if (!m_hintWidget) {
m_hintWidget = new CompletionHintWidget(widget, fontSize);
}
m_hintWidget->move(position);
m_hintWidget->show();
m_hintWidget->raise();
}
void CompletionHintHandler::hideHint()
{
if (m_hintWidget) {
m_hintWidget->deleteLater();
m_hintWidget = nullptr;
}
}
bool CompletionHintHandler::isHintVisible() const
{
return !m_hintWidget.isNull() && m_hintWidget->isVisible();
}
void CompletionHintHandler::updateHintPosition(TextEditor::TextEditorWidget *widget, QPoint position)
{
if (!widget || !m_hintWidget) {
return;
}
m_hintWidget->move(position);
}
} // namespace QodeAssist

View File

@ -0,0 +1,49 @@
/*
* 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 <QPointer>
#include <functional>
namespace TextEditor {
class TextEditorWidget;
}
namespace QodeAssist {
class CompletionHintWidget;
class CompletionHintHandler
{
public:
CompletionHintHandler();
~CompletionHintHandler();
void showHint(TextEditor::TextEditorWidget *widget, QPoint position, int fontSize);
void hideHint();
bool isHintVisible() const;
void updateHintPosition(TextEditor::TextEditorWidget *widget, QPoint position);
private:
QPointer<CompletionHintWidget> m_hintWidget;
};
} // namespace QodeAssist

View File

@ -0,0 +1,83 @@
/*
* 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 "CompletionHintWidget.hpp"
#include <QPainter>
#include <utils/theme/theme.h>
namespace QodeAssist {
CompletionHintWidget::CompletionHintWidget(QWidget *parent, int fontSize)
: QWidget(parent)
, m_isHovered(false)
{
m_accentColor = Utils::creatorTheme()->color(Utils::Theme::TextColorNormal);
setMouseTracking(true);
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_TranslucentBackground);
int triangleSize = qMax(6, fontSize / 2);
setFixedSize(triangleSize, triangleSize);
}
void CompletionHintWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QColor triangleColor = m_accentColor;
triangleColor.setAlpha(m_isHovered ? 255 : 200);
painter.setPen(Qt::NoPen);
painter.setBrush(triangleColor);
QPolygonF triangle;
int w = width();
int h = height();
triangle << QPointF(0, 0)
<< QPointF(0, h)
<< QPointF(w, h / 2.0);
painter.drawPolygon(triangle);
}
void CompletionHintWidget::enterEvent(QEnterEvent *event)
{
Q_UNUSED(event);
m_isHovered = true;
setCursor(Qt::PointingHandCursor);
update();
}
void CompletionHintWidget::leaveEvent(QEvent *event)
{
Q_UNUSED(event);
m_isHovered = false;
setCursor(Qt::ArrowCursor);
update();
}
} // 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 <QWidget>
namespace QodeAssist {
class CompletionHintWidget : public QWidget
{
Q_OBJECT
public:
explicit CompletionHintWidget(QWidget *parent = nullptr, int fontSize = 12);
~CompletionHintWidget() override = default;
protected:
void paintEvent(QPaintEvent *event) override;
void enterEvent(QEnterEvent *event) override;
void leaveEvent(QEvent *event) override;
private:
QColor m_accentColor;
bool m_isHovered;
};
} // namespace QodeAssist

View File

@ -88,7 +88,6 @@ void CompletionProgressHandler::operateTooltip(
m_progressWidget = new ProgressWidget(editorWidget);
// Set cancel callback for the widget
if (m_cancelCallback) {
m_progressWidget->setCancelCallback(m_cancelCallback);
}
@ -96,6 +95,8 @@ void CompletionProgressHandler::operateTooltip(
const QRect cursorRect = editorWidget->cursorRect(editorWidget->textCursor());
QPoint globalPos = editorWidget->viewport()->mapToGlobal(cursorRect.topLeft());
QPoint localPos = editorWidget->mapFromGlobal(globalPos);
localPos.rx() += 5;
localPos.ry() -= m_progressWidget->height() + 5;
if (localPos.y() < 0) {