4242#include < QFileInfo>
4343#include < QDateTime>
4444#include < QFileSystemWatcher>
45+ #include < QCryptographicHash>
4546#include < QMessageBox>
4647#include < QTimer>
4748#include < QDesktopServices>
@@ -547,12 +548,27 @@ void FileManager::applyOption(QString id)
547548 m_fileWatcherAutoReload = m_liteApp->settings ()->value (LITEAPP_FILEWATCHERAUTORELOAD ,false ).toBool ();
548549}
549550
551+ static QByteArray fileContentHash (const QString &fileName)
552+ {
553+ QFile file (fileName);
554+ if (!file.open (QIODevice::ReadOnly)) {
555+ return QByteArray ();
556+ }
557+ QCryptographicHash hash (QCryptographicHash::Sha256);
558+ if (!hash.addData (&file)) {
559+ return QByteArray ();
560+ }
561+ return hash.result ();
562+ }
563+
550564void FileManager::updateFileState (const QString &fileName)
551565{
552566 if (fileName.isEmpty ()) {
553567 return ;
554568 }
555569 m_fileStateMap.insert (fileName,QFileInfo (fileName).lastModified ());
570+ m_fileSizeMap.insert (fileName,QFileInfo (fileName).size ());
571+ m_fileHashMap.insert (fileName,fileContentHash (fileName));
556572 if (!m_fileWatcher->files ().contains (fileName)) {
557573 m_fileWatcher->addPath (fileName);;
558574 }
@@ -563,6 +579,7 @@ void FileManager::editorCreated(LiteApi::IEditor *editor)
563579 if (!editor) {
564580 return ;
565581 }
582+ connect (editor,SIGNAL (reloaded ()),this ,SLOT (editorReloaded ()));
566583 QString fileName = editor->filePath ();
567584 if (!fileName.isEmpty ()) {
568585 updateFileState (fileName);
@@ -577,6 +594,8 @@ void FileManager::editorAboutToClose(LiteApi::IEditor *editor)
577594 QString fileName = editor->filePath ();
578595 if (!fileName.isEmpty ()) {
579596 m_fileStateMap.remove (fileName);
597+ m_fileSizeMap.remove (fileName);
598+ m_fileHashMap.remove (fileName);
580599 m_changedFiles.remove (fileName);
581600 m_fileWatcher->removePath (fileName);
582601 }
@@ -590,6 +609,11 @@ void FileManager::editorSaved(LiteApi::IEditor *editor)
590609 updateFileState (editor->filePath ());
591610}
592611
612+ void FileManager::editorReloaded ()
613+ {
614+ editorSaved (qobject_cast<LiteApi::IEditor*>(sender ()));
615+ }
616+
593617void FileManager::fileChanged (QString fileName)
594618{
595619 const bool wasempty = m_changedFiles.isEmpty ();
@@ -712,6 +736,14 @@ void FileManager::checkForReload()
712736 if (!m_fileWatcher->files ().contains (fileName)) {
713737 m_fileWatcher->addPath (fileName);;
714738 }
739+ // Compare with the last loaded/saved file, not the unsaved editor buffer.
740+ const QFileInfo fileInfo (fileName);
741+ const bool sameSize = fileInfo.size () == m_fileSizeMap.value (fileName, -1 );
742+ const QByteArray hash = sameSize ? fileContentHash (fileName) : QByteArray ();
743+ if (sameSize && !hash.isEmpty () && hash == m_fileHashMap.value (fileName)) {
744+ m_fileStateMap.insert (fileName,lastModified);
745+ continue ;
746+ }
715747 if (lastModified > modified) {
716748 int ret = QMessageBox::Yes;
717749 if (lastReloadRet != QMessageBox::YesToAll) {
@@ -734,9 +766,10 @@ void FileManager::checkForReload()
734766 QDateTime lastModified = QFileInfo (fileName).lastModified ();
735767 QDateTime modified = m_fileStateMap.value (fileName);
736768 if (lastModified != modified) {
737- editor->reload ();
738- m_fileStateMap.insert (fileName,lastModified);
739- m_liteApp->appendLog (" EditorManager" ,fileName+" reload" ,false );
769+ if (editor->reload ()) {
770+ updateFileState (fileName);
771+ m_liteApp->appendLog (" EditorManager" ,fileName+" reload" ,false );
772+ }
740773
741774 }
742775 }
0 commit comments