Skip to content

Commit

Permalink
feat: [editor] New feature for in line chat
Browse files Browse the repository at this point in the history
as title

Log: new feature
  • Loading branch information
Kakueeen committed Sep 6, 2024
1 parent 3b9e964 commit dd12fb9
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 79 deletions.
4 changes: 1 addition & 3 deletions src/common/util/eventdefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ OPI_OBJECT(editor,
OPI_INTERFACE(clearAllAnnotation, "title")
OPI_INTERFACE(setDebugLine, "fileName", "line")
OPI_INTERFACE(removeDebugLine)
OPI_INTERFACE(setLineBackgroundColor, "fileName", "line", "color")
OPI_INTERFACE(resetLineBackgroundColor, "fileName", "line")
OPI_INTERFACE(clearLineBackgroundColor, "fileName")
OPI_INTERFACE(setModifiedAutoReload, "fileName", "flag")
OPI_INTERFACE(addBreakpoint, "fileName", "line", "enabled")
OPI_INTERFACE(removeBreakpoint, "fileName", "line")
Expand All @@ -74,6 +71,7 @@ OPI_OBJECT(editor,
OPI_INTERFACE(breakpointStatusChanged, "fileName", "line", "enabled")
OPI_INTERFACE(textChanged)
OPI_INTERFACE(cursorPositionChanged, "fileName", "line", "index")
OPI_INTERFACE(selectionChanged, "fileName", "lineFrom", "indexFrom", "lineTo", "indexTo")

//right-cliked menu, Related to debugging
OPI_INTERFACE(setBreakpointCondition, "fileName", "line")
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/codeeditor/codeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ void CodeEditor::initEditorService()
editorService->fileText = std::bind(&WorkspaceWidget::fileText, workspaceWidget, _1);
editorService->replaceAll = std::bind(&WorkspaceWidget::replaceAll, workspaceWidget, _1, _2, _3, _4, _5);
editorService->replaceRange = std::bind(&WorkspaceWidget::replaceRange, workspaceWidget, _1, _2, _3, _4, _5);
editorService->setRangeBackgroundColor = std::bind(&WorkspaceWidget::setRangeBackgroundColor, workspaceWidget, _1, _2, _3, _4);
editorService->clearRangeBackgroundColor = std::bind(&WorkspaceWidget::clearRangeBackgroundColor, workspaceWidget, _1, _2, _3, _4);
editorService->clearAllBackgroundColor = std::bind(&WorkspaceWidget::clearAllBackgroundColor, workspaceWidget, _1, _2);
editorService->showLineWidget = std::bind(&WorkspaceWidget::showLineWidget, workspaceWidget, _1, _2);

LexerManager::instance()->init(editorService);
}
Expand Down
3 changes: 0 additions & 3 deletions src/plugins/codeeditor/gui/private/tabwidget_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public slots:
void handleAddAnnotation(const QString &fileName, const QString &title, const QString &content, int line, AnnotationType type);
void handleRemoveAnnotation(const QString &fileName, const QString &title);
void handleClearAllAnnotation(const QString &title);
void handleSetLineBackgroundColor(const QString &fileName, int line, const QColor &color);
void handleResetLineBackground(const QString &fileName, int line);
void handleClearLineBackground(const QString &fileName);
void handleDoRename(const newlsp::WorkspaceEdit &info);
void handleOpenFiles(const QList<QUrl> &fileList);

Expand Down
83 changes: 78 additions & 5 deletions src/plugins/codeeditor/gui/private/texteditor_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ void TextEditorPrivate::init()
q->SendScintilla(TextEditor::SCI_AUTOCSETCASEINSENSITIVEBEHAVIOUR,
TextEditor::SC_CASEINSENSITIVEBEHAVIOUR_IGNORECASE);

lineWidgetContainer = new QFrame(q, Qt::Tool | Qt::FramelessWindowHint);
lineWidgetContainer->setLayout(new QVBoxLayout);
lineWidgetContainer->setContentsMargins(0, 0, 0, 0);
lineWidgetContainer->installEventFilter(q);
if (mainWindow())
mainWindow()->installEventFilter(q);

initMargins();
updateColorTheme();
updateSettings();
Expand All @@ -69,6 +76,7 @@ void TextEditorPrivate::initConnection()
q->cancelTips();
});

connect(q->verticalScrollBar(), &QScrollBar::valueChanged, this, &TextEditorPrivate::updateLineWidgetPosition);
connect(q, &TextEditor::SCN_ZOOM, q, &TextEditor::zoomValueChanged);
connect(q, &TextEditor::SCN_DWELLSTART, this, &TextEditorPrivate::onDwellStart);
connect(q, &TextEditor::SCN_DWELLEND, this, &TextEditorPrivate::onDwellEnd);
Expand All @@ -91,13 +99,12 @@ void TextEditorPrivate::initMargins()
q->setMarginMarkerMask(SymbolMargin,
1 << Breakpoint | 1 << BreakpointDisabled
| 1 << Bookmark | 1 << Runtime
| 1 << RuntimeLineBackground | 1 << CustomLineBackground);
| 1 << RuntimeLineBackground);

