changeset 30745:2f6904439d3c

fix updating the name of renamed editor files * files-dock-widget.cc (contextmenu_delete): emit file_renamed_signal in nay case, not only on errors while deleting the file * file-editor-tab.cc (set_file_name): update window title here; (enable_file_watcher): new function for en-/disabling the file watcher for the current file; (load_file): set modified state before setting the file name, do not set window title, which is done in set_file_name; (do_save_file): dito * file-editor-tab.h: new function enable_file_watcher, function set_file_name public * file-editor.cc (handle_file_remove): use removed_file_data instead of session_data, only disable file watching and remember editor tab and new file name; (handle_file_renamed): if succesfully renamed/removed, update the file name or close the file, otherwise just re-enable file watcher (handle_dir_remove): use removed_file_data, disable file watcher, remember editor tab and new file name * file-editor.h: new struct removed_file_data, m_tmp_closed_files is of type removed_file_data
author Torsten Lilge <ttl-octave@mailbox.org>
date Tue, 15 Feb 2022 18:57:16 +0100
parents e393ba9ab5d2
children d31d4f2504de
files libgui/src/files-dock-widget.cc libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/main-window.cc
diffstat 6 files changed, 70 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/files-dock-widget.cc	Tue Feb 15 11:57:26 2022 -0500
+++ b/libgui/src/files-dock-widget.cc	Tue Feb 15 18:57:16 2022 +0100
@@ -791,8 +791,8 @@
                                           tr ("Could not delete file \"%1\".").
                                           arg (info.filePath ()));
                     // Reload the old file
-                    emit file_renamed_signal (false);
                   }
+                emit file_renamed_signal (st);
               }
 
             m_file_system_model->revert ();
--- a/libgui/src/m-editor/file-editor-tab.cc	Tue Feb 15 11:57:26 2022 -0500
+++ b/libgui/src/m-editor/file-editor-tab.cc	Tue Feb 15 18:57:16 2022 +0100
@@ -509,6 +509,9 @@
         update_lexer ();
       }
 
+    // set the window title to actual filename (not modified)
+    update_window_title (m_edit_area->isModified ());
+
     // update the file editor with current editing directory
     emit editor_state_changed (m_copy_available, m_is_octave_file,
                                m_edit_area->isModified ());
@@ -532,6 +535,14 @@
     return true;
   }
 
+  void file_editor_tab::enable_file_watcher (bool do_enable)
+  {
+    if (do_enable)
+      m_file_system_watcher.addPath (m_file_name);
+    else
+      m_file_system_watcher.removePath (m_file_name);
+  }
+
   // We cannot create a breakpoint when the file is modified
   // because the line number the editor is providing might
   // not match what Octave core is interpreting in the
@@ -1845,9 +1856,8 @@
     QApplication::restoreOverrideCursor ();
 
     m_copy_available = false;     // no selection yet available
+    m_edit_area->setModified (false); // loaded file is not modified yet
     set_file_name (file_to_load);
-    update_window_title (false); // window title (no modification)
-    m_edit_area->setModified (false); // loaded file is not modified yet
 
     update_eol_indicator ();
 
@@ -2251,16 +2261,13 @@
         QFileInfo file_info = QFileInfo (file.fileName ());
         QString full_file_to_save = file_info.canonicalFilePath ();
 
-        // save filename after closing file as set_file_name starts watching again
-        set_file_name (full_file_to_save);   // make absolute
-
-        // set the window title to actual filename (not modified)
-        update_window_title (false);
-
         // file is save -> not modified, update encoding in statusbar
         m_edit_area->setModified (false);
         m_enc_indicator->setText (m_encoding);
 
+        // save filename after closing file as set_file_name starts watching again
+        set_file_name (full_file_to_save);   // make absolute
+
         emit tab_ready_to_close ();
 
         if (remove_on_success)
--- a/libgui/src/m-editor/file-editor-tab.h	Tue Feb 15 11:57:26 2022 -0500
+++ b/libgui/src/m-editor/file-editor-tab.h	Tue Feb 15 18:57:16 2022 +0100
@@ -64,6 +64,8 @@
     bool conditional_close (void);
 
     void update_breakpoints ();
