diff -r 4146ac0fd307 -r 9f6ffcd1db54 src/eric7/QScintilla/Editor.py --- a/src/eric7/QScintilla/Editor.py Mon Dec 04 15:21:07 2023 +0100 +++ b/src/eric7/QScintilla/Editor.py Mon Dec 04 19:00:59 2023 +0100 @@ -632,10 +632,13 @@ """ renamed = self.fileName != name + oldFileName = self.fileName self.fileName = name if renamed: self.vm.setEditorName(self, self.fileName) + self.vm.removeWatchedFilePath(oldFileName) + self.vm.addWatchedFilePath(self.fileName) if self.fileName: self.__fileNameExtension = os.path.splitext(self.fileName)[1][1:].lower() @@ -3375,15 +3378,20 @@ break # Couldn't find the unmodified state - def readFile(self, fn, createIt=False, encoding=""): + def readFile(self, fn, createIt=False, encoding="", noempty=False): """ Public method to read the text from a file. - @param fn filename to read from (string) + @param fn filename to read from + @type str @param createIt flag indicating the creation of a new file, if the - given one doesn't exist (boolean) - @param encoding encoding to be used to read the file (string) + given one doesn't exist (defaults to False) + @type bool (optional) + @param encoding encoding to be used to read the file (defaults to "") (Note: this parameter overrides encoding detection) + @type str (optional) + @param noempty flag indicating to not set an empty text (defaults to False) + @type bool (optional) """ self.__loadEditorConfig(fileName=fn) @@ -3411,6 +3419,9 @@ ) raise + if noempty and not bool(txt): + return + with EricOverrideCursor(): modified = False @@ -7774,51 +7785,16 @@ @type QFocusEvent """ self.recolor() + self.vm.editActGrp.setEnabled(True) self.vm.editorActGrp.setEnabled(True) self.vm.copyActGrp.setEnabled(True) self.vm.viewActGrp.setEnabled(True) self.vm.searchActGrp.setEnabled(True) + with contextlib.suppress(AttributeError): self.setCaretWidth(self.caretWidth) self.__updateReadOnly(False) - # TODO: realize this with a QFileSystemWatcher in ViewManager - if ( - self.vm.editorsCheckFocusInEnabled() - and not self.inReopenPrompt - and self.fileName - and pathlib.Path(self.fileName).exists() - and pathlib.Path(self.fileName).stat().st_mtime != self.lastModified - ): - self.inReopenPrompt = True - if Preferences.getEditor("AutoReopen") and not self.isModified(): - self.refresh() - else: - msg = self.tr( - """<p>The file <b>{0}</b> has been changed while it""" - """ was opened in eric. Reread it?</p>""" - ).format(self.fileName) - yesDefault = True - if self.isModified(): - msg += self.tr( - """<br><b>Warning:</b> You will lose""" - """ your changes upon reopening it.""" - ) - yesDefault = False - res = EricMessageBox.yesNo( - self, - self.tr("File changed"), - msg, - icon=EricMessageBox.Warning, - yesDefault=yesDefault, - ) - if res: - self.refresh() - else: - # do not prompt for this change again... - self.lastModified = pathlib.Path(self.fileName).stat().st_mtime - self.inReopenPrompt = False - self.setCursorFlashTime(QApplication.cursorFlashTime()) super().focusInEvent(event) @@ -8028,6 +8004,52 @@ ) or self.isReadOnly() @pyqtSlot() + def checkRereadFile(self): + """ + Public slot to check, if the file needs to be re-read, and refresh it if + needed. + """ + if ( + self.fileName + and pathlib.Path(self.fileName).exists() + and pathlib.Path(self.fileName).stat().st_mtime != self.lastModified + ): + if Preferences.getEditor("AutoReopen") and not self.isModified(): + self.refresh() + else: + msg = self.tr( + """<p>The file <b>{0}</b> has been changed while it""" + """ was opened in eric. Reread it?</p>""" + ).format(self.fileName) + yesDefault = True + if self.isModified(): + msg += self.tr( + """<br><b>Warning:</b> You will lose""" + """ your changes upon reopening it.""" + ) + yesDefault = False + res = EricMessageBox.yesNo( + self, + self.tr("File changed"), + msg, + icon=EricMessageBox.Warning, + yesDefault=yesDefault, + ) + if res: + self.refresh() + else: + # do not prompt for this change again... + self.lastModified = pathlib.Path(self.fileName).stat().st_mtime + + @pyqtSlot() + def recordModificationTime(self): + """ + Public slot to record the modification time of our file. + """ + if self.fileName and pathlib.Path(self.fileName).exists(): + self.lastModified = pathlib.Path(self.fileName).stat().st_mtime + + @pyqtSlot() def refresh(self): """ Public slot to refresh the editor contents. @@ -8057,7 +8079,7 @@ # reread the file try: - self.readFile(self.fileName) + self.readFile(self.fileName, noempty=True) except OSError: # do not prompt for this change again... self.lastModified = QDateTime.currentDateTime()