From 32f6732fad215ff546cbd66ec28a108208c7f74d Mon Sep 17 00:00:00 2001 From: liuzhangjian Date: Mon, 12 Aug 2024 09:55:19 +0800 Subject: [PATCH] fix: [editor] Fixed an issue where documents could not be synchronized as title Log: fix issue --- .../codeeditor/gui/private/tabwidget_p.h | 3 +- .../gui/private/workspacewidget_p.h | 2 +- src/plugins/codeeditor/gui/tabbar.cpp | 24 +++--------- src/plugins/codeeditor/gui/tabbar.h | 3 +- src/plugins/codeeditor/gui/tabwidget.cpp | 38 ++++++++----------- src/plugins/codeeditor/gui/tabwidget.h | 5 ++- src/plugins/codeeditor/gui/texteditor.cpp | 34 ++++++++++++----- src/plugins/codeeditor/gui/texteditor.h | 4 +- .../codeeditor/gui/workspacewidget.cpp | 23 ++++++----- 9 files changed, 66 insertions(+), 70 deletions(-) diff --git a/src/plugins/codeeditor/gui/private/tabwidget_p.h b/src/plugins/codeeditor/gui/private/tabwidget_p.h index 17e95ba70..c364d9f9a 100644 --- a/src/plugins/codeeditor/gui/private/tabwidget_p.h +++ b/src/plugins/codeeditor/gui/private/tabwidget_p.h @@ -38,7 +38,7 @@ class TabWidgetPrivate : public QObject QWidget *createSpaceWidget(); QWidget *createFindPlaceHolder(); - TextEditor *createEditor(const QString &fileName = ""); + TextEditor *createEditor(const QString &fileName = "", QsciDocument *doc = nullptr); TextEditor *findEditor(const QString &fileName); TextEditor *currentTextEditor() const; void changeFocusProxy(); @@ -53,7 +53,6 @@ public slots: void onTabClosed(const QString &fileName); void onSpliterClicked(Qt::Orientation ori); void onCursorRecordChanged(int pos); - void onFileChanged(const QString &fileName); 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); diff --git a/src/plugins/codeeditor/gui/private/workspacewidget_p.h b/src/plugins/codeeditor/gui/private/workspacewidget_p.h index 24ea3330b..972bac218 100644 --- a/src/plugins/codeeditor/gui/private/workspacewidget_p.h +++ b/src/plugins/codeeditor/gui/private/workspacewidget_p.h @@ -26,7 +26,7 @@ class WorkspaceWidgetPrivate : public QObject void connectTabWidgetSignals(TabWidget *tabWidget); TabWidget *currentTabWidget() const; - void doSplit(QSplitter *spliter, int index, const QString &fileName, int pos, int scroll); + void doSplit(TabWidget *fromTW, QSplitter *spliter, int index, const QString &fileName); int showFileChangedConfirmDialog(const QString &fileName); int showFileRemovedConfirmDialog(const QString &fileName); diff --git a/src/plugins/codeeditor/gui/tabbar.cpp b/src/plugins/codeeditor/gui/tabbar.cpp index 82516faf3..21e67036a 100644 --- a/src/plugins/codeeditor/gui/tabbar.cpp +++ b/src/plugins/codeeditor/gui/tabbar.cpp @@ -279,30 +279,16 @@ void TabBar::closeTab(int index) d->onTabColseRequested(index); } -void TabBar::onFileChanged(const QString &fileName, bool isModified) +void TabBar::onModificationChanged(const QString &fileName, bool isModified) { int index = indexOf(fileName); if (-1 == index) return; - QString changedFileName = QFileInfo(fileName).fileName(); + QString tabName = QFileInfo(fileName).fileName(); if (isModified) - changedFileName.prepend("*"); + tabName.prepend("*"); - if (d->tabBar->tabText(index) != changedFileName) - d->tabBar->setTabText(index, changedFileName); -} - -void TabBar::onFileSaved(const QString &fileName) -{ - int index = indexOf(fileName); - if (index == -1) - return; - - auto text = d->tabBar->tabText(index); - if (QFileInfo(fileName).fileName() == text) - return; - - text = text.remove(0, 1); - d->tabBar->setTabText(index, text); + if (d->tabBar->tabText(index) != tabName) + d->tabBar->setTabText(index, tabName); } diff --git a/src/plugins/codeeditor/gui/tabbar.h b/src/plugins/codeeditor/gui/tabbar.h index c5a3c2d68..d072a82d7 100644 --- a/src/plugins/codeeditor/gui/tabbar.h +++ b/src/plugins/codeeditor/gui/tabbar.h @@ -30,8 +30,7 @@ class TabBar : public QWidget public slots: void closeTab(int index); - void onFileChanged(const QString &fileName, bool isModified); - void onFileSaved(const QString &fileName); + void onModificationChanged(const QString &fileName, bool isModified); signals: void spliterClicked(Qt::Orientation ori); diff --git a/src/plugins/codeeditor/gui/tabwidget.cpp b/src/plugins/codeeditor/gui/tabwidget.cpp index 43fc5382f..f56d68bc6 100644 --- a/src/plugins/codeeditor/gui/tabwidget.cpp +++ b/src/plugins/codeeditor/gui/tabwidget.cpp @@ -228,7 +228,7 @@ QWidget *TabWidgetPrivate::createFindPlaceHolder() return windowService->createFindPlaceHolder(q, docFind); } -TextEditor *TabWidgetPrivate::createEditor(const QString &fileName) +TextEditor *TabWidgetPrivate::createEditor(const QString &fileName, QsciDocument *doc) { TextEditor *editor = new TextEditor(q); editor->updateLineNumberWidth(false); @@ -236,18 +236,17 @@ TextEditor *TabWidgetPrivate::createEditor(const QString &fileName) connect(editor, &TextEditor::zoomValueChanged, q, &TabWidget::zoomValueChanged); connect(editor, &TextEditor::cursorRecordChanged, this, &TabWidgetPrivate::onCursorRecordChanged); - connect(editor, &TextEditor::fileSaved, tabBar, &TabBar::onFileSaved); connect(editor, &TextEditor::requestOpenFiles, this, &TabWidgetPrivate::handleOpenFiles); connect(editor, &TextEditor::cursorPositionChanged, symbolBar, &SymbolBar::updateSymbol); + connect(editor, &TextEditor::modificationChanged, tabBar, std::bind(&TabBar::onModificationChanged, tabBar, fileName, std::placeholders::_1)); - editor->setFile(fileName); - editor->setCursorPosition(0, 0); - - connect(editor, &TextEditor::textChanged, this, - [this, fileName] { - onFileChanged(fileName); - }); + if (doc) { + editor->openFileWithDocument(fileName, *doc); + } else { + editor->openFile(fileName); + } + editor->setCursorPosition(0, 0); editorMng.insert(fileName, editor); recentOpenedFiles.prepend(fileName); @@ -418,18 +417,6 @@ void TabWidgetPrivate::onCursorRecordChanged(int pos) prePosRecord.takeFirst(); } -void TabWidgetPrivate::onFileChanged(const QString &fileName) -{ - auto editor = qobject_cast(sender()); - if (!editor) - return; - - // The correct value cannot be immediately get by `editor->isModified()` - QTimer::singleShot(50, tabBar, [this, editor, fileName] { - tabBar->onFileChanged(fileName, editor->isModified()); - }); -} - void TabWidgetPrivate::handleAddAnnotation(const QString &fileName, const QString &title, const QString &content, int line, AnnotationType type) { if (auto editor = findEditor(fileName)) @@ -875,7 +862,12 @@ QWidget *TabWidget::currentWidget() const return d->currentTextEditor(); } -void TabWidget::openFile(const QString &fileName) +TextEditor *TabWidget::findEditor(const QString &fileName) +{ + return d->findEditor(fileName); +} + +void TabWidget::openFile(const QString &fileName, QsciDocument *doc) { if (!QFile::exists(fileName) || QFileInfo(fileName).isDir()) return; @@ -891,7 +883,7 @@ void TabWidget::openFile(const QString &fileName) d->symbolBar->setPath(fileName); d->symbolBar->setVisible(true); d->tabBar->setFileName(fileName); - TextEditor *editor = d->createEditor(fileName); + TextEditor *editor = d->createEditor(fileName, doc); SymbolWidgetGenerator::instance()->symbolWidget()->setEditor(editor); d->editorLayout->addWidget(editor); diff --git a/src/plugins/codeeditor/gui/tabwidget.h b/src/plugins/codeeditor/gui/tabwidget.h index a64f3b783..79a75e4aa 100644 --- a/src/plugins/codeeditor/gui/tabwidget.h +++ b/src/plugins/codeeditor/gui/tabwidget.h @@ -7,6 +7,8 @@ #include +class TextEditor; +class QsciDocument; class TabWidgetPrivate; class TabWidget : public QWidget { @@ -64,9 +66,10 @@ class TabWidget : public QWidget void updateZoomValue(int value); QWidget *currentWidget() const; + TextEditor *findEditor(const QString &fileName); public slots: - void openFile(const QString &fileName); + void openFile(const QString &fileName, QsciDocument *doc = nullptr); void setDebugLine(int line); void removeDebugLine(); void gotoLine(int line); diff --git a/src/plugins/codeeditor/gui/texteditor.cpp b/src/plugins/codeeditor/gui/texteditor.cpp index 524ae1072..bcba52c4d 100644 --- a/src/plugins/codeeditor/gui/texteditor.cpp +++ b/src/plugins/codeeditor/gui/texteditor.cpp @@ -7,9 +7,8 @@ #include "utils/editorutils.h" #include "common/common.h" #include "settings/settingsdefine.h" -#include "services/option/optionutils.h" -#include "Qsci/qscistyledtext.h" +#include "Qsci/qscidocument.h" #include @@ -42,7 +41,7 @@ void TextEditor::init() connect(this, &TextEditor::cursorPositionChanged, this, &TextEditor::onCursorPositionChanged); } -void TextEditor::setFile(const QString &fileName) +void TextEditor::openFile(const QString &fileName) { if (d->fileName == fileName) return; @@ -66,6 +65,21 @@ void TextEditor::setFile(const QString &fileName) endUndoAction(); } +void TextEditor::openFileWithDocument(const QString &fileName, const QsciDocument &doc) +{ + if (d->fileName == fileName) + return; + + d->fileName = fileName; + setDocument(doc); + d->loadLexer(); + d->initLanguageClient(); + d->isAutoCompletionEnabled = true; + + if (isModified()) + emit modificationChanged(true); +} + QString TextEditor::getFile() const { return d->fileName; @@ -94,7 +108,6 @@ void TextEditor::save() file.write(text().toUtf8()); file.close(); setModified(false); - emit fileSaved(d->fileName); editor.fileSaved(d->fileName); } @@ -124,7 +137,7 @@ void TextEditor::reload() int line = 0, index = 0; getCursorPosition(&line, &index); const auto &markers = d->allMarkers(); - + QString text; QFile file(d->fileName); if (file.open(QFile::OpenModeFlag::ReadOnly)) { @@ -136,6 +149,7 @@ void TextEditor::reload() d->setMarkers(markers); setCursorPosition(line, index); + emit textChanged(); } void TextEditor::addBreakpoint(int line, bool enabled) @@ -148,7 +162,7 @@ void TextEditor::addBreakpoint(int line, bool enabled) } else { markerAdd(line, TextEditorPrivate::BreakpointDisabled); } - + editor.breakpointAdded(d->fileName, line + 1, enabled); } @@ -614,7 +628,7 @@ void TextEditor::switchHeaderSource() { if (!d->languageClient) return; - + d->languageClient->switchHeaderSource(d->fileName); } @@ -622,7 +636,7 @@ void TextEditor::followSymbolUnderCursor() { if (!d->languageClient) return; - + d->languageClient->followSymbolUnderCursor(); } @@ -630,7 +644,7 @@ void TextEditor::findUsage() { if (!d->languageClient) return; - + d->languageClient->findUsagesActionTriggered(); } @@ -638,7 +652,7 @@ void TextEditor::renameSymbol() { if (!d->languageClient) return; - + d->languageClient->renameActionTriggered(); } diff --git a/src/plugins/codeeditor/gui/texteditor.h b/src/plugins/codeeditor/gui/texteditor.h index 44cea6278..f53ea9d08 100644 --- a/src/plugins/codeeditor/gui/texteditor.h +++ b/src/plugins/codeeditor/gui/texteditor.h @@ -25,7 +25,8 @@ class TextEditor : public QsciScintilla explicit TextEditor(QWidget *parent = nullptr); ~TextEditor() override; - virtual void setFile(const QString &fileName); + void openFile(const QString &fileName); + void openFileWithDocument(const QString &fileName, const QsciDocument &doc); QString getFile() const; void save(); @@ -116,7 +117,6 @@ public slots: virtual bool event(QEvent *event) override; signals: - void fileSaved(const QString &fileName); void fileClosed(const QString &fileName); void textAdded(int pos, int len, int added, const QString &text, int line); void textRemoved(int pos, int len, int removed, const QString &text, int line); diff --git a/src/plugins/codeeditor/gui/workspacewidget.cpp b/src/plugins/codeeditor/gui/workspacewidget.cpp index 9ab511bee..904a3d883 100644 --- a/src/plugins/codeeditor/gui/workspacewidget.cpp +++ b/src/plugins/codeeditor/gui/workspacewidget.cpp @@ -144,17 +144,24 @@ TabWidget *WorkspaceWidgetPrivate::currentTabWidget() const return tabWidgetList.isEmpty() ? nullptr : tabWidgetList.first(); } -void WorkspaceWidgetPrivate::doSplit(QSplitter *spliter, int index, const QString &fileName, int pos, int scroll) +void WorkspaceWidgetPrivate::doSplit(TabWidget *fromTW, QSplitter *spliter, int index, const QString &fileName) { + auto fromEdit = fromTW->findEditor(fileName); + if (!fromEdit) + return; + TabWidget *tabWidget = new TabWidget(spliter); connectTabWidgetSignals(tabWidget); tabWidgetList.append(tabWidget); spliter->insertWidget(index, tabWidget); - tabWidget->openFile(fileName); + auto doc = fromEdit->document(); + tabWidget->openFile(fileName, &doc); // Set the cursor and scroll position - tabWidget->setEditorCursorPosition(pos); + int cursorPos = fromTW->editorCursorPosition(); + int scroll = fromTW->editorScrollValue(); + tabWidget->setEditorCursorPosition(cursorPos); tabWidget->setEditorScrollValue(scroll); } @@ -316,18 +323,14 @@ void WorkspaceWidgetPrivate::onSplitRequested(Qt::Orientation ori, const QString return; tabWidgetSender->setCloseButtonVisible(true); - int index = spliter->indexOf(tabWidgetSender); - int cursorPos = tabWidgetSender->editorCursorPosition(); - int scroll = tabWidgetSender->editorScrollValue(); - if (spliter->count() == 1) { // Only one widget is added to the splitter, // change its orientation and add a new widget spliter->setOrientation(ori); - doSplit(spliter, index + 1, fileName, cursorPos, scroll); + doSplit(tabWidgetSender, spliter, index + 1, fileName); } else if (spliter->orientation() == ori) { - doSplit(spliter, index + 1, fileName, cursorPos, scroll); + doSplit(tabWidgetSender, spliter, index + 1, fileName); } else { // Use a new splitter to replace QSplitter *newSplitter = new QSplitter(q); @@ -335,7 +338,7 @@ void WorkspaceWidgetPrivate::onSplitRequested(Qt::Orientation ori, const QString spliter->replaceWidget(index, newSplitter); newSplitter->addWidget(tabWidgetSender); - doSplit(newSplitter, 1, fileName, cursorPos, scroll); + doSplit(tabWidgetSender, newSplitter, 1, fileName); } }