Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions liteidex/src/liteapp/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <QFileInfo>
#include <QDateTime>
#include <QFileSystemWatcher>
#include <QCryptographicHash>
#include <QMessageBox>
#include <QTimer>
#include <QDesktopServices>
Expand Down Expand Up @@ -547,12 +548,27 @@ void FileManager::applyOption(QString id)
m_fileWatcherAutoReload = m_liteApp->settings()->value(LITEAPP_FILEWATCHERAUTORELOAD,false).toBool();
}

static QByteArray fileContentHash(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
return QByteArray();
}
QCryptographicHash hash(QCryptographicHash::Sha256);
if (!hash.addData(&file)) {
return QByteArray();
}
return hash.result();
}

void FileManager::updateFileState(const QString &fileName)
{
if (fileName.isEmpty()) {
return;
}
m_fileStateMap.insert(fileName,QFileInfo(fileName).lastModified());
m_fileSizeMap.insert(fileName,QFileInfo(fileName).size());
m_fileHashMap.insert(fileName,fileContentHash(fileName));
if (!m_fileWatcher->files().contains(fileName)) {
m_fileWatcher->addPath(fileName);;
}
Expand All @@ -563,6 +579,7 @@ void FileManager::editorCreated(LiteApi::IEditor *editor)
if (!editor) {
return;
}
connect(editor,SIGNAL(reloaded()),this,SLOT(editorReloaded()));
QString fileName = editor->filePath();
if (!fileName.isEmpty()) {
updateFileState(fileName);
Expand All @@ -577,6 +594,8 @@ void FileManager::editorAboutToClose(LiteApi::IEditor *editor)
QString fileName = editor->filePath();
if (!fileName.isEmpty()) {
m_fileStateMap.remove(fileName);
m_fileSizeMap.remove(fileName);
m_fileHashMap.remove(fileName);
m_changedFiles.remove(fileName);
m_fileWatcher->removePath(fileName);
}
Expand All @@ -590,6 +609,11 @@ void FileManager::editorSaved(LiteApi::IEditor *editor)
updateFileState(editor->filePath());
}

void FileManager::editorReloaded()
{
editorSaved(qobject_cast<LiteApi::IEditor*>(sender()));
}

void FileManager::fileChanged(QString fileName)
{
const bool wasempty = m_changedFiles.isEmpty();
Expand Down Expand Up @@ -712,6 +736,14 @@ void FileManager::checkForReload()
if (!m_fileWatcher->files().contains(fileName)) {
m_fileWatcher->addPath(fileName);;
}
// Compare with the last loaded/saved file, not the unsaved editor buffer.
const QFileInfo fileInfo(fileName);
const bool sameSize = fileInfo.size() == m_fileSizeMap.value(fileName, -1);
const QByteArray hash = sameSize ? fileContentHash(fileName) : QByteArray();
if (sameSize && !hash.isEmpty() && hash == m_fileHashMap.value(fileName)) {
m_fileStateMap.insert(fileName,lastModified);
continue;
}
if (lastModified > modified) {
int ret = QMessageBox::Yes;
if (lastReloadRet != QMessageBox::YesToAll) {
Expand All @@ -734,9 +766,10 @@ void FileManager::checkForReload()
QDateTime lastModified = QFileInfo(fileName).lastModified();
QDateTime modified = m_fileStateMap.value(fileName);
if (lastModified != modified) {
editor->reload();
m_fileStateMap.insert(fileName,lastModified);
m_liteApp->appendLog("EditorManager",fileName+" reload",false);
if (editor->reload()) {
updateFileState(fileName);
m_liteApp->appendLog("EditorManager",fileName+" reload",false);
}

}
}
Expand Down
3 changes: 3 additions & 0 deletions liteidex/src/liteapp/filemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public slots:
void fileChanged(QString);
void editorSaved(LiteApi::IEditor*);
void editorCreated(LiteApi::IEditor*);
void editorReloaded();
void editorAboutToClose(LiteApi::IEditor*);
void checkForReload();
void applyOption(QString);
Expand All @@ -107,6 +108,8 @@ public slots:
IFolderWindow *m_folderWindow;
QFileSystemWatcher *m_fileWatcher;
QMap<QString,QDateTime> m_fileStateMap;
QMap<QString,qint64> m_fileSizeMap;
QMap<QString,QByteArray> m_fileHashMap;
QSet<QString> m_changedFiles;
bool m_checkBlockActivated;
bool m_checkOnFocusChange;
Expand Down
Loading