q->markerDefine(TextEditor::RightTriangle, Bookmark);
q->setMarkerBackgroundColor(QColor(Qt::red), Bookmark);

q->markerDefine(TextEditor::Background, RuntimeLineBackground);
q->markerDefine(TextEditor::Background, CustomLineBackground);
}

void TextEditorPrivate::updateColorTheme()
Expand Down Expand Up @@ -440,7 +447,7 @@ QMap<int, int> TextEditorPrivate::allMarkers()
if (mask != 0)
markers.insert(line, mask);
}

return markers;
}

Expand All @@ -450,7 +457,7 @@ void TextEditorPrivate::setMarkers(const QMap<int, int> &maskMap)
for (auto iter = maskMap.begin(); iter != maskMap.end(); ++iter) {
if (iter.key() >= totalLine)
break;

if (iter.value() & (1 << Breakpoint)) {
q->addBreakpoint(iter.key(), true);
} else if (iter.value() & (1 << BreakpointDisabled)) {
Expand All @@ -459,6 +466,66 @@ void TextEditorPrivate::setMarkers(const QMap<int, int> &maskMap)
}
}

QWidget *TextEditorPrivate::mainWindow()
{
static QWidget *mw { nullptr };
if (mw)
return mw;

for (auto w : qApp->allWidgets()) {
if (w->objectName() == "MainWindow") {
mw = w;
break;
}
}

return mw;
}

void TextEditorPrivate::setContainerWidget(QWidget *widget)

Check warning on line 485 in src/plugins/codeeditor/gui/private/texteditor_p.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'widget' shadows outer argument

Check warning on line 485 in src/plugins/codeeditor/gui/private/texteditor_p.cpp

View workflow job for this annotation

GitHub Actions / static-check / Call-CppCheck

Local variable 'widget' shadows outer argument
{
auto layout = lineWidgetContainer->layout();
while (auto item = layout->takeAt(0)) {
if (QWidget *widget = item->widget())
widget->setVisible(false);
delete item;
}

widget->setVisible(true);
lineWidgetContainer->setFocusProxy(widget);
lineWidgetContainer->layout()->addWidget(widget);
lineWidgetContainer->show();
updateLineWidgetPosition();
}

void TextEditorPrivate::updateLineWidgetPosition()
{
if (!lineWidgetContainer->isVisible() || showAtLine < 0 || showAtLine > q->lines() - 1)
return;

int pos = q->positionFromLineIndex(showAtLine, 0);
auto point = q->mapToGlobal(q->pointFromPosition(pos));
auto displayY = point.y() - lineWidgetContainer->height();

auto rect = q->rect();
auto rectTL = q->mapToGlobal(rect.topLeft());
auto rectBL = q->mapToGlobal(rect.bottomLeft());

// NOTE: upate the `lineWidgetContainer` position
// 1.It is displayed above `showAtLine` by default
// 2.The `lineWidgetContainer` does not extend beyond the top and bottom of the editor
// 3.When the `lineWidgetContainer` will block the `showAtLine`, display it below the `showAtLine`
if (displayY < rectTL.y()) {
displayY = point.y() + q->textHeight(showAtLine);
if (displayY < rectTL.y())
displayY = rectTL.y();
} else if (displayY > rectBL.y() - lineWidgetContainer->height()) {
displayY = rectBL.y() - lineWidgetContainer->height();
}

lineWidgetContainer->move(point.x(), displayY);
}

