changeset 28132:4cfe24f56336

add editor prefs for forcing coding standards (bug #57860, bug #57861) * gui-preferences-ed.h: add new editor preferences for removing trailing spaces and forcing a newline at end of the file when saving the file. * file-editor-tab.cc (eol_string): new method for determining the current end of line string; (do_save_file): depending on the user preferences, rmove trailing spaces and/or add a newline at the end before saving the file * file-editor-tab.h: new private method eol_string * settings-dialog.cc (settings_dialog): initialize new check boxes for handling trailing spaces and newline at end of file with prefs from the settings file; (write_changed_settings): write checked states of new check boxes into the settings file * settings-dialog.ui: new check boxes for handling trailing spaces and newline at end of file
author Torsten Lilge <ttl-octave@mailbox.org>
date Sat, 22 Feb 2020 17:03:01 +0100
parents 712cd23aaa4f
children baf16e6f498b
files libgui/src/gui-preferences-ed.h libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/settings-dialog.cc libgui/src/settings-dialog.ui
diffstat 5 files changed, 85 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/gui-preferences-ed.h	Fri Feb 21 12:40:53 2020 -0800
+++ b/libgui/src/gui-preferences-ed.h	Sat Feb 22 17:03:01 2020 +0100
@@ -215,6 +215,12 @@
 const gui_pref
 ed_notebook_tab_width_max ("editor/notebook_tab_width_max", QVariant (300));
 
+const gui_pref
+ed_force_newline ("editor/force_newline", QVariant (true));
+
+const gui_pref
+ed_rm_trailing_spaces ("editor/rm_trailing_spaces", QVariant (true));
+
 #if defined (HAVE_QSCINTILLA)
 #if defined (Q_OS_WIN32)
 const int os_eol_mode = QsciScintilla::EolWindows;
--- a/libgui/src/m-editor/file-editor-tab.cc	Fri Feb 21 12:40:53 2020 -0800
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sat Feb 22 17:03:01 2020 +0100
@@ -1990,6 +1990,21 @@
     return eol_mode;
   }
 
