Skip to content

Commit

Permalink
fix: don't create new file from palette
Browse files Browse the repository at this point in the history
  • Loading branch information
Montel committed Oct 29, 2024
1 parent 6e781bc commit 51523a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/core/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,16 @@ const QList<Document *> &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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/core/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 51523a8

Please sign in to comment.