void TextEditorPrivate::resetThemeColor()
{
if (q->lexer()) {
Expand Down Expand Up @@ -505,11 +572,17 @@ void TextEditorPrivate::onModified(int pos, int mtype, const QString &text, int
contentsChanged = true;
if (isAutoCompletionEnabled && !text.isEmpty())
editor.textChanged();

if (added != 0) {
int line = 0, index = 0;
q->lineIndexFromPosition(pos, &line, &index);
editor.lineChanged(fileName, line + 1, added);
if (lineWidgetContainer->isVisible() && added != 0) {
if (showAtLine > line) {
showAtLine += added;
updateLineWidgetPosition();
}
}
}

if (mtype & TextEditor::SC_MOD_INSERTTEXT) {
Expand Down
14 changes: 10 additions & 4 deletions src/plugins/codeeditor/gui/private/texteditor_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class TextEditorPrivate : public QObject
BreakpointDisabled,
Bookmark,
Runtime,
RuntimeLineBackground,
CustomLineBackground
RuntimeLineBackground
};

explicit TextEditorPrivate(TextEditor *qq);
Expand All @@ -56,6 +55,10 @@ class TextEditorPrivate : public QObject
QMap<int, int> allMarkers();
void setMarkers(const QMap<int, int> &maskMap);

QWidget *mainWindow();
void setContainerWidget(QWidget *widget);
void updateLineWidgetPosition();

public slots:
void resetThemeColor();
void onDwellStart(int position, int x, int y);
Expand All @@ -71,7 +74,7 @@ public slots:
int preFirstLineNum { 0 };
int lastCursorPos { 0 };
QMultiHash<QString, int> annotationRecords;

LanguageClientHandler *languageClient { nullptr };
bool isAutoCompletionEnabled { false };

Expand All @@ -83,8 +86,11 @@ public slots:
int fontSize { 10 };

CodeCompletionWidget *completionWidget { nullptr };

QMap<QString, QVariant> commentSettings;

std::tuple<int, int, int, int> selectionCache { -1, -1, -1, -1 };
QFrame *lineWidgetContainer { nullptr };
int showAtLine { 0 };
};

#endif // TEXTEDITOR_P_H
63 changes: 42 additions & 21 deletions src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ void TabWidgetPrivate::initConnection()
connect(EditorCallProxy::instance(), &EditorCallProxy::reqAddAnnotation, this, &TabWidgetPrivate::handleAddAnnotation);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqRemoveAnnotation, this, &TabWidgetPrivate::handleRemoveAnnotation);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqClearAllAnnotation, this, &TabWidgetPrivate::handleClearAllAnnotation);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqSetLineBackgroundColor, this, &TabWidgetPrivate::handleSetLineBackgroundColor);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqResetLineBackground, this, &TabWidgetPrivate::handleResetLineBackground);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqClearLineBackground, this, &TabWidgetPrivate::handleClearLineBackground);
connect(EditorCallProxy::instance(), &EditorCallProxy::reqDoRename, this, &TabWidgetPrivate::handleDoRename);
}

Expand Down Expand Up @@ -439,24 +436,6 @@ void TabWidgetPrivate::handleClearAllAnnotation(const QString &title)
editor->removeAnnotation(title);
}

void TabWidgetPrivate::handleSetLineBackgroundColor(const QString &fileName, int line, const QColor &color)
{
if (auto editor = findEditor(fileName))
editor->setLineBackgroundColor(line, color);
}

void TabWidgetPrivate::handleResetLineBackground(const QString &fileName, int line)
{
if (auto editor = findEditor(fileName))
editor->resetLineBackgroundColor(line);
}

void TabWidgetPrivate::handleClearLineBackground(const QString &fileName)
{
if (auto editor = findEditor(fileName))
editor->clearLineBackgroundColor();
}

void TabWidgetPrivate::handleDoRename(const newlsp::WorkspaceEdit &info)
{
if (info.changes) {
Expand Down Expand Up @@ -861,6 +840,48 @@ void TabWidget::updateZoomValue(int value)
}
}

bool TabWidget::setRangeBackgroundColor(const QString &fileName, int startLine, int endLine, const QColor &color, int &marker)
{
if (auto editor = d->findEditor(fileName)) {
marker = editor->setRangeBackgroundColor(startLine, endLine, color);
return true;
}

return false;
}

bool TabWidget::clearRangeBackground(const QString &fileName, int startLine, int endLine, int marker)
{
if (auto editor = d->findEditor(fileName)) {
editor->clearRangeBackgroundColor(startLine, endLine, marker);
return true;
}

return false;
}

bool TabWidget::clearAllBackground(const QString &fileName, int marker)
{
if (auto editor = d->findEditor(fileName)) {
editor->clearAllBackgroundColor(marker);
return true;
}

return false;
}

void TabWidget::showLineWidget(int line, QWidget *widget)
{
if (auto editor = d->currentTextEditor())
editor->showLineWidget(line, widget);
}

void TabWidget::closeLineWidget()
{
if (auto editor = d->currentTextEditor())
editor->closeLineWidget();
}

TextEditor *TabWidget::currentEditor() const
{
return d->currentTextEditor();
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/codeeditor/gui/tabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class TabWidget : public QWidget
int zoomValue();
void updateZoomValue(int value);

bool setRangeBackgroundColor(const QString &fileName, int startLine, int endLine, const QColor &color, int &marker);
bool clearRangeBackground(const QString &fileName, int startLine, int endLine, int marker);
bool clearAllBackground(const QString &fileName, int marker);
void showLineWidget(int line, QWidget *widget);
void closeLineWidget();

TextEditor *currentEditor() const;
TextEditor *findEditor(const QString &fileName);

Expand Down
Loading

0 comments on commit dd12fb9

Please sign in to comment.