EditorManager: Small API clean-up

- Move private enum to private header
- Add missing parameter to openEditorAtSearchResult
- Remove convenience singular close* methods, we have {} nowadays

Change-Id: Ic5b6d831a9f506ffee09b89b3358874433d62998
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2020-09-29 10:24:48 +02:00
parent 52d1b31a67
commit f811b87c66
18 changed files with 42 additions and 74 deletions

View File

@@ -747,7 +747,7 @@ void BazaarPluginPrivate::commitFromEditor()
// Close the submit editor // Close the submit editor
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
EditorManager::closeDocument(submitEditor()->document()); EditorManager::closeDocuments({submitEditor()->document()});
} }
void BazaarPluginPrivate::uncommit() void BazaarPluginPrivate::uncommit()

View File

@@ -198,7 +198,7 @@ OpenEditorAtCursorPosition::OpenEditorAtCursorPosition(const TestDocument &testD
OpenEditorAtCursorPosition::~OpenEditorAtCursorPosition() OpenEditorAtCursorPosition::~OpenEditorAtCursorPosition()
{ {
if (m_editor) if (m_editor)
Core::EditorManager::closeEditor(m_editor, /* askAboutModifiedEditors= */ false); Core::EditorManager::closeEditors({m_editor}, /* askAboutModifiedEditors= */ false);
} }

View File

@@ -1643,7 +1643,7 @@ void ClearCasePluginPrivate::commitFromEditor()
{ {
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
EditorManager::closeDocument(submitEditor()->document()); EditorManager::closeDocuments({submitEditor()->document()});
} }
QString ClearCasePluginPrivate::runCleartoolSync(const QString &workingDir, QString ClearCasePluginPrivate::runCleartoolSync(const QString &workingDir,
@@ -2712,7 +2712,7 @@ public:
~TestCase() ~TestCase()
{ {
EditorManager::closeDocument(m_editor->document(), false); EditorManager::closeDocuments({m_editor->document()}, false);
QCoreApplication::processEvents(); // process any pending events QCoreApplication::processEvents(); // process any pending events
QFile file(m_fileName); QFile file(m_fileName);

View File

@@ -1447,9 +1447,9 @@ void EditorManagerPrivate::closeEditorOrDocument(IEditor *editor)
[&editor](IEditor *other) { [&editor](IEditor *other) {
return editor != other && other->document() == editor->document(); return editor != other && other->document() == editor->document();
})) { })) {
EditorManager::closeEditor(editor); EditorManager::closeEditors({editor});
} else { } else {
EditorManager::closeDocument(editor->document()); EditorManager::closeDocuments({editor->document()});
} }
} }
@@ -2239,7 +2239,7 @@ void EditorManagerPrivate::closeEditorFromContextMenu()
} else { } else {
IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr; IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : nullptr;
if (document) if (document)
EditorManager::closeDocument(document); EditorManager::closeDocuments({document});
} }
} }
@@ -2784,29 +2784,6 @@ void EditorManager::revertToSaved()
EditorManagerPrivate::revertToSaved(currentDocument()); EditorManagerPrivate::revertToSaved(currentDocument());
} }
/*!
Closes \a editor. If \a askAboutModifiedEditors is \c true, prompts
users to save their changes before closing the editor.
*/
void EditorManager::closeEditor(IEditor *editor, bool askAboutModifiedEditors)
{
if (editor)
closeEditors({editor}, askAboutModifiedEditors);
}
/*!
Closes the document specified by \a entry.
*/
void EditorManager::closeDocument(DocumentModel::Entry *entry)
{
if (!entry)
return;
if (entry->isSuspended)
DocumentModelPrivate::removeEntry(entry);
else
closeDocuments({entry->document});
}
/*! /*!
Closes the documents specified by \a entries. Closes the documents specified by \a entries.
@@ -2914,15 +2891,22 @@ IEditor *EditorManager::openEditorAt(const QString &fileName, int line, int colu
Opens the document at the position of the search hit \a item in the editor Opens the document at the position of the search hit \a item in the editor
using the settings specified by \a flags. using the settings specified by \a flags.
*/ */
void EditorManager::openEditorAtSearchResult(const SearchResultItem &item, OpenEditorFlags flags) void EditorManager::openEditorAtSearchResult(const SearchResultItem &item,
Id editorId,
OpenEditorFlags flags,
bool *newEditor)
{ {
if (item.path.empty()) { if (item.path.empty()) {
openEditor(QDir::fromNativeSeparators(item.text), Id(), flags); openEditor(QDir::fromNativeSeparators(item.text), editorId, flags, newEditor);
return; return;
} }
openEditorAt(QDir::fromNativeSeparators(item.path.first()), item.mainRange.begin.line, openEditorAt(QDir::fromNativeSeparators(item.path.first()),
item.mainRange.begin.column, Id(), flags); item.mainRange.begin.line,
item.mainRange.begin.column,
editorId,
flags,
newEditor);
} }
/*! /*!
@@ -3190,17 +3174,6 @@ QList<IEditor*> EditorManager::visibleEditors()
return editors; return editors;
} }
/*!
Closes \a document. If \a askAboutModifiedEditors is \c true, prompts
users to save their changes before closing the document.
Returns whether the document was closed.
*/
bool EditorManager::closeDocument(IDocument *document, bool askAboutModifiedEditors)
{
return closeDocuments({document}, askAboutModifiedEditors);
}
/*! /*!
Closes \a documents. If \a askAboutModifiedEditors is \c true, prompts Closes \a documents. If \a askAboutModifiedEditors is \c true, prompts
users to save their changes before closing the documents. users to save their changes before closing the documents.

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.
@@ -47,13 +47,6 @@ namespace Core {
class IDocument; class IDocument;
class SearchResultItem; class SearchResultItem;
enum MakeWritableResult {
OpenedWithVersionControl,
MadeWritable,
SavedAs,
Failed
};
namespace Internal { namespace Internal {
class EditorManagerPrivate; class EditorManagerPrivate;
class MainWindow; class MainWindow;
@@ -98,13 +91,17 @@ public:
int lineNumber; // extracted line number, -1 if none int lineNumber; // extracted line number, -1 if none
int columnNumber; // extracted column number, -1 if none int columnNumber; // extracted column number, -1 if none
}; };
static FilePathInfo splitLineAndColumnNumber(const QString &filePath); static FilePathInfo splitLineAndColumnNumber(const QString &filePath);
static IEditor *openEditor(const QString &fileName, Utils::Id editorId = {}, static IEditor *openEditor(const QString &fileName, Utils::Id editorId = {},
OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr); OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr);
static IEditor *openEditorAt(const QString &fileName, int line, int column = 0, static IEditor *openEditorAt(const QString &fileName, int line, int column = 0,
Utils::Id editorId = {}, OpenEditorFlags flags = NoFlags, Utils::Id editorId = {}, OpenEditorFlags flags = NoFlags,
bool *newEditor = nullptr); bool *newEditor = nullptr);
static void openEditorAtSearchResult(const SearchResultItem &item, OpenEditorFlags flags = NoFlags); static void openEditorAtSearchResult(const SearchResultItem &item,
Utils::Id editorId = {},
OpenEditorFlags flags = NoFlags,
bool *newEditor = nullptr);
static IEditor *openEditorWithContents(Utils::Id editorId, QString *titlePattern = nullptr, static IEditor *openEditorWithContents(Utils::Id editorId, QString *titlePattern = nullptr,
const QByteArray &contents = QByteArray(), const QByteArray &contents = QByteArray(),
const QString &uniqueId = QString(), const QString &uniqueId = QString(),
@@ -125,9 +122,7 @@ public:
static void activateEditorForEntry(DocumentModel::Entry *entry, OpenEditorFlags flags = NoFlags); static void activateEditorForEntry(DocumentModel::Entry *entry, OpenEditorFlags flags = NoFlags);
static IEditor *activateEditorForDocument(IDocument *document, OpenEditorFlags flags = NoFlags); static IEditor *activateEditorForDocument(IDocument *document, OpenEditorFlags flags = NoFlags);
static bool closeDocument(IDocument *document, bool askAboutModifiedEditors = true);
static bool closeDocuments(const QList<IDocument *> &documents, bool askAboutModifiedEditors = true); static bool closeDocuments(const QList<IDocument *> &documents, bool askAboutModifiedEditors = true);
static void closeDocument(DocumentModel::Entry *entry);
static bool closeDocuments(const QList<DocumentModel::Entry *> &entries); static bool closeDocuments(const QList<DocumentModel::Entry *> &entries);
static void closeOtherDocuments(IDocument *document); static void closeOtherDocuments(IDocument *document);
static bool closeAllDocuments(); static bool closeAllDocuments();
@@ -139,7 +134,6 @@ public:
static bool saveDocument(IDocument *document); static bool saveDocument(IDocument *document);
static bool closeEditors(const QList<IEditor *> &editorsToClose, bool askAboutModifiedEditors = true); static bool closeEditors(const QList<IEditor *> &editorsToClose, bool askAboutModifiedEditors = true);
static void closeEditor(IEditor *editor, bool askAboutModifiedEditors = true);
static QByteArray saveState(); static QByteArray saveState();
static bool restoreState(const QByteArray &state); static bool restoreState(const QByteArray &state);

View File

@@ -57,6 +57,8 @@ class MainWindow;
class OpenEditorsViewFactory; class OpenEditorsViewFactory;
class OpenEditorsWindow; class OpenEditorsWindow;
enum MakeWritableResult { OpenedWithVersionControl, MadeWritable, SavedAs, Failed };
class EditorManagerPrivate : public QObject class EditorManagerPrivate : public QObject
{ {
Q_OBJECT Q_OBJECT

View File

@@ -106,8 +106,7 @@ void OpenEditorsWidget::activateEditor(const QModelIndex &index)
void OpenEditorsWidget::closeDocument(const QModelIndex &index) void OpenEditorsWidget::closeDocument(const QModelIndex &index)
{ {
EditorManager::closeDocument( EditorManager::closeDocuments({DocumentModel::entryAtRow(m_model->mapToSource(index).row())});
DocumentModel::entryAtRow(m_model->mapToSource(index).row()));
// work around selection changes // work around selection changes
updateCurrentItem(EditorManager::currentEditor()); updateCurrentItem(EditorManager::currentEditor());
} }

View File

@@ -666,7 +666,7 @@ void CppToolsPlugin::test_modelmanager_gc_if_last_cppeditor_closed()
helper.waitForRefreshedSourceFiles(); helper.waitForRefreshedSourceFiles();
// Close file/editor // Close file/editor
Core::EditorManager::closeDocument(editor->document(), /*askAboutModifiedEditors=*/ false); Core::EditorManager::closeDocuments({editor->document()}, /*askAboutModifiedEditors=*/false);
helper.waitForFinishedGc(); helper.waitForFinishedGc();
// Check: File is removed from the snapshpt // Check: File is removed from the snapshpt
@@ -705,7 +705,7 @@ void CppToolsPlugin::test_modelmanager_dont_gc_opened_files()
QVERIFY(mm->snapshot().contains(file)); QVERIFY(mm->snapshot().contains(file));
// Close editor // Close editor
Core::EditorManager::closeDocument(editor->document()); Core::EditorManager::closeDocuments({editor->document()});
helper.waitForFinishedGc(); helper.waitForFinishedGc();
QVERIFY(mm->snapshot().isEmpty()); QVERIFY(mm->snapshot().isEmpty());
} }

