Core: replace QString with Utils::FilePath to get documents

Change-Id: I01777c227398be8bd3bf877c5429b84a75aa361b
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2020-09-02 12:06:51 +02:00
parent e4dec0484c
commit 339db00f85
15 changed files with 33 additions and 34 deletions

View File

@@ -633,15 +633,15 @@ QList<IDocument *> DocumentModel::openedDocuments()
return d->m_editors.keys(); return d->m_editors.keys();
} }
IDocument *DocumentModel::documentForFilePath(const QString &filePath) IDocument *DocumentModel::documentForFilePath(const Utils::FilePath &filePath)
{ {
const Utils::optional<int> index = d->indexOfFilePath(Utils::FilePath::fromString(filePath)); const Utils::optional<int> index = d->indexOfFilePath(filePath);
if (!index) if (!index)
return nullptr; return nullptr;
return d->m_entries.at(*index)->document; return d->m_entries.at(*index)->document;
} }
QList<IEditor *> DocumentModel::editorsForFilePath(const QString &filePath) QList<IEditor *> DocumentModel::editorsForFilePath(const Utils::FilePath &filePath)
{ {
IDocument *document = documentForFilePath(filePath); IDocument *document = documentForFilePath(filePath);
if (document) if (document)

View File

@@ -84,8 +84,8 @@ public:
static Entry *entryForFilePath(const Utils::FilePath &filePath); static Entry *entryForFilePath(const Utils::FilePath &filePath);
static QList<IDocument *> openedDocuments(); static QList<IDocument *> openedDocuments();
static IDocument *documentForFilePath(const QString &filePath); static IDocument *documentForFilePath(const Utils::FilePath &filePath);
static QList<IEditor *> editorsForFilePath(const QString &filePath); static QList<IEditor *> editorsForFilePath(const Utils::FilePath &filePath);
static QList<IEditor *> editorsForDocument(IDocument *document); static QList<IEditor *> editorsForDocument(IDocument *document);
static QList<IEditor *> editorsForDocuments(const QList<IDocument *> &entries); static QList<IEditor *> editorsForDocuments(const QList<IDocument *> &entries);
static QList<IEditor *> editorsForOpenedDocuments(); static QList<IEditor *> editorsForOpenedDocuments();

View File

@@ -104,8 +104,6 @@
#include <QTest> #include <QTest>
#endif #endif
using namespace Utils;
enum { debugEditorManager=0 }; enum { debugEditorManager=0 };
static const char kCurrentDocumentPrefix[] = "CurrentDocument"; static const char kCurrentDocumentPrefix[] = "CurrentDocument";
@@ -654,7 +652,7 @@ IEditor *EditorManagerPrivate::openEditor(EditorView *view, const QString &fileN
if (newEditor) if (newEditor)
*newEditor = false; *newEditor = false;
const QList<IEditor *> editors = DocumentModel::editorsForFilePath(fn); const QList<IEditor *> editors = DocumentModel::editorsForFilePath(FilePath::fromString(fn));
if (!editors.isEmpty()) { if (!editors.isEmpty()) {
IEditor *editor = editors.first(); IEditor *editor = editors.first();
if (flags & EditorManager::SwitchSplitIfAlreadyVisible) { if (flags & EditorManager::SwitchSplitIfAlreadyVisible) {
@@ -812,8 +810,8 @@ IEditor *EditorManagerPrivate::openEditorWith(const QString &fileName, Utils::Id
// close any open editors that have this file open // close any open editors that have this file open
// remember the views to open new editors in there // remember the views to open new editors in there
QList<EditorView *> views; QList<EditorView *> views;
QList<IEditor *> editorsOpenForFile QList<IEditor *> editorsOpenForFile = DocumentModel::editorsForFilePath(
= DocumentModel::editorsForFilePath(fileName); FilePath::fromString(fileName));
foreach (IEditor *openEditor, editorsOpenForFile) { foreach (IEditor *openEditor, editorsOpenForFile) {
EditorView *view = EditorManagerPrivate::viewForEditor(openEditor); EditorView *view = EditorManagerPrivate::viewForEditor(openEditor);
if (view && view->currentEditor() == openEditor) // visible if (view && view->currentEditor() == openEditor) // visible
@@ -2295,13 +2293,12 @@ bool EditorManagerPrivate::saveDocumentAs(IDocument *document)
if (!document) if (!document)
return false; return false;
const QString &absoluteFilePath = const auto &absoluteFilePath = FilePath::fromString(DocumentManager::getSaveAsFileName(document));
DocumentManager::getSaveAsFileName(document);
if (absoluteFilePath.isEmpty()) if (absoluteFilePath.isEmpty())
return false; return false;
if (absoluteFilePath != document->filePath().toString()) { if (absoluteFilePath != document->filePath()) {
// close existing editors for the new file name // close existing editors for the new file name
IDocument *otherDocument = DocumentModel::documentForFilePath(absoluteFilePath); IDocument *otherDocument = DocumentModel::documentForFilePath(absoluteFilePath);
if (otherDocument) if (otherDocument)
@@ -2309,7 +2306,7 @@ bool EditorManagerPrivate::saveDocumentAs(IDocument *document)
} }
emit m_instance->aboutToSave(document); emit m_instance->aboutToSave(document);
const bool success = DocumentManager::saveDocument(document, absoluteFilePath); const bool success = DocumentManager::saveDocument(document, absoluteFilePath.toString());
document->checkPermissions(); document->checkPermissions();
// TODO: There is an issue to be treated here. The new file might be of a different mime // TODO: There is an issue to be treated here. The new file might be of a different mime

View File

@@ -388,7 +388,7 @@ static void onReplaceUsagesClicked(const QString &text,
static QTextDocument *getOpenDocument(const QString &path) static QTextDocument *getOpenDocument(const QString &path)
{ {
const IDocument *document = DocumentModel::documentForFilePath(path); const IDocument *document = DocumentModel::documentForFilePath(FilePath::fromString(path));
if (document) if (document)
return qobject_cast<const TextDocument *>(document)->document(); return qobject_cast<const TextDocument *>(document)->document();
@@ -1104,7 +1104,7 @@ void CppEditorWidget::onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefL
abortDeclDefLink(); abortDeclDefLink();
d->m_declDefLink = link; d->m_declDefLink = link;
IDocument *targetDocument = DocumentModel::documentForFilePath( IDocument *targetDocument = DocumentModel::documentForFilePath(
d->m_declDefLink->targetFile->fileName()); FilePath::fromString(d->m_declDefLink->targetFile->fileName()));
if (textDocument() != targetDocument) { if (textDocument() != targetDocument) {
if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument)) if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument))
connect(textDocument, connect(textDocument,
@@ -1137,7 +1137,7 @@ void CppEditorWidget::abortDeclDefLink()
return; return;
IDocument *targetDocument = DocumentModel::documentForFilePath( IDocument *targetDocument = DocumentModel::documentForFilePath(
d->m_declDefLink->targetFile->fileName()); FilePath::fromString(d->m_declDefLink->targetFile->fileName()));
if (textDocument() != targetDocument) { if (textDocument() != targetDocument) {
if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument)) if (auto textDocument = qobject_cast<BaseTextDocument *>(targetDocument))
disconnect(textDocument, disconnect(textDocument,

View File

@@ -241,7 +241,8 @@ void clearExceptionSelection()
QStringList highlightExceptionCode(int lineNumber, const QString &filePath, const QString &errorMessage) QStringList highlightExceptionCode(int lineNumber, const QString &filePath, const QString &errorMessage)
{ {
QStringList messages; QStringList messages;
const QList<IEditor *> editors = DocumentModel::editorsForFilePath(filePath); const QList<IEditor *> editors = DocumentModel::editorsForFilePath(
Utils::FilePath::fromString(filePath));
const TextEditor::FontSettings &fontSettings = TextEditor::TextEditorSettings::instance()->fontSettings(); const TextEditor::FontSettings &fontSettings = TextEditor::TextEditorSettings::instance()->fontSettings();
QTextCharFormat errorFormat = fontSettings.toTextCharFormat(TextEditor::C_ERROR); QTextCharFormat errorFormat = fontSettings.toTextCharFormat(TextEditor::C_ERROR);

View File

@@ -199,7 +199,7 @@ QList<ReloadInput> DiffCurrentFileController::reloadInputList() const
QList<ReloadInput> result; QList<ReloadInput> result;
auto textDocument = qobject_cast<TextEditor::TextDocument *>( auto textDocument = qobject_cast<TextEditor::TextDocument *>(
DocumentModel::documentForFilePath(m_fileName)); DocumentModel::documentForFilePath(Utils::FilePath::fromString(m_fileName)));
if (textDocument && textDocument->isModified()) { if (textDocument && textDocument->isModified()) {
QString errorString; QString errorString;
@@ -313,7 +313,7 @@ QList<ReloadInput> DiffModifiedFilesController::reloadInputList() const
for (const QString &fileName : m_fileNames) { for (const QString &fileName : m_fileNames) {
auto textDocument = qobject_cast<TextEditor::TextDocument *>( auto textDocument = qobject_cast<TextEditor::TextDocument *>(
DocumentModel::documentForFilePath(fileName)); DocumentModel::documentForFilePath(Utils::FilePath::fromString(fileName)));
if (textDocument && textDocument->isModified()) { if (textDocument && textDocument->isModified()) {
QString errorString; QString errorString;

View File

@@ -178,7 +178,7 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde
m_document->reload(); m_document->reload();
} else { // PatchEditor } else { // PatchEditor
auto textDocument = qobject_cast<TextEditor::TextDocument *>( auto textDocument = qobject_cast<TextEditor::TextDocument *>(
DocumentModel::documentForFilePath(absFileName)); DocumentModel::documentForFilePath(Utils::FilePath::fromString(absFileName)));
if (!textDocument) if (!textDocument)
return; return;

View File

@@ -1588,7 +1588,8 @@ void GitPluginPrivate::updateSubmodules()
// If the file is modified in an editor, make sure it is saved. // If the file is modified in an editor, make sure it is saved.
static bool ensureFileSaved(const QString &fileName) static bool ensureFileSaved(const QString &fileName)
{ {
return DocumentManager::saveModifiedDocument(DocumentModel::documentForFilePath(fileName)); return DocumentManager::saveModifiedDocument(
DocumentModel::documentForFilePath(FilePath::fromString(fileName)));
} }
void GitPluginPrivate::applyCurrentFilePatch() void GitPluginPrivate::applyCurrentFilePatch()

View File

@@ -722,8 +722,7 @@ bool QmakePriFile::addDependencies(const QStringList &dependencies)
bool QmakePriFile::saveModifiedEditors() bool QmakePriFile::saveModifiedEditors()
{ {
Core::IDocument *document Core::IDocument *document = Core::DocumentModel::documentForFilePath(filePath());
= Core::DocumentModel::documentForFilePath(filePath().toString());
if (!document || !document->isModified()) if (!document || !document->isModified())
return true; return true;
@@ -976,7 +975,7 @@ void QmakePriFile::save(const QStringList &lines)
// We manually tell each editor to reload it's file. // We manually tell each editor to reload it's file.
// (The .pro files are notified by the file system watcher.) // (The .pro files are notified by the file system watcher.)
QStringList errorStrings; QStringList errorStrings;
Core::IDocument *document = Core::DocumentModel::documentForFilePath(filePath().toString()); Core::IDocument *document = Core::DocumentModel::documentForFilePath(filePath());
if (document) { if (document) {
QString errorString; QString errorString;
if (!document->reload(&errorString, Core::IDocument::FlagReload, Core::IDocument::TypeContents)) if (!document->reload(&errorString, Core::IDocument::FlagReload, Core::IDocument::TypeContents))

View File

@@ -1043,7 +1043,7 @@ void FindReferences::onReplaceButtonClicked(const QString &text, const QList<Sea
QStringList changedOnDisk; QStringList changedOnDisk;
QStringList changedUnsavedEditors; QStringList changedUnsavedEditors;
foreach (const QString &fileName, fileNames) { foreach (const QString &fileName, fileNames) {
if (DocumentModel::documentForFilePath(fileName)) if (DocumentModel::documentForFilePath(Utils::FilePath::fromString(fileName)))
changedOnDisk += fileName; changedOnDisk += fileName;
else else
changedUnsavedEditors += fileName; changedUnsavedEditors += fileName;

View File

@@ -474,8 +474,8 @@ bool QmlBuildSystem::renameFile(Node * context, const QString &filePath, const Q
setMainFile(newFilePath); setMainFile(newFilePath);
// make sure to change it also in the qmlproject file // make sure to change it also in the qmlproject file
const QString qmlProjectFilePath = project()->projectFilePath().toString(); const Utils::FilePath qmlProjectFilePath = project()->projectFilePath();
Core::FileChangeBlocker fileChangeBlocker(qmlProjectFilePath); Core::FileChangeBlocker fileChangeBlocker(qmlProjectFilePath.toString());
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath); const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath);
TextEditor::TextDocument *document = nullptr; TextEditor::TextDocument *document = nullptr;
if (!editors.isEmpty()) { if (!editors.isEmpty()) {
@@ -489,7 +489,7 @@ bool QmlBuildSystem::renameFile(Node * context, const QString &filePath, const Q
QString error; QString error;
Utils::TextFileFormat textFileFormat; Utils::TextFileFormat textFileFormat;
const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8 const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8
if (Utils::TextFileFormat::readFile(qmlProjectFilePath, codec, &fileContent, &textFileFormat, &error) if (Utils::TextFileFormat::readFile(qmlProjectFilePath.toString(), codec, &fileContent, &textFileFormat, &error)
!= Utils::TextFileFormat::ReadSuccess) { != Utils::TextFileFormat::ReadSuccess) {
qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error; qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error;
} }
@@ -502,7 +502,7 @@ bool QmlBuildSystem::renameFile(Node * context, const QString &filePath, const Q
fileContent.replace(match.capturedStart(1), match.capturedLength(1), QFileInfo(newFilePath).fileName()); fileContent.replace(match.capturedStart(1), match.capturedLength(1), QFileInfo(newFilePath).fileName());
if (!textFileFormat.writeFile(qmlProjectFilePath, fileContent, &error)) if (!textFileFormat.writeFile(qmlProjectFilePath.toString(), fileContent, &error))
qWarning() << "Failed to write file" << qmlProjectFilePath << ":" << error; qWarning() << "Failed to write file" << qmlProjectFilePath << ":" << error;
refresh(Everything); refresh(Everything);

View File

@@ -164,7 +164,7 @@ RefactoringFile::RefactoringFile(const QString &fileName, const QSharedPointer<R
: m_fileName(fileName) : m_fileName(fileName)
, m_data(data) , m_data(data)
{ {
QList<IEditor *> editors = DocumentModel::editorsForFilePath(fileName); QList<IEditor *> editors = DocumentModel::editorsForFilePath(Utils::FilePath::fromString(fileName));
if (!editors.isEmpty()) { if (!editors.isEmpty()) {
auto editorWidget = TextEditorWidget::fromEditor(editors.first()); auto editorWidget = TextEditorWidget::fromEditor(editors.first());
if (editorWidget && !editorWidget->isReadOnly()) if (editorWidget && !editorWidget->isReadOnly())

View File

@@ -351,7 +351,7 @@ TextDocument *TextDocument::currentTextDocument()
TextDocument *TextDocument::textDocumentForFilePath(const Utils::FilePath &filePath) TextDocument *TextDocument::textDocumentForFilePath(const Utils::FilePath &filePath)
{ {
return qobject_cast<TextDocument *>(DocumentModel::documentForFilePath(filePath.toString())); return qobject_cast<TextDocument *>(DocumentModel::documentForFilePath(filePath));
} }
QString TextDocument::plainText() const QString TextDocument::plainText() const

View File

@@ -1260,7 +1260,8 @@ const VcsBaseEditorParameters *VcsBaseEditor::findType(const VcsBaseEditorParame
// Find the codec used for a file querying the editor. // Find the codec used for a file querying the editor.
static QTextCodec *findFileCodec(const QString &source) static QTextCodec *findFileCodec(const QString &source)
{ {
Core::IDocument *document = Core::DocumentModel::documentForFilePath(source); Core::IDocument *document = Core::DocumentModel::documentForFilePath(
Utils::FilePath::fromString(source));
if (auto textDocument = qobject_cast<Core::BaseTextDocument *>(document)) if (auto textDocument = qobject_cast<Core::BaseTextDocument *>(document))
return const_cast<QTextCodec *>(textDocument->codec()); return const_cast<QTextCodec *>(textDocument->codec());
return nullptr; return nullptr;

View File

@@ -246,7 +246,7 @@ QString StateListener::windowTitleVcsTopic(const QString &filePath)
static inline QString displayNameOfEditor(const QString &fileName) static inline QString displayNameOfEditor(const QString &fileName)
{ {
IDocument *document = DocumentModel::documentForFilePath(fileName); IDocument *document = DocumentModel::documentForFilePath(FilePath::fromString(fileName));
if (document) if (document)
return document->displayName(); return document->displayName();
return QString(); return QString();