Skip to content

Commit 3b6f1ff

Browse files
LiHua000deepin-mozart
authored andcommitted
feat: [codegeex] optimized of reference files functional
Log: as title
1 parent 50da92a commit 3b6f1ff

File tree

6 files changed

+200
-152
lines changed

6 files changed

+200
-152
lines changed

assets/translations/en_US.ts

Lines changed: 73 additions & 67 deletions
Large diffs are not rendered by default.

assets/translations/zh_CN.ts

Lines changed: 73 additions & 67 deletions
Large diffs are not rendered by default.

src/plugins/codegeex/widgets/inputeditwidget.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "referencepopup.h"
77
#include "codegeexmanager.h"
88
#include "services/editor/editorservice.h"
9+
#include "services/window/windowservice.h"
910

1011
#include <DTextEdit>
1112
#include <DToolButton>
@@ -19,6 +20,7 @@
1920
#include <QAbstractTextDocumentLayout>
2021

2122
DWIDGET_USE_NAMESPACE
23+
using namespace dpfservice;
2224

2325
static const int minInputEditHeight = 36;
2426
static const int maxInputEditHeight = 236;
@@ -131,7 +133,8 @@ void InputEditWidgetPrivate::initEdit()
131133
{
132134
edit = new InputEdit(q);
133135
InputEditWidget::connect(edit, &InputEdit::textChanged, q, [this]() {
134-
if (edit->toPlainText().isEmpty())
136+
auto currentText = edit->toPlainText();
137+
if (currentText.isEmpty())
135138
sendButton->setEnabled(false);
136139
else
137140
sendButton->setEnabled(true);
@@ -140,6 +143,18 @@ void InputEditWidgetPrivate::initEdit()
140143
auto cursorPos = edit->textCursor().position();
141144
if (cursorPos > 0 && edit->document()->characterAt(cursorPos - 1) == "@")
142145
q->popupReference();
146+
147+
if (referencePopup->isVisible() && !currentText.endsWith('@')) {
148+
auto start = currentText.indexOf('@');
149+
auto firstSpace = currentText.indexOf(' ', start); // first space after `@`
150+
if (start == -1 || (firstSpace != -1 && cursorPos > firstSpace))
151+
referencePopup->hide();
152+
153+
auto filterText = currentText.mid(start + 1, cursorPos - start - 1);
154+
model.setFilterText(filterText);
155+
} else {
156+
model.setFilterText("");
157+
}
143158
});
144159
}
145160

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

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

