Skip to content

Commit

Permalink
feat: [codegeex] optimized of reference files functional
Browse files Browse the repository at this point in the history
Log: as title
  • Loading branch information
LiHua000 authored and deepin-mozart committed Oct 28, 2024
1 parent 50da92a commit 3b6f1ff
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 152 deletions.
140 changes: 73 additions & 67 deletions assets/translations/en_US.ts

Large diffs are not rendered by default.

140 changes: 73 additions & 67 deletions assets/translations/zh_CN.ts

Large diffs are not rendered by default.

32 changes: 27 additions & 5 deletions src/plugins/codegeex/widgets/inputeditwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "referencepopup.h"
#include "codegeexmanager.h"
#include "services/editor/editorservice.h"
#include "services/window/windowservice.h"

#include <DTextEdit>
#include <DToolButton>
Expand All @@ -19,6 +20,7 @@
#include <QAbstractTextDocumentLayout>

DWIDGET_USE_NAMESPACE
using namespace dpfservice;

static const int minInputEditHeight = 36;
static const int maxInputEditHeight = 236;
Expand Down Expand Up @@ -131,7 +133,8 @@ void InputEditWidgetPrivate::initEdit()
{
edit = new InputEdit(q);
InputEditWidget::connect(edit, &InputEdit::textChanged, q, [this]() {
if (edit->toPlainText().isEmpty())
auto currentText = edit->toPlainText();
if (currentText.isEmpty())
sendButton->setEnabled(false);
else
sendButton->setEnabled(true);
Expand All @@ -140,6 +143,18 @@ void InputEditWidgetPrivate::initEdit()
auto cursorPos = edit->textCursor().position();
if (cursorPos > 0 && edit->document()->characterAt(cursorPos - 1) == "@")
q->popupReference();

if (referencePopup->isVisible() && !currentText.endsWith('@')) {
auto start = currentText.indexOf('@');
auto firstSpace = currentText.indexOf(' ', start); // first space after `@`
if (start == -1 || (firstSpace != -1 && cursorPos > firstSpace))
referencePopup->hide();

auto filterText = currentText.mid(start + 1, cursorPos - start - 1);
model.setFilterText(filterText);
} else {
model.setFilterText("");
}
});
}

Expand Down Expand Up @@ -378,7 +393,6 @@ bool InputEditWidget::eventFilter(QObject *watched, QEvent *event)
else
emit handleKey(keyEvent);
return true;
case Qt::Key_Backspace:
case Qt::Key_Space:
d->referencePopup->hide();
break;
Expand Down Expand Up @@ -411,10 +425,14 @@ void InputEditWidget::accept(const QModelIndex &index)
if (row < 0 || row >= d->model.rowCount())
return;

using dpfservice::EditorService;
EditorService *editorSrv = dpfGetService(EditorService);
ItemInfo item = d->model.getItems().at(row);

auto notify = [=](const QString &message){
WindowService *windowSrv = dpfGetService(WindowService);
windowSrv->notify(2, "CodeGeeX", message, {});
};

auto appendTag = [=](const QString &filePath) {
QFileInfo info(filePath);
d->selectedFiles.append(filePath);
Expand All @@ -424,8 +442,10 @@ void InputEditWidget::accept(const QModelIndex &index)
};
if (item.type == reference_current_file) {
auto filePath = editorSrv->currentFile();
if (filePath.isEmpty())
if (filePath.isEmpty()) {
notify(tr("No opened file"));
return;
}
appendTag(filePath);
} else if (item.type == reference_select_file) {
QString result = QFileDialog::getOpenFileName(this, QAction::tr("Select File"), QDir::homePath());
Expand All @@ -434,8 +454,10 @@ void InputEditWidget::accept(const QModelIndex &index)
appendTag(result);
} else if (item.type == reference_opened_files) {
auto openedFiles = editorSrv->openedFiles();
if (openedFiles.isEmpty())
if (openedFiles.isEmpty()) {
notify(tr("No opened file"));
return;
}
QList<ItemInfo> items;
for (auto file : openedFiles) {
ItemInfo item;
Expand Down
36 changes: 24 additions & 12 deletions src/plugins/codegeex/widgets/referencepopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void DisplayItemDelegate::paint(QPainter *painter,

QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);

paintItemBackground(painter, opt, index);
paintItemColumn(painter, opt, index);
}
Expand Down Expand Up @@ -130,27 +129,28 @@ void ItemModel::clear()
{
beginResetModel();
items.clear();
displayItems.clear();
endResetModel();
}

QVariant ItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= items.size())
if (!index.isValid() || index.row() >= displayItems.size())
return QVariant();

