Skip to content

Commit

Permalink
fix: [inline chat] Fix some issues of Inline Chat
Browse files Browse the repository at this point in the history
as title

Log: fix issues
  • Loading branch information
Kakueeen authored and deepin-mozart committed Oct 9, 2024
1 parent 2b77889 commit 264a318
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/plugins/codeeditor/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
68 changes: 42 additions & 26 deletions src/plugins/codeeditor/gui/private/texteditor_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/codeeditor/gui/private/texteditor_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class TextEditorPrivate : public QObject
RuntimeLineBackground
};

struct MarkerRange
{
int startLine = -1;
int endLine = -1;
};

explicit TextEditorPrivate(TextEditor *qq);

void init();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -98,6 +105,8 @@ public slots:
QTimer selectionChangeTimer;
DTK_WIDGET_NAMESPACE::DFloatingWidget *lineWidgetContainer { nullptr };
int showAtLine { 0 };
bool leftButtonPressed { false };
QMap<int, MarkerRange> markerCache;
};

#endif // TEXTEDITOR_P_H
11 changes: 7 additions & 4 deletions src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codeeditor/gui/tabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 25 additions & 7 deletions src/plugins/codeeditor/gui/texteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/codeeditor/gui/texteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 7 additions & 2 deletions src/plugins/codeeditor/gui/workspacewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codeeditor/gui/workspacewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/codegeex/copilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/codegeex/copilot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -78,7 +79,7 @@ public slots:
CodeGeeX::CopilotApi::GenerateType generateType;
CodeGeeX::CopilotApi::GenerateType checkPrefixType(const QString &prefixCode);

bool generateCodeEnabled = true;
QAtomicInteger<bool> generateCodeEnabled = true;
};

#endif // COPILOT_H
Loading

0 comments on commit 264a318

Please sign in to comment.