+    void set_file_name (const QString& fileName);
+    void enable_file_watcher (bool do_enable);
 
     QString file_name (void) const { return m_file_name; }
     QString encoding (void) const { return m_encoding; }
@@ -255,7 +257,6 @@
   protected:
 
     void closeEvent (QCloseEvent *event);
-    void set_file_name (const QString& fileName);
 
   private:
 
--- a/libgui/src/m-editor/file-editor.cc	Tue Feb 15 11:57:26 2022 -0500
+++ b/libgui/src/m-editor/file-editor.cc	Tue Feb 15 18:57:16 2022 +0100
@@ -1167,7 +1167,7 @@
   {
     // Clear old list of file data and declare a structure for file data
     m_tmp_closed_files.clear ();
-    session_data f_data;
+    removed_file_data f_data;
 
     // Preprocessing old name(s)
     QString old_name_clean = old_name.trimmed ();
@@ -1201,22 +1201,12 @@
 
             if (editor_tab)
               {
-                // Get index and line.
-
-                f_data.encoding = editor_tab->encoding ();
-                f_data.index = m_tab_widget->indexOf (editor_tab);
-                int l, c;
-                editor_tab->qsci_edit_area ()->getCursorPosition (&l, &c);
-                f_data.line = l + 1;
-
-                // Close it silently
-                m_no_focus = true;  // Remember for not focussing editor
-                editor_tab->file_has_changed (QString (), true);  // Close the tab
-                m_no_focus = false;  // Back to normal
-
-                // For reloading old file if error while removing
-                f_data.file_name = old_names.at (i);
-                // For reloading new file (if new_file is not empty)
+
+                editor_tab->enable_file_watcher (false);
+
+                // For re-enabling tracking if error while removing/renaming
+                f_data.editor_tab = editor_tab;
+                // For renaming into new file (if new_file is not empty)
                 if (new_is_dir)
                   {
                     std::string ndir = new_name.toStdString ();
@@ -1239,27 +1229,24 @@
   {
     m_no_focus = true;  // Remember for not focussing editor
 
-    // Loop over all files that have to be reloaded.  Start at the end of the
+    // Loop over all files that have to be handled.  Start at the end of the
     // list, otherwise the stored indexes are not correct.
     for (int i = m_tmp_closed_files.count () - 1; i >= 0; i--)
       {
-        // Load old or new file
         if (load_new)
           {
-            if (! m_tmp_closed_files.at (i).new_file_name.isEmpty ())
-              request_open_file (m_tmp_closed_files.at (i).new_file_name,
-                                 m_tmp_closed_files.at (i).encoding,
-                                 m_tmp_closed_files.at (i).line,
-                                 false, false, true, "",
-                                 m_tmp_closed_files.at (i).index);
+            // Close file (remove) or rename into new file (rename)
+            if (m_tmp_closed_files.at (i).new_file_name.isEmpty ())
+              m_tmp_closed_files.at (i).editor_tab->file_has_changed (QString (), true);
+            else
+              m_tmp_closed_files.at (i).editor_tab->set_file_name (
+                                    m_tmp_closed_files.at (i).new_file_name);
           }
         else
           {
-            request_open_file (m_tmp_closed_files.at (i).file_name,
-                               m_tmp_closed_files.at (i).encoding,
-                               m_tmp_closed_files.at (i).line,
-                               false, false, true, "",
-                               m_tmp_closed_files.at (i).index);
+            // Something went wrong while renaming or removing:
+            // Leave everything as it is but reactivate tracking
+            m_tmp_closed_files.at (i).editor_tab->enable_file_watcher (true);
           }
 
       }
@@ -2704,7 +2691,7 @@
                                        const QString& new_name)
   {
     QDir old_dir (old_name);
-    session_data f_data;
+    removed_file_data f_data;
 
     std::list<file_editor_tab *> editor_tab_lst = m_tab_widget->tab_list ();
 
@@ -2731,52 +2718,39 @@
             && (rel_path_to_file.left (3) != QString ("../")))
           {
             // The currently considered file is included in the
-            // removed/renamed diectory: Delete it.
-            m_no_focus = true;  // Remember for not focussing editor
-
+            // removed/renamed diectory: remeber it
             if (editor_tab)
               {
-                // Get index and line
-                int l, c;
-                editor_tab->qsci_edit_area ()->getCursorPosition (&l, &c);
-                f_data.line = l + 1;
-                f_data.index = m_tab_widget->indexOf (editor_tab);
-                // Close
-                editor_tab->file_has_changed (QString (), true);
-              }
-            m_no_focus = false;  // Back to normal
-
-            // Store file for possible later reload
-            f_data.file_name = file_name;
-
-            // Add the new file path and the encoding for later reloading
-            // if new_name is given
-            if (! new_name.isEmpty ())
-              {
-                QDir new_dir (new_name);
-                QString append_to_new_dir;
-                if (new_dir.exists ())
+                editor_tab->enable_file_watcher (false);
+                f_data.editor_tab = editor_tab;
+
+                // Add the new file path and the encoding for later reloading
+                // if new_name is given
+                if (! new_name.isEmpty ())
                   {
-                    // The new directory already exists (movefile was used).
-                    // This means, we have to add the name (not the path)
-                    // of the old dir and the relative path to the file
-                    // to new dir.
-                    append_to_new_dir
-                      = old_dir.dirName () + "/" + rel_path_to_file;
+                    QDir new_dir (new_name);
+                    QString append_to_new_dir;
+                    if (new_dir.exists ())
+                      {
+                        // The new directory already exists (movefile was used).
+                        // This means, we have to add the name (not the path)
+                        // of the old dir and the relative path to the file
+                        // to new dir.
+                        append_to_new_dir
+                          = old_dir.dirName () + "/" + rel_path_to_file;
+                      }
+                    else
+                      append_to_new_dir = rel_path_to_file;
+
+                    f_data.new_file_name
+                      = new_dir.absoluteFilePath (append_to_new_dir);
                   }
                 else
-                  append_to_new_dir = rel_path_to_file;
-
-                f_data.new_file_name
-                  = new_dir.absoluteFilePath (append_to_new_dir);
+                  f_data.new_file_name = ""; // no new name, just removing this file
+
+                // Store data in list for later reloading
+                m_tmp_closed_files << f_data;
               }
-            else
-              f_data.new_file_name = ""; // no new name, just removing this file
-
-            f_data.encoding = editor_tab->encoding (); // store the encoding
-
-            // Store data in list for later reloading
-            m_tmp_closed_files << f_data;
           }
       }
   }
--- a/libgui/src/m-editor/file-editor.h	Tue Feb 15 11:57:26 2022 -0500
+++ b/libgui/src/m-editor/file-editor.h	Tue Feb 15 18:57:16 2022 +0100
@@ -89,6 +89,12 @@
       }
     };
 
+    struct removed_file_data
+    {
+      file_editor_tab *editor_tab;
+      QString new_file_name;
+    };
+
     file_editor (QWidget *p, base_qobject& oct_qobj);
 
     ~file_editor (void) = default;
@@ -487,7 +493,7 @@
     QPointer<find_dialog> m_find_dialog;
 
     // List of data on temporarily closed files for later reloading.
-    QList<session_data> m_tmp_closed_files;
+    QList<removed_file_data> m_tmp_closed_files;
   };
 }
 
--- a/libgui/src/main-window.cc	Tue Feb 15 11:57:26 2022 -0500
+++ b/libgui/src/main-window.cc	Tue Feb 15 18:57:16 2022 +0100
@@ -828,7 +828,6 @@
 
     // Wait for worker to suspend
     qt_link->lock ();
-
     // Close the file if opened
 #if defined (HAVE_QSCINTILLA)
     m_editor_window->handle_file_remove (o, n);