Skip to content

Commit

Permalink
fix: [editor] Fixed an issue where documents could not be synchronized
Browse files Browse the repository at this point in the history
as title

Log: fix issue
  • Loading branch information
Kakueeen committed Aug 12, 2024
1 parent a8db5f0 commit 32f6732
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 70 deletions.
3 changes: 1 addition & 2 deletions src/plugins/codeeditor/gui/private/tabwidget_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codeeditor/gui/private/workspacewidget_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 5 additions & 19 deletions src/plugins/codeeditor/gui/tabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 1 addition & 2 deletions src/plugins/codeeditor/gui/tabbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 15 additions & 23 deletions src/plugins/codeeditor/gui/tabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,25 @@ 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);
editor->installEventFilter(q);

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);

Expand Down Expand Up @@ -418,18 +417,6 @@ void TabWidgetPrivate::onCursorRecordChanged(int pos)
prePosRecord.takeFirst();
}

void TabWidgetPrivate::onFileChanged(const QString &fileName)
{
auto editor = qobject_cast<TextEditor *>(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))
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/codeeditor/gui/tabwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <QWidget>

class TextEditor;
class QsciDocument;
class TabWidgetPrivate;
class TabWidget : public QWidget
{
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 24 additions & 10 deletions src/plugins/codeeditor/gui/texteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DDialog>

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -94,7 +108,6 @@ void TextEditor::save()
file.write(text().toUtf8());
file.close();
setModified(false);
emit fileSaved(d->fileName);
editor.fileSaved(d->fileName);
}

Expand Down Expand Up @@ -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)) {
Expand All @@ -136,6 +149,7 @@ void TextEditor::reload()

d->setMarkers(markers);
setCursorPosition(line, index);
emit textChanged();
}

void TextEditor::addBreakpoint(int line, bool enabled)
Expand All @@ -148,7 +162,7 @@ void TextEditor::addBreakpoint(int line, bool enabled)
} else {
markerAdd(line, TextEditorPrivate::BreakpointDisabled);
}

editor.breakpointAdded(d->fileName, line + 1, enabled);
}

Expand Down Expand Up @@ -614,31 +628,31 @@ void TextEditor::switchHeaderSource()
{
if (!d->languageClient)
return;

d->languageClient->switchHeaderSource(d->fileName);
}

void TextEditor::followSymbolUnderCursor()
{
if (!d->languageClient)
return;

d->languageClient->followSymbolUnderCursor();
}

void TextEditor::findUsage()
{
if (!d->languageClient)
return;

d->languageClient->findUsagesActionTriggered();
}

void TextEditor::renameSymbol()
{
if (!d->languageClient)
return;

d->languageClient->renameActionTriggered();
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/codeeditor/gui/texteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 13 additions & 10 deletions src/plugins/codeeditor/gui/workspacewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -316,26 +323,22 @@ 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);
newSplitter->setOrientation(ori);

spliter->replaceWidget(index, newSplitter);
newSplitter->addWidget(tabWidgetSender);
doSplit(newSplitter, 1, fileName, cursorPos, scroll);
doSplit(tabWidgetSender, newSplitter, 1, fileName);
}
}

Expand Down

0 comments on commit 32f6732

Please sign in to comment.