switch (role) {
case Qt::DisplayRole:
if (index.column() == DisplayNameColumn)
return items.at(index.row()).displayName;
return displayItems.at(index.row()).displayName;
else if (index.column() == ExtraInfoColumn)
return items.at(index.row()).extraInfo;
return displayItems.at(index.row()).extraInfo;
break;
case Qt::ToolTipRole:
if (!items.at(index.row()).extraInfo.isEmpty())
return items.at(index.row()).extraInfo;
if (!displayItems.at(index.row()).extraInfo.isEmpty())
return displayItems.at(index.row()).extraInfo;
case Qt::DecorationRole:
if (index.column() == DisplayNameColumn)
return items[index.row()].icon;
return displayItems[index.row()].icon;
break;
};
return QVariant();
Expand All @@ -160,26 +160,38 @@ void ItemModel::addItems(const QList<ItemInfo> &items)
{
beginInsertRows(QModelIndex(), this->items.size(), this->items.size() + items.size() - 1);
this->items.append(items);
this->displayItems.append(items);
endInsertRows();
}

void ItemModel::setFilterText(const QString &filterText)
{
beginResetModel();
displayItems.clear();
for (auto item : items) {
if (item.displayName.startsWith(filterText))
displayItems.append(item);
}
endResetModel();
}

QList<ItemInfo> ItemModel::getItems() const
{
return items;
return displayItems;
}

int ItemModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return items.size();
return displayItems.size();
}

int ItemModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
for (auto item : items)
for (auto item : displayItems)
if (!item.extraInfo.isEmpty())
return ColumnCount;
return 1;
Expand Down Expand Up @@ -217,14 +229,14 @@ PopupWidget::PopupWidget(QWidget *parent)
void PopupWidget::updateGeometry()
{
Q_ASSERT(parentWidget());

const int border = list->frameWidth();
QSize size = parentWidget()->size();
auto itemHeight = fontMetrics().height() * 2 + 5; // same as itemdelegate
auto height = 10 * itemHeight;
if (model && model->rowCount() < 10)
height = model->rowCount() * itemHeight;

height = height < minimumHeight() ? minimumHeight() : height;
const QRect rect(parentWidget()->mapToGlobal(QPoint(-border, -(height + 5))), QSize(size.width(), height));
setGeometry(rect);
}
Expand Down Expand Up @@ -301,7 +313,7 @@ void PopupWidget::keyPressEvent(QKeyEvent *event)
break;
case Qt::Key_Return:
case Qt::Key_Enter:
if (event->modifiers() == 0) {
if (event->modifiers() == 0 || event->modifiers() == Qt::KeypadModifier) {
emit selectIndex(list->currentIndex());
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/codegeex/widgets/referencepopup.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ class ItemModel : public QAbstractListModel
int columnCount(const QModelIndex &parent = QModelIndex()) const override;

void addItems(const QList<ItemInfo> &items);
void setFilterText(const QString &filterText);

QList<ItemInfo> getItems() const;

private:
QList<ItemInfo> items;
QList<ItemInfo> displayItems;
};

class PopupWidget : public DBlurEffectWidget
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/locator/popupwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void PopupWidget::keyPressEvent(QKeyEvent *event)
break;
case Qt::Key_Return:
case Qt::Key_Enter:
if (event->modifiers() == 0) {
if (event->modifiers() == 0 || event->modifiers() == Qt::KeypadModifier) {
emit selectIndex(tree->currentIndex());
return;
}
Expand Down

0 comments on commit 3b6f1ff

Please sign in to comment.