+  QString file_editor_tab::eol_string (void)
+  {
+    switch (m_edit_area->eolMode ())
+      {
+      case QsciScintilla::EolWindows:
+        return ("\r\n");
+      case QsciScintilla::EolMac:
+        return ("\r");
+      case QsciScintilla::EolUnix:
+        return ("\n");
+      }
+
+    return QString ();
+  }
+
   void file_editor_tab::update_eol_indicator (void)
   {
     switch (m_edit_area->eolMode ())
@@ -2255,6 +2270,39 @@
     if (! codec)
       return;   // No valid codec
 
+    // Remove trailing white spaces and force file ending with
+    // a newline if desired
+
+    resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
+    gui_settings *settings = rmgr.get_settings ();
+
+    bool rm_trailing_space = settings->value (ed_rm_trailing_spaces).toBool ();
+    bool force_newline = settings->value (ed_force_newline).toBool ();
+
+    if (rm_trailing_space || force_newline)
+      {
+        int line, col;
+        m_edit_area->getCursorPosition (&line,&col);
+
+        QString eol = eol_string ();
+        QString edit_text = m_edit_area->text ();
+
+        if (rm_trailing_space)
+          edit_text.replace (QRegExp ("[\\t ]+" + eol), eol);
+
+        if (force_newline)
+          {
+            int last_non_white_space = edit_text.lastIndexOf (QRegExp ("\\S"));
+            edit_text.chop (edit_text.length () - last_non_white_space - 1);
+            edit_text.append (eol);
+          }
+
+          m_edit_area->setText (edit_text);
+
+          m_edit_area->setCursorPosition (line,col);
+        }
+
+    // Save the file
     out.setCodec (codec);
 
     QApplication::setOverrideCursor (Qt::WaitCursor);
--- a/libgui/src/m-editor/file-editor-tab.h	Fri Feb 21 12:40:53 2020 -0800
+++ b/libgui/src/m-editor/file-editor-tab.h	Sat Feb 22 17:03:01 2020 +0100
@@ -295,6 +295,7 @@
     QString get_function_name (void);
 
     QsciScintilla::EolMode detect_eol_mode (void);
+    QString eol_string (void);
     void update_eol_indicator (void);
 
     octave_qscintilla *m_edit_area;
--- a/libgui/src/settings-dialog.cc	Fri Feb 21 12:40:53 2020 -0800
+++ b/libgui/src/settings-dialog.cc	Sat Feb 22 17:03:01 2020 +0100
@@ -350,6 +350,8 @@
     editor_restoreSession->setChecked (settings->value (ed_restore_session).toBool ());
     editor_create_new_file->setChecked (settings->value (ed_create_new_file).toBool ());
     editor_reload_changed_files->setChecked (settings->value (ed_always_reload_changed_files).toBool ());
+    editor_force_newline->setChecked (settings->value (ed_force_newline).toBool ());
+    editor_remove_trailing_spaces->setChecked (settings->value (ed_rm_trailing_spaces).toBool ());
     editor_hiding_closes_files->setChecked (settings->value (ed_hiding_closes_files).toBool ());
     editor_show_dbg_file->setChecked (settings->value (ed_show_dbg_file).toBool ());
 
@@ -935,6 +937,8 @@
     settings->setValue (ed_create_new_file.key, editor_create_new_file->isChecked ());
     settings->setValue (ed_hiding_closes_files.key, editor_hiding_closes_files->isChecked ());
     settings->setValue (ed_always_reload_changed_files.key, editor_reload_changed_files->isChecked ());
+    settings->setValue (ed_force_newline.key, editor_force_newline->isChecked ());
+    settings->setValue (ed_rm_trailing_spaces.key, editor_remove_trailing_spaces->isChecked ());
     settings->setValue (ed_show_dbg_file.key, editor_show_dbg_file->isChecked ());
 
     settings->setValue (cs_font_size.key, terminal_fontSize->value ());
--- a/libgui/src/settings-dialog.ui	Fri Feb 21 12:40:53 2020 -0800
+++ b/libgui/src/settings-dialog.ui	Sat Feb 22 17:03:01 2020 +0100
@@ -32,7 +32,7 @@
       </size>
      </property>
      <property name="currentIndex">
-      <number>5</number>
+      <number>2</number>
      </property>
      <widget class="QWidget" name="tab_general">
       <property name="enabled">
@@ -841,9 +841,9 @@
           <property name="geometry">
            <rect>
             <x>0</x>
-            <y>0</y>
+            <y>-898</y>
             <width>1021</width>
-            <height>1467</height>
+            <height>1529</height>
            </rect>
           </property>
           <layout class="QVBoxLayout" name="verticalLayout_16">
@@ -2053,7 +2053,7 @@
                     </property>
                    </widget>
                   </item>
-                  <item row="8" column="0">
+                  <item row="10" column="0">
                    <layout class="QHBoxLayout" name="horizontalLayout_2">
                     <property name="topMargin">
                      <number>0</number>
@@ -2111,7 +2111,7 @@
                     </item>
                    </layout>
                   </item>
-                  <item row="10" column="0">
+                  <item row="12" column="0">
                    <layout class="QHBoxLayout" name="horizontalLayout_16">
                     <item>
                      <widget class="QLabel" name="label_16">
@@ -2138,7 +2138,7 @@
                     </item>
                    </layout>
                   </item>
-                  <item row="7" column="0">
+                  <item row="9" column="0">
                    <widget class="QCheckBox" name="editor_hiding_closes_files">
                     <property name="text">
                      <string>Close all files when the editor widget is closed/hidden</string>
@@ -2148,6 +2148,26 @@
                     </property>
                    </widget>
                   </item>
+                  <item row="5" column="0">
+                   <widget class="QCheckBox" name="editor_force_newline">
+                    <property name="text">
+                     <string>Force newline at end when saving file</string>
+                    </property>
+                    <property name="checked">
+                     <bool>true</bool>
+                    </property>
+                   </widget>
+                  </item>
+                  <item row="6" column="0">
+                   <widget class="QCheckBox" name="editor_remove_trailing_spaces">
+                    <property name="text">
+                     <string>Remove trailing spaces when saving file</string>
+                    </property>
+                    <property name="checked">
+                     <bool>true</bool>
+                    </property>
+                   </widget>
+                  </item>
                  </layout>
                 </item>
                </layout>