View File

@@ -1424,7 +1424,7 @@ void CvsPluginPrivate::commitFromEditor()
{ {
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
EditorManager::closeDocument(submitEditor()->document()); EditorManager::closeDocuments({submitEditor()->document()});
} }
// Run CVS. At this point, file arguments must be relative to // Run CVS. At this point, file arguments must be relative to

View File

@@ -284,7 +284,7 @@ MemoryAgent::MemoryAgent(const MemoryViewSetupData &data, DebuggerEngine *engine
MemoryAgent::~MemoryAgent() MemoryAgent::~MemoryAgent()
{ {
if (m_service && m_service->editor()) if (m_service && m_service->editor())
EditorManager::closeDocument(m_service->editor()->document()); EditorManager::closeDocuments({m_service->editor()->document()});
if (m_service && m_service->widget()) // m_service might be set to null by closeDocument if (m_service && m_service->widget()) // m_service might be set to null by closeDocument
m_service->widget()->close(); m_service->widget()->close();
} }

View File

@@ -75,7 +75,7 @@ SourceAgentPrivate::SourceAgentPrivate()
SourceAgentPrivate::~SourceAgentPrivate() SourceAgentPrivate::~SourceAgentPrivate()
{ {
if (editor) if (editor)
EditorManager::closeDocument(editor->document()); EditorManager::closeDocuments({editor->document()});
editor = nullptr; editor = nullptr;
delete locationMark; delete locationMark;
} }

View File

@@ -2035,7 +2035,7 @@ void FakeVimPluginPrivate::handleDelayedQuit(bool forced, IEditor *editor)
if (EditorManager::hasSplitter()) if (EditorManager::hasSplitter())
triggerAction(Core::Constants::REMOVE_CURRENT_SPLIT); triggerAction(Core::Constants::REMOVE_CURRENT_SPLIT);
else else
EditorManager::closeEditor(editor, !forced); EditorManager::closeEditors({editor}, !forced);
} }
void FakeVimPluginPrivate::handleDelayedQuitAll(bool forced) void FakeVimPluginPrivate::handleDelayedQuitAll(bool forced)

