From 51523a85e7057ffa0095cb6c68cecfc2fb112de0 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 29 Oct 2024 11:41:16 +0100 Subject: [PATCH] fix: don't create new file from palette --- src/core/project.cpp | 18 +++++++++++++----- src/core/project.h | 4 ++-- src/gui/palette.cpp | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/core/project.cpp b/src/core/project.cpp index 0c4846e9..4553aa65 100644 --- a/src/core/project.cpp +++ b/src/core/project.cpp @@ -261,13 +261,16 @@ const QList &Project::documents() const return m_documents; } -Document *Project::getDocument(QString fileName, bool moveToBack) +Document *Project::getDocument(QString fileName, bool moveToBack, bool onlyExistingFile) { QFileInfo fi(fileName); if (!fi.exists() && fi.isRelative()) fileName = m_root + '/' + fileName; else fileName = fi.absoluteFilePath(); + if (onlyExistingFile && !QFileInfo::exists(fileName)) { + return {}; + } auto findIt = std::ranges::find_if(m_documents, [fileName](auto document) { return document->fileName() == fileName; @@ -315,20 +318,25 @@ Document *Project::get(const QString &fileName) } /*! - * \qmlmethod Document Project::open(string fileName) + * \qmlmethod Document Project::open(string fileName, bool onlyExistingFile) * Opens or creates a document for the given `fileName` and make it current. If the document is already opened, returns * the same instance, a document can't be open twice. If the fileName is relative, use the root path as the base. + * If onlyExistingFile is true, don't create file * - * If the document does not exist, creates a new document (but don't save it yet). + * If the document does not exist (and onlyExistingFile is false (default)), creates a new document (but don't save it + * yet). */ -Document *Project::open(const QString &fileName) +Document *Project::open(const QString &fileName, bool onlyExistingFile) { if (m_current && m_current->fileName() == fileName) return m_current; LOG(LOG_ARG("path", fileName)); - m_current = getDocument(fileName, true); + m_current = getDocument(fileName, true, onlyExistingFile); + if (!m_current) { + return {}; + } emit currentDocumentChanged(m_current); LOG_RETURN("document", m_current); diff --git a/src/core/project.h b/src/core/project.h index dbecde72..292c4697 100644 --- a/src/core/project.h +++ b/src/core/project.h @@ -58,7 +58,7 @@ class Project : public QObject public slots: Core::Document *get(const QString &fileName); - Core::Document *open(const QString &fileName); + Core::Document *open(const QString &fileName, bool onlyExistingFile = false); void closeAll(); void saveAllDocuments(); Core::Document *openPrevious(int index = 1); @@ -72,7 +72,7 @@ public slots: friend class KnutCore; explicit Project(QObject *parent = nullptr); - Core::Document *getDocument(QString fileName, bool moveToBack = false); + Core::Document *getDocument(QString fileName, bool moveToBack = false, bool onlyExistingFile = false); Lsp::Client *getClient(Document::Type type); private: diff --git a/src/gui/palette.cpp b/src/gui/palette.cpp index c8d0d4e2..5c6d0151 100644 --- a/src/gui/palette.cpp +++ b/src/gui/palette.cpp @@ -481,7 +481,7 @@ void Palette::addFileSelector() model->resetFileInfo(); }; auto selectFile = [](const QVariant &path) { - Core::Project::instance()->open(path.toString()); + Core::Project::instance()->open(path.toString(), true); }; m_selectors.emplace_back("", std::move(fileModel), selectFile, resetFiles); }