diff --git a/src/plugins/codeeditor/codeeditor.cpp b/src/plugins/codeeditor/codeeditor.cpp index 00ba4586..27d4fd7d 100644 --- a/src/plugins/codeeditor/codeeditor.cpp +++ b/src/plugins/codeeditor/codeeditor.cpp @@ -184,7 +184,7 @@ void CodeEditor::initEditorService() editorService->replaceRange = std::bind(&WorkspaceWidget::replaceRange, workspaceWidget, _1, _2, _3); editorService->backgroundMarkerDefine = std::bind(&WorkspaceWidget::backgroundMarkerDefine, workspaceWidget, _1, _2, _3); editorService->setRangeBackgroundColor = std::bind(&WorkspaceWidget::setRangeBackgroundColor, workspaceWidget, _1, _2, _3, _4); - editorService->clearRangeBackgroundColor = std::bind(&WorkspaceWidget::clearRangeBackgroundColor, workspaceWidget, _1, _2, _3, _4); + editorService->getBackgroundRange = std::bind(&WorkspaceWidget::getBackgroundRange, workspaceWidget, _1, _2); editorService->clearAllBackgroundColor = std::bind(&WorkspaceWidget::clearAllBackgroundColor, workspaceWidget, _1, _2); editorService->showLineWidget = std::bind(&WorkspaceWidget::showLineWidget, workspaceWidget, _1, _2); editorService->closeLineWidget = std::bind(&WorkspaceWidget::closeLineWidget, workspaceWidget); diff --git a/src/plugins/codeeditor/gui/private/texteditor_p.cpp b/src/plugins/codeeditor/gui/private/texteditor_p.cpp index 21a3e930..a615029e 100644 --- a/src/plugins/codeeditor/gui/private/texteditor_p.cpp +++ b/src/plugins/codeeditor/gui/private/texteditor_p.cpp @@ -55,7 +55,7 @@ void TextEditorPrivate::init() q->SendScintilla(TextEditor::SCI_AUTOCSETCASEINSENSITIVEBEHAVIOUR, TextEditor::SC_CASEINSENSITIVEBEHAVIOUR_IGNORECASE); selectionChangeTimer.setSingleShot(true); - selectionChangeTimer.setInterval(150); + selectionChangeTimer.setInterval(50); initWidgetContainer(); initMargins(); @@ -220,8 +220,7 @@ void TextEditorPrivate::updateSettings() void TextEditorPrivate::onSelectionChanged() { - auto buttons = QApplication::mouseButtons(); - if (buttons.testFlag(Qt::LeftButton)) + if (leftButtonPressed) return; int lineFrom = -1, indexFrom = -1, lineTo = -1, indexTo = -1; @@ -539,6 +538,44 @@ void TextEditorPrivate::updateLineWidgetPosition() lineWidgetContainer->move(point.x(), displayY); } +void TextEditorPrivate::updateCacheInfo(int pos, int added) +{ + int line = 0, index = 0; + q->lineIndexFromPosition(pos, &line, &index); + editor.lineChanged(fileName, line, added); + if (lineWidgetContainer->isVisible()) { + if (showAtLine > line) { + showAtLine += added; + updateLineWidgetPosition(); + } + } + + if (cpCache.first != -1 && cpCache.first >= line) { + const auto &eolStr = q->eolAnnotation(cpCache.first); + if (eolStr.isEmpty() || !cpCache.second.contains(eolStr)) + cpCache.first += added; + } + + // update eolannotation line + for (auto it = eOLAnnotationRecords.begin(); it != eOLAnnotationRecords.end(); ++it) { + if (it.value() >= line) + it.value() += added; + } + + auto iter = markerCache.begin(); + for (; iter != markerCache.end(); ++iter) { + auto &range = iter.value(); + if (range.endLine < line) + continue; + else if (range.startLine > line) + range.startLine += added; + + range.endLine += added; + if (added > 0) + q->setRangeBackgroundColor(range.startLine, range.endLine, iter.key()); + } +} + void TextEditorPrivate::resetThemeColor() { if (q->lexer()) { @@ -586,29 +623,8 @@ void TextEditorPrivate::onModified(int pos, int mtype, const QString &text, int if (isAutoCompletionEnabled && !text.isEmpty()) editor.textChanged(); - if (added != 0) { - int line = 0, index = 0; - q->lineIndexFromPosition(pos, &line, &index); - editor.lineChanged(fileName, line, added); - if (lineWidgetContainer->isVisible()) { - if (showAtLine > line) { - showAtLine += added; - updateLineWidgetPosition(); - } - } - - if (cpCache.first != -1 && cpCache.first >= line) { - const auto &eolStr = q->eolAnnotation(cpCache.first); - if (eolStr.isEmpty() || !cpCache.second.contains(eolStr)) - cpCache.first += added; - } - - // update eolannotation line - for (auto it = eOLAnnotationRecords.begin(); it != eOLAnnotationRecords.end(); ++it) { - if (it.value() >= line) - it.value() += added; - } - } + if (added != 0) + updateCacheInfo(pos, added); if (mtype & TextEditor::SC_MOD_INSERTTEXT) { emit q->textAdded(pos, len, added, text, line); diff --git a/src/plugins/codeeditor/gui/private/texteditor_p.h b/src/plugins/codeeditor/gui/private/texteditor_p.h index 370170a7..e802b587 100644 --- a/src/plugins/codeeditor/gui/private/texteditor_p.h +++ b/src/plugins/codeeditor/gui/private/texteditor_p.h @@ -33,6 +33,12 @@ class TextEditorPrivate : public QObject RuntimeLineBackground }; + struct MarkerRange + { + int startLine = -1; + int endLine = -1; + }; + explicit TextEditorPrivate(TextEditor *qq); void init(); @@ -61,6 +67,7 @@ class TextEditorPrivate : public QObject QWidget *mainWindow(); void setContainerWidget(QWidget *widget); void updateLineWidgetPosition(); + void updateCacheInfo(int pos, int added); public slots: void resetThemeColor(); @@ -98,6 +105,8 @@ public slots: QTimer selectionChangeTimer; DTK_WIDGET_NAMESPACE::DFloatingWidget *lineWidgetContainer { nullptr }; int showAtLine { 0 }; + bool leftButtonPressed { false }; + QMap markerCache; }; #endif // TEXTEDITOR_P_H diff --git a/src/plugins/codeeditor/gui/tabwidget.cpp b/src/plugins/codeeditor/gui/tabwidget.cpp index 11f8c7bf..b1a9420a 100644 --- a/src/plugins/codeeditor/gui/tabwidget.cpp +++ b/src/plugins/codeeditor/gui/tabwidget.cpp @@ -943,14 +943,17 @@ bool TabWidget::setRangeBackgroundColor(const QString &fileName, int startLine, return false; } -bool TabWidget::clearRangeBackground(const QString &fileName, int startLine, int endLine, int marker) +Edit::Range TabWidget::getBackgroundRange(const QString &fileName, int marker, bool &found) { if (auto editor = d->findEditor(fileName)) { - editor->clearRangeBackgroundColor(startLine, endLine, marker); - return true; + found = true; + Edit::Range range; + editor->getBackgroundRange(marker, &range.start.line, &range.end.line); + return range; } - return false; + found = false; + return {}; } bool TabWidget::clearAllBackground(const QString &fileName, int marker) diff --git a/src/plugins/codeeditor/gui/tabwidget.h b/src/plugins/codeeditor/gui/tabwidget.h index 600d7745..4a5f3268 100644 --- a/src/plugins/codeeditor/gui/tabwidget.h +++ b/src/plugins/codeeditor/gui/tabwidget.h @@ -80,7 +80,7 @@ class TabWidget : public QWidget int backgroundMarkerDefine(const QString &fileName, const QColor &color, int defaultMarker); bool setRangeBackgroundColor(const QString &fileName, int startLine, int endLine, int marker); - bool clearRangeBackground(const QString &fileName, int startLine, int endLine, int marker); + dpfservice::Edit::Range getBackgroundRange(const QString &fileName, int marker, bool &found); bool clearAllBackground(const QString &fileName, int marker); void showLineWidget(int line, QWidget *widget); void closeLineWidget(); diff --git a/src/plugins/codeeditor/gui/texteditor.cpp b/src/plugins/codeeditor/gui/texteditor.cpp index 1cad0d8a..b118035d 100644 --- a/src/plugins/codeeditor/gui/texteditor.cpp +++ b/src/plugins/codeeditor/gui/texteditor.cpp @@ -346,25 +346,27 @@ void TextEditor::setRangeBackgroundColor(int startLine, int endLine, int marker) if (startLine > endLine) return; + d->markerCache.insert(marker, { startLine, endLine }); for (; startLine <= endLine; ++startLine) { markerAdd(startLine, marker); } } -void TextEditor::clearRangeBackgroundColor(int startLine, int endLine, int marker) +void TextEditor::getBackgroundRange(int marker, int *startLine, int *endLine) { - startLine = qMax(startLine, 0); - endLine = qMin(endLine, lines() - 1); - if (startLine > endLine) + if (!d->markerCache.contains(marker)) return; - for (; startLine <= endLine; ++startLine) { - markerDelete(startLine, marker); - } + if (!startLine || !endLine) + return; + + *startLine = d->markerCache[marker].startLine; + *endLine = d->markerCache[marker].endLine; } void TextEditor::clearAllBackgroundColor(int marker) { + d->markerCache.remove(marker); markerDeleteAll(marker); } @@ -913,6 +915,22 @@ void TextEditor::mouseMoveEvent(QMouseEvent *event) QsciScintilla::mouseMoveEvent(event); } +void TextEditor::mousePressEvent(QMouseEvent *event) +{ + if (event->buttons().testFlag(Qt::LeftButton)) + d->leftButtonPressed = true; + + QsciScintilla::mousePressEvent(event); +} + +void TextEditor::mouseReleaseEvent(QMouseEvent *event) +{ + if (!event->buttons().testFlag(Qt::LeftButton)) + d->leftButtonPressed = false; + + QsciScintilla::mouseReleaseEvent(event); +} + bool TextEditor::eventFilter(QObject *obj, QEvent *event) { if (obj == d->lineWidgetContainer && event->type() == QEvent::Resize) { diff --git a/src/plugins/codeeditor/gui/texteditor.h b/src/plugins/codeeditor/gui/texteditor.h index c7bbaa09..82c9f119 100644 --- a/src/plugins/codeeditor/gui/texteditor.h +++ b/src/plugins/codeeditor/gui/texteditor.h @@ -68,7 +68,7 @@ class TextEditor : public QsciScintilla int cursorPosition(); int backgroundMarkerDefine(const QColor &color, int defaultMarker); void setRangeBackgroundColor(int startLine, int endLine, int marker); - void clearRangeBackgroundColor(int startLine, int endLine, int marker); + void getBackgroundRange(int marker, int *startLine, int *endLine); void clearAllBackgroundColor(int marker); void showTips(const QString &tips); void showTips(int pos, const QString &tips); @@ -122,6 +122,8 @@ public slots: virtual void focusOutEvent(QFocusEvent *event) override; virtual void keyPressEvent(QKeyEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override; + virtual void mousePressEvent(QMouseEvent *event) override; + virtual void mouseReleaseEvent(QMouseEvent *event) override; virtual bool eventFilter(QObject *obj, QEvent *event) override; virtual bool event(QEvent *event) override; diff --git a/src/plugins/codeeditor/gui/workspacewidget.cpp b/src/plugins/codeeditor/gui/workspacewidget.cpp index 4526c5eb..cbf5857c 100644 --- a/src/plugins/codeeditor/gui/workspacewidget.cpp +++ b/src/plugins/codeeditor/gui/workspacewidget.cpp @@ -1312,12 +1312,17 @@ void WorkspaceWidget::setRangeBackgroundColor(const QString &fileName, int start } } -void WorkspaceWidget::clearRangeBackgroundColor(const QString &fileName, int startLine, int endLine, int marker) +Edit::Range WorkspaceWidget::getBackgroundRange(const QString &fileName, int marker) { + Edit::Range range; for (auto tabWidget : d->tabWidgetList) { - if (tabWidget->clearRangeBackground(fileName, startLine, endLine, marker)) + bool found = false; + range = tabWidget->getBackgroundRange(fileName, marker, found); + if (found) break; } + + return range; } void WorkspaceWidget::clearAllBackgroundColor(const QString &fileName, int marker) diff --git a/src/plugins/codeeditor/gui/workspacewidget.h b/src/plugins/codeeditor/gui/workspacewidget.h index 884b76aa..0e7fa045 100644 --- a/src/plugins/codeeditor/gui/workspacewidget.h +++ b/src/plugins/codeeditor/gui/workspacewidget.h @@ -62,7 +62,7 @@ class WorkspaceWidget : public QWidget int backgroundMarkerDefine(const QString &fileName, const QColor &color, int defaultMarker); void setRangeBackgroundColor(const QString &fileName, int startLine, int endLine, int marker); - void clearRangeBackgroundColor(const QString &fileName, int startLine, int endLine, int marker); + dpfservice::Edit::Range getBackgroundRange(const QString &fileName, int marker); void clearAllBackgroundColor(const QString &fileName, int marker); void showLineWidget(int line, QWidget *widget); void closeLineWidget(); diff --git a/src/plugins/codegeex/copilot.cpp b/src/plugins/codegeex/copilot.cpp index 64cc61a4..05f2b032 100644 --- a/src/plugins/codegeex/copilot.cpp +++ b/src/plugins/codegeex/copilot.cpp @@ -155,9 +155,16 @@ void Copilot::insterText(const QString &text) void Copilot::setGenerateCodeEnabled(bool enabled) { + if (!enabled && generateTimer->isActive()) + generateTimer->stop(); generateCodeEnabled = enabled; } +bool Copilot::getGenerateCodeEnabled() const +{ + return generateCodeEnabled; +} + void Copilot::setLocale(const QString &locale) { this->locale = locale; @@ -175,6 +182,9 @@ void Copilot::setCurrentModel(CodeGeeX::languageModel model) void Copilot::handleTextChanged() { + if (!generateCodeEnabled) + return; + editorService->setCompletion(""); QMetaObject::invokeMethod(this, [this]() { generateTimer->start(500); diff --git a/src/plugins/codegeex/copilot.h b/src/plugins/codegeex/copilot.h index 66be680d..42e063de 100644 --- a/src/plugins/codegeex/copilot.h +++ b/src/plugins/codegeex/copilot.h @@ -28,6 +28,7 @@ class Copilot : public QObject void replaceSelectedText(const QString &text); void insterText(const QString &text); void setGenerateCodeEnabled(bool enabled); + bool getGenerateCodeEnabled() const; void setLocale(const QString &locale); void setCommitsLocale(const QString &locale); void setCurrentModel(CodeGeeX::languageModel model); @@ -78,7 +79,7 @@ public slots: CodeGeeX::CopilotApi::GenerateType generateType; CodeGeeX::CopilotApi::GenerateType checkPrefixType(const QString &prefixCode); - bool generateCodeEnabled = true; + QAtomicInteger generateCodeEnabled = true; }; #endif // COPILOT_H diff --git a/src/plugins/codegeex/widgets/inlinechatwidget.cpp b/src/plugins/codegeex/widgets/inlinechatwidget.cpp index 309feef1..d8697e85 100644 --- a/src/plugins/codegeex/widgets/inlinechatwidget.cpp +++ b/src/plugins/codegeex/widgets/inlinechatwidget.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,7 @@ constexpr char UrlSSEChat[] = "https://codegeex.cn/prod/code/chatCodeSseV3/chat" using namespace dpfservice; DWIDGET_USE_NAMESPACE +DGUI_USE_NAMESPACE class InlineChatWidgetPrivate : public QObject { @@ -101,6 +103,7 @@ class InlineChatWidgetPrivate : public QObject void processGeneratedData(const QString &data); void updateButtonIcon(); QString createPrompt(const QString &question, bool useChunk, bool useContext); + Edit::Range calculateTextRange(const QString &fileName, const Edit::Position &pos); public: InlineChatWidget *q; @@ -125,8 +128,9 @@ class InlineChatWidgetPrivate : public QObject State state { None }; State prevState { None }; CodeGeeX::AskApi askApi; - int deleteMarker = -1; - int insertMarker = -1; + int deleteMarker { -1 }; + int insertMarker { -1 }; + int selectionMarker { -1 }; }; InlineChatWidgetPrivate::InlineChatWidgetPrivate(InlineChatWidget *qq) @@ -184,18 +188,18 @@ void InlineChatWidgetPrivate::initUI() QHBoxLayout *btnLayout = new QHBoxLayout; btnLayout->setContentsMargins(0, 0, 0, 0); spinner = new DSpinner(q); - spinner->setFixedSize({ 16, 16 }); + spinner->setFixedSize({ 12, 12 }); spinner->setProperty(VisibleProperty, SubmitStart | QuestionStart); escBtn = createButton(InlineChatWidget::tr("Esc to close"), PushButton, Original | QuestionComplete); - submitBtn = createButton(InlineChatWidget::tr("Submit Edit"), SuggestButton, ReadyAsk); + submitBtn = createButton(InlineChatWidget::tr("Submit Edit"), SuggestButton, None); questionBtn = createButton(InlineChatWidget::tr("quick question"), PushButton, ReadyAsk); - questionBtn->setIconSize({ 42, 16 }); + questionBtn->setIconSize({ 24, 12 }); stopBtn = createButton(InlineChatWidget::tr("Stop"), PushButton, SubmitStart | QuestionStart); - stopBtn->setIconSize({ 42, 16 }); + stopBtn->setIconSize({ 36, 12 }); acceptBtn = createButton(InlineChatWidget::tr("Accept"), SuggestButton, SubmitComplete); - acceptBtn->setIconSize({ 42, 16 }); + acceptBtn->setIconSize({ 36, 12 }); rejectBtn = createButton(InlineChatWidget::tr("Reject"), PushButton, SubmitComplete); - rejectBtn->setIconSize({ 42, 16 }); + rejectBtn->setIconSize({ 36, 12 }); btnLayout->addWidget(spinner); btnLayout->addWidget(escBtn); @@ -219,8 +223,8 @@ void InlineChatWidgetPrivate::initUI() void InlineChatWidgetPrivate::initConnection() { connect(edit, &InputEdit::textChanged, this, &InlineChatWidgetPrivate::handleTextChanged); - connect(closeBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleReject); - connect(escBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleReject); + connect(closeBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleClose); + connect(escBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleClose); connect(submitBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleSubmitEdit); connect(questionBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleQuickQuestion); connect(acceptBtn, &QAbstractButton::clicked, this, &InlineChatWidgetPrivate::handleAccept); @@ -246,12 +250,13 @@ QAbstractButton *InlineChatWidgetPrivate::createButton(const QString &name, Butt break; } + btn->setFixedHeight(24); btn->setProperty(VisibleProperty, flags); if (!name.isEmpty()) btn->setText(name); auto f = btn->font(); - f.setPointSize(12); + f.setPixelSize(12); btn->setFont(f); return btn; } @@ -315,6 +320,11 @@ void InlineChatWidgetPrivate::handleTextChanged() void InlineChatWidgetPrivate::handleSubmitEdit() { + if (state == FollowSubmit) { + handleReject(); + setState(FollowSubmit); + } + setState(SubmitStart); askForCodeGeeX(); } @@ -362,11 +372,14 @@ void InlineChatWidgetPrivate::handleAccept() if (!replaceText.endsWith('\n')) replaceText.append('\n'); + bool enabled = Copilot::instance()->getGenerateCodeEnabled(); + Copilot::instance()->setGenerateCodeEnabled(false); int endLineOffset = chatInfo.tempText.count('\n') - chatInfo.originalText.count('\n') - 1; Edit::Range replaceRange = chatInfo.originalRange; replaceRange.end.line += endLineOffset; editSrv->replaceRange(chatInfo.fileName, replaceRange, replaceText); handleClose(); + Copilot::instance()->setGenerateCodeEnabled(enabled); } void InlineChatWidgetPrivate::handleReject() @@ -376,12 +389,23 @@ void InlineChatWidgetPrivate::handleReject() if (!replaceText.endsWith('\n')) replaceText.append('\n'); + bool enabled = Copilot::instance()->getGenerateCodeEnabled(); + Copilot::instance()->setGenerateCodeEnabled(false); int endLineOffset = chatInfo.tempText.count('\n') - chatInfo.originalText.count('\n') - 1; Edit::Range replaceRange = chatInfo.originalRange; replaceRange.end.line += endLineOffset; editSrv->replaceRange(chatInfo.fileName, replaceRange, replaceText); + editSrv->setRangeBackgroundColor(chatInfo.fileName, chatInfo.originalRange.start.line, + chatInfo.originalRange.end.line, selectionMarker); + Copilot::instance()->setGenerateCodeEnabled(enabled); } - handleClose(); + + if (insertMarker != -1) + editSrv->clearAllBackgroundColor(chatInfo.fileName, insertMarker); + if (deleteMarker != -1) + editSrv->clearAllBackgroundColor(chatInfo.fileName, deleteMarker); + setState(Original); + q->setFocus(); } void InlineChatWidgetPrivate::handleClose() @@ -393,6 +417,7 @@ void InlineChatWidgetPrivate::handleStop() { setState(prevState); edit->setPlainText(questionLabel->text()); + edit->moveCursor(QTextCursor::End); foreach (auto watcher, futureWatcherList) { if (!watcher->isFinished()) watcher->cancel(); @@ -418,13 +443,23 @@ void InlineChatWidgetPrivate::defineBackgroundMarker(const QString &fileName) { QColor insBgColor(230, 240, 208); QColor delBgColor(242, 198, 196); + QColor selBgColor(227, 227, 227); + if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType) { + insBgColor.setRgb(30, 78, 42); + delBgColor.setRgb(87, 32, 49); + selBgColor.setRgb(49, 53, 59); + } insertMarker = editSrv->backgroundMarkerDefine(fileName, insBgColor, insertMarker); deleteMarker = editSrv->backgroundMarkerDefine(fileName, delBgColor, deleteMarker); + selectionMarker = editSrv->backgroundMarkerDefine(fileName, selBgColor, selectionMarker); } void InlineChatWidgetPrivate::askForCodeGeeX() { + chatInfo.originalRange = editSrv->getBackgroundRange(chatInfo.fileName, selectionMarker); + chatInfo.originalText = editSrv->rangeText(chatInfo.fileName, chatInfo.originalRange); + auto f = questionLabel->font(); f.setUnderline(state == QuestionStart); questionLabel->setFont(f); @@ -465,6 +500,7 @@ QList InlineChatWidgetPrivate::diffText(const QString &str1, const QString void InlineChatWidgetPrivate::processGeneratedData(const QString &data) { + editSrv->clearAllBackgroundColor(chatInfo.fileName, selectionMarker); chatInfo.operationRange.clear(); if (chatInfo.originalText.isEmpty()) chatInfo.diffList << Diff { INSERT, data }; @@ -490,15 +526,17 @@ void InlineChatWidgetPrivate::processGeneratedData(const QString &data) tempText.append(diff.text); } + bool enabled = Copilot::instance()->getGenerateCodeEnabled(); + Copilot::instance()->setGenerateCodeEnabled(false); chatInfo.tempText = tempText; editSrv->replaceRange(chatInfo.fileName, chatInfo.originalRange, tempText); - defineBackgroundMarker(chatInfo.fileName); auto iter = chatInfo.operationRange.cbegin(); for (; iter != chatInfo.operationRange.cend(); ++iter) { const auto &range = iter.value(); iter.key() == DELETE ? editSrv->setRangeBackgroundColor(chatInfo.fileName, range.start.line, range.end.line, deleteMarker) : editSrv->setRangeBackgroundColor(chatInfo.fileName, range.start.line, range.end.line, insertMarker); } + Copilot::instance()->setGenerateCodeEnabled(enabled); } void InlineChatWidgetPrivate::updateButtonIcon() @@ -560,6 +598,36 @@ QString InlineChatWidgetPrivate::createPrompt(const QString &question, bool useC return prompt.join('\n'); } +Edit::Range InlineChatWidgetPrivate::calculateTextRange(const QString &fileName, const Edit::Position &pos) +{ + Edit::Range textRange; + const auto &selRange = editSrv->selectionRange(fileName); + auto codeRange = editSrv->codeRange(fileName, pos); + if (selRange.start.line == -1) { + if (codeRange.start.line == -1) { + textRange.start.line = pos.line; + textRange.end.line = pos.line; + } else if (codeRange.start.line == pos.line) { + textRange.start.line = codeRange.start.line; + textRange.end.line = codeRange.end.line; + } else { + textRange.start.line = pos.line; + textRange.end.line = codeRange.end.line; + } + } else { + if (selRange.start.line == selRange.end.line + && selRange.start.line == codeRange.start.line) { + textRange.start.line = codeRange.start.line; + textRange.end.line = codeRange.end.line; + } else { + textRange.start.line = selRange.start.line; + textRange.end.line = selRange.end.line; + } + } + + return textRange; +} + InlineChatWidget::InlineChatWidget(QWidget *parent) : QWidget(parent), d(new InlineChatWidgetPrivate(this)) @@ -580,33 +648,13 @@ void InlineChatWidget::start() if (d->chatInfo.fileName.isEmpty()) return; - const auto &selRange = d->editSrv->selectionRange(d->chatInfo.fileName); auto pos = d->editSrv->cursorPosition(); - auto codeRange = d->editSrv->codeRange(d->chatInfo.fileName, pos); - if (selRange.start.line == -1) { - if (codeRange.start.line == -1) { - d->chatInfo.originalRange.start.line = pos.line; - d->chatInfo.originalRange.end.line = pos.line; - } else if (codeRange.start.line == pos.line) { - d->chatInfo.originalRange.start.line = codeRange.start.line; - d->chatInfo.originalRange.end.line = codeRange.end.line; - } else { - d->chatInfo.originalRange.start.line = pos.line; - d->chatInfo.originalRange.end.line = codeRange.end.line; - } - } else { - if (selRange.start.line == selRange.end.line - && selRange.start.line == codeRange.start.line) { - d->chatInfo.originalRange.start.line = codeRange.start.line; - d->chatInfo.originalRange.end.line = codeRange.end.line; - } else { - d->chatInfo.originalRange.start.line = selRange.start.line; - d->chatInfo.originalRange.end.line = selRange.end.line; - } - } + const auto &textRange = d->calculateTextRange(d->chatInfo.fileName, pos); + d->editSrv->showLineWidget(textRange.start.line, this); - d->chatInfo.originalText = d->editSrv->rangeText(d->chatInfo.fileName, d->chatInfo.originalRange); - d->editSrv->showLineWidget(d->chatInfo.originalRange.start.line, this); + d->defineBackgroundMarker(d->chatInfo.fileName); + d->editSrv->setRangeBackgroundColor(d->chatInfo.fileName, textRange.start.line, + textRange.end.line, d->selectionMarker); } void InlineChatWidget::showEvent(QShowEvent *e) @@ -622,7 +670,7 @@ void InlineChatWidget::keyPressEvent(QKeyEvent *e) switch (e->modifiers()) { case Qt::NoModifier: if (e->key() == Qt::Key_Escape) { - d->handleReject(); + d->handleClose(); return; } default: @@ -633,10 +681,10 @@ void InlineChatWidget::keyPressEvent(QKeyEvent *e) void InlineChatWidget::hideEvent(QHideEvent *e) { - if (d->insertMarker != -1) - d->editSrv->clearAllBackgroundColor(d->chatInfo.fileName, d->insertMarker); - if (d->deleteMarker != -1) - d->editSrv->clearAllBackgroundColor(d->chatInfo.fileName, d->deleteMarker); + d->handleReject(); + if (d->selectionMarker != -1) + d->editSrv->clearAllBackgroundColor(d->chatInfo.fileName, d->selectionMarker); + QWidget::hideEvent(e); } @@ -683,18 +731,29 @@ bool InlineChatWidget::eventFilter(QObject *obj, QEvent *e) break; } } - case Qt::NoModifier: { + case Qt::AltModifier: { switch (ke->key()) { case Qt::Key_Enter: case Qt::Key_Return: - if (d->submitBtn->isVisible()) { - d->handleSubmitEdit(); + if (d->edit->isVisible()) { + d->edit->insertPlainText("\n"); return true; } break; default: break; } + } + case Qt::NoModifier: { + switch (ke->key()) { + case Qt::Key_Enter: + case Qt::Key_Return: + if (d->submitBtn->isVisible()) + d->handleSubmitEdit(); + return true; + default: + break; + } } break; } } else if (e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut) { diff --git a/src/plugins/find/findplugin.cpp b/src/plugins/find/findplugin.cpp index ed47d1fa..56914eea 100644 --- a/src/plugins/find/findplugin.cpp +++ b/src/plugins/find/findplugin.cpp @@ -18,7 +18,6 @@ constexpr char M_FIND[] = "Find.FindMenu"; - using namespace dpfservice; void FindPlugin::initialize() @@ -59,11 +58,14 @@ void FindPlugin::registerShortcut() cmd->setDefaultKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_F); mFind->addAction(cmd, "Find.FindMenu.Advanced"); connect(advancedFindAction, &QAction::triggered, qApp, [=] { + windowService->switchWidgetNavigation(MWNA_ADVANCEDSEARCH); + if (!advSearchWidget) + return; + auto editSrv = dpfGetService(EditorService); const auto &selectedText = editSrv->getSelectedText(); if (!selectedText.isEmpty()) advSearchWidget->setSearchText(selectedText); - windowService->switchWidgetNavigation(MWNA_ADVANCEDSEARCH); }); } @@ -80,14 +82,13 @@ void FindPlugin::registerToSidebar() auto actionImpl = new AbstractAction(action); windowService->addNavigationItem(actionImpl, Priority::highest); - std::function findCreator = []()->AbstractWidget * { - auto advancedSearchWidget = new AdvancedSearchWidget(); - advancedSearchWidget->initOperator(); - return new AbstractWidget(advancedSearchWidget); + std::function findCreator = [this]() -> AbstractWidget * { + advSearchWidget = new AdvancedSearchWidget(); + advSearchWidget->initOperator(); + return new AbstractWidget(advSearchWidget); }; windowService->registerWidgetCreator(MWNA_ADVANCEDSEARCH, findCreator); - windowService->setDockHeaderName(MWNA_ADVANCEDSEARCH, tr("ADVANCED SEARCH")); windowService->bindWidgetToNavigation(MWNA_ADVANCEDSEARCH, actionImpl); diff --git a/src/services/editor/editorservice.h b/src/services/editor/editorservice.h index 8823b6e5..1bad0d0c 100644 --- a/src/services/editor/editorservice.h +++ b/src/services/editor/editorservice.h @@ -71,7 +71,7 @@ class EditorService final : public dpf::PluginService, // if the return value is -1, it indicates that the setting failed. DPF_INTERFACE(int, backgroundMarkerDefine, const QString &file, const QColor &color, int defaultMarker); DPF_INTERFACE(void, setRangeBackgroundColor, const QString &file, int startLine, int endLine, int marker); - DPF_INTERFACE(void, clearRangeBackgroundColor, const QString &file, int startLine, int endLine, int marker); + DPF_INTERFACE(Edit::Range, getBackgroundRange, const QString &file, int marker); DPF_INTERFACE(void, clearAllBackgroundColor, const QString &file, int marker); DPF_INTERFACE(void, showLineWidget, int line, QWidget *widget); DPF_INTERFACE(void, closeLineWidget);