View File

@@ -1405,7 +1405,7 @@ void GitPluginPrivate::commitFromEditor()
// Close the submit editor // Close the submit editor
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
EditorManager::closeDocument(submitEditor()->document()); EditorManager::closeDocuments({submitEditor()->document()});
} }
bool GitPluginPrivate::submitEditorAboutToClose() bool GitPluginPrivate::submitEditorAboutToClose()

View File

@@ -676,7 +676,7 @@ void MercurialPluginPrivate::commitFromEditor()
// Close the submit editor // Close the submit editor
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
Core::EditorManager::closeDocument(submitEditor()->document()); Core::EditorManager::closeDocuments({submitEditor()->document()});
} }
bool MercurialPluginPrivate::submitEditorAboutToClose() bool MercurialPluginPrivate::submitEditorAboutToClose()

View File

@@ -1555,7 +1555,7 @@ void PerforcePluginPrivate::commitFromEditor()
{ {
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
EditorManager::closeDocument(submitEditor()->document()); EditorManager::closeDocuments({submitEditor()->document()});
} }
void PerforcePluginPrivate::cleanCommitMessageFile() void PerforcePluginPrivate::cleanCommitMessageFile()

View File

@@ -1010,7 +1010,7 @@ void SubversionPluginPrivate::commitFromEditor()
{ {
m_submitActionTriggered = true; m_submitActionTriggered = true;
QTC_ASSERT(submitEditor(), return); QTC_ASSERT(submitEditor(), return);
EditorManager::closeDocument(submitEditor()->document()); EditorManager::closeDocuments({submitEditor()->document()});
} }
SubversionResponse SubversionPluginPrivate::runSvn(const QString &workingDir, SubversionResponse SubversionPluginPrivate::runSvn(const QString &workingDir,

View File

@@ -450,7 +450,7 @@ void BaseFileFind::openEditor(SearchResult *result, const SearchResultItem &item
IEditor *openedEditor = IEditor *openedEditor =
d->m_searchEngines[parameters.searchEngineIndex]->openEditor(item, parameters); d->m_searchEngines[parameters.searchEngineIndex]->openEditor(item, parameters);
if (!openedEditor) if (!openedEditor)
EditorManager::openEditorAtSearchResult(item, EditorManager::DoNotSwitchToDesignMode); EditorManager::openEditorAtSearchResult(item, Id(), EditorManager::DoNotSwitchToDesignMode);
if (d->m_currentFindSupport) if (d->m_currentFindSupport)
d->m_currentFindSupport->clearHighlights(); d->m_currentFindSupport->clearHighlights();
d->m_currentFindSupport = nullptr; d->m_currentFindSupport = nullptr;

View File

@@ -147,7 +147,7 @@ void Internal::TextEditorPlugin::testBlockSelectionTransformation()
} }
QCOMPARE(textEditor->textDocument()->plainText(), transformedText); QCOMPARE(textEditor->textDocument()->plainText(), transformedText);
} }
Core::EditorManager::closeDocument(editor->document(), false); Core::EditorManager::closeDocuments({editor->document()}, false);
} }
static const char text[] = static const char text[] =
@@ -362,7 +362,7 @@ void Internal::TextEditorPlugin::testBlockSelectionInsert()
QCOMPARE(textEditor->textDocument()->plainText(), transformedText); QCOMPARE(textEditor->textDocument()->plainText(), transformedText);
} }
Core::EditorManager::closeDocument(editor->document(), false); Core::EditorManager::closeDocuments({editor->document()}, false);
} }
@@ -440,7 +440,7 @@ void Internal::TextEditorPlugin::testBlockSelectionRemove()
QCOMPARE(textEditor->textDocument()->plainText(), transformedText); QCOMPARE(textEditor->textDocument()->plainText(), transformedText);
} }
Core::EditorManager::closeDocument(editor->document(), false); Core::EditorManager::closeDocuments({editor->document()}, false);
} }
void Internal::TextEditorPlugin::testBlockSelectionCopy_data() void Internal::TextEditorPlugin::testBlockSelectionCopy_data()
@@ -495,7 +495,7 @@ void Internal::TextEditorPlugin::testBlockSelectionCopy()
QCOMPARE(QGuiApplication::clipboard()->text(), copiedText); QCOMPARE(QGuiApplication::clipboard()->text(), copiedText);
} }
Core::EditorManager::closeDocument(editor->document(), false); Core::EditorManager::closeDocuments({editor->document()}, false);
} }
QString tabPolicyToString(TabSettings::TabPolicy policy) QString tabPolicyToString(TabSettings::TabPolicy policy)