431+
auto notify = [=](const QString &message){
432+
WindowService *windowSrv = dpfGetService(WindowService);
433+
windowSrv->notify(2, "CodeGeeX", message, {});
434+
};
435+
418436
auto appendTag = [=](const QString &filePath) {
419437
QFileInfo info(filePath);
420438
d->selectedFiles.append(filePath);
@@ -424,8 +442,10 @@ void InputEditWidget::accept(const QModelIndex &index)
424442
};
425443
if (item.type == reference_current_file) {
426444
auto filePath = editorSrv->currentFile();
427-
if (filePath.isEmpty())
445+
if (filePath.isEmpty()) {
446+
notify(tr("No opened file"));
428447
return;
448+
}
429449
appendTag(filePath);
430450
} else if (item.type == reference_select_file) {
431451
QString result = QFileDialog::getOpenFileName(this, QAction::tr("Select File"), QDir::homePath());
@@ -434,8 +454,10 @@ void InputEditWidget::accept(const QModelIndex &index)
434454
appendTag(result);
435455
} else if (item.type == reference_opened_files) {
436456
auto openedFiles = editorSrv->openedFiles();
437-
if (openedFiles.isEmpty())
457+
if (openedFiles.isEmpty()) {
458+
notify(tr("No opened file"));
438459
return;
460+
}
439461
QList<ItemInfo> items;
440462
for (auto file : openedFiles) {
441463
ItemInfo item;

src/plugins/codegeex/widgets/referencepopup.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ void DisplayItemDelegate::paint(QPainter *painter,
4242

4343
QStyleOptionViewItem opt = option;
4444
initStyleOption(&opt, index);
45-
4645
paintItemBackground(painter, opt, index);
4746
paintItemColumn(painter, opt, index);
4847
}
@@ -130,27 +129,28 @@ void ItemModel::clear()
130129
{
131130
beginResetModel();
132131
items.clear();
132+
displayItems.clear();
133133
endResetModel();
134134
}
135135

136136
QVariant ItemModel::data(const QModelIndex &index, int role) const
137137
{
138-
if (!index.isValid() || index.row() >= items.size())
138+
if (!index.isValid() || index.row() >= displayItems.size())
139139
return QVariant();
140140

141141
switch (role) {
142142
case Qt::DisplayRole:
143143
if (index.column() == DisplayNameColumn)
144-
return items.at(index.row()).displayName;
144+
return displayItems.at(index.row()).displayName;
145145
else if (index.column() == ExtraInfoColumn)
146-
return items.at(index.row()).extraInfo;
146+
return displayItems.at(index.row()).extraInfo;
147147
break;
148148
case Qt::ToolTipRole:
149-
if (!items.at(index.row()).extraInfo.isEmpty())
150-
return items.at(index.row()).extraInfo;
149+
if (!displayItems.at(index.row()).extraInfo.isEmpty())
150+
return displayItems.at(index.row()).extraInfo;
151151
case Qt::DecorationRole:
152152
if (index.column() == DisplayNameColumn)
153-
return items[index.row()].icon;
153+
return displayItems[index.row()].icon;
154154
break;
155155
};
156156
return QVariant();
@@ -160,26 +160,38 @@ void ItemModel::addItems(const QList<ItemInfo> &items)
160160
{
161161
beginInsertRows(QModelIndex(), this->items.size(), this->items.size() + items.size() - 1);
162162
this->items.append(items);
163+
this->displayItems.append(items);
163164
endInsertRows();
164165
}
165166

167+
void ItemModel::setFilterText(const QString &filterText)
168+
{
169+
beginResetModel();
170+
displayItems.clear();
171+
for (auto item : items) {
172+
if (item.displayName.startsWith(filterText))
173+
displayItems.append(item);
174+
}
175+
endResetModel();
176+
}
177+
166178
QList<ItemInfo> ItemModel::getItems() const
167179
{
168-
return items;
180+
return displayItems;
169181
}
170182

171183
int ItemModel::rowCount(const QModelIndex &parent) const
172184
{
173185
if (parent.isValid())
174186
return 0;
175-
return items.size();
187+
return displayItems.size();
176188
}
177189

178190
int ItemModel::columnCount(const QModelIndex &parent) const
179191
{
180192
if (parent.isValid())
181193
return 0;
182-
for (auto item : items)
194+
for (auto item : displayItems)
183195
if (!item.extraInfo.isEmpty())
184196
return ColumnCount;
185197
return 1;
@@ -217,14 +229,14 @@ PopupWidget::PopupWidget(QWidget *parent)
217229
void PopupWidget::updateGeometry()
218230
{
219231
Q_ASSERT(parentWidget());
220-
221232
const int border = list->frameWidth();
222233
QSize size = parentWidget()->size();
223234
auto itemHeight = fontMetrics().height() * 2 + 5; // same as itemdelegate
224235
auto height = 10 * itemHeight;
225236
if (model && model->rowCount() < 10)
226237
height = model->rowCount() * itemHeight;
227238

239+
height = height < minimumHeight() ? minimumHeight() : height;
228240
const QRect rect(parentWidget()->mapToGlobal(QPoint(-border, -(height + 5))), QSize(size.width(), height));
229241
setGeometry(rect);
230242
}
@@ -301,7 +313,7 @@ void PopupWidget::keyPressEvent(QKeyEvent *event)
301313
break;
302314
case Qt::Key_Return:
303315
case Qt::Key_Enter:
304-
if (event->modifiers() == 0) {
316+
if (event->modifiers() == 0 || event->modifiers() == Qt::KeypadModifier) {
305317
emit selectIndex(list->currentIndex());
306318
return;
307319
}

src/plugins/codegeex/widgets/referencepopup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ class ItemModel : public QAbstractListModel
6969
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
7070

7171
void addItems(const QList<ItemInfo> &items);
72+
void setFilterText(const QString &filterText);
7273

7374
QList<ItemInfo> getItems() const;
7475

7576
private:
7677
QList<ItemInfo> items;
78+
QList<ItemInfo> displayItems;
7779
};
7880

7981
class PopupWidget : public DBlurEffectWidget

src/plugins/core/locator/popupwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void PopupWidget::keyPressEvent(QKeyEvent *event)
186186
break;
187187
case Qt::Key_Return:
188188
case Qt::Key_Enter:
189-
if (event->modifiers() == 0) {
189+
if (event->modifiers() == 0 || event->modifiers() == Qt::KeypadModifier) {
190190
emit selectIndex(tree->currentIndex());
191191
return;
192192
}

0 commit comments

Comments
 (0)