[latexila] Do not comment empty or spaces-only lines



commit 36ba2e97f0ac404fe53770c7a7c68936375354b8
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 26 01:59:54 2011 +0200

    Do not comment empty or spaces-only lines
    
    This applies for commenting a structure item, or commenting via Edit ->
    Comment.

 src/document.vala           |   43 ++++++++++++++++++++++++++++++-------------
 src/document_structure.vala |   23 ++---------------------
 2 files changed, 32 insertions(+), 34 deletions(-)
---
diff --git a/src/document.vala b/src/document.vala
index 0088d7e..6ae7dd3 100644
--- a/src/document.vala
+++ b/src/document.vala
@@ -381,29 +381,46 @@ public class Document : Gtk.SourceBuffer
             cursor_moved ();
     }
 
-    private void insert_text_at_beginning_of_selected_lines (string text)
+    public void comment_selected_lines ()
     {
-        TextIter start, end;
+        TextIter start;
+        TextIter end;
         get_selection_bounds (out start, out end);
 
-        int start_line = start.get_line ();
-        int end_line = end.get_line ();
+        comment_between (start, end);
+    }
+
+    // comment the lines between start_iter and end_iter included
+    public void comment_between (TextIter start_iter, TextIter? end_iter)
+    {
+        int start_line = start_iter.get_line ();
+        int end_line = start_line;
+
+        if (end_iter != null)
+            end_line = end_iter.get_line ();
+
+        TextIter cur_iter;
+        get_iter_at_line (out cur_iter, start_line);
 
         begin_user_action ();
-        for (int i = start_line ; i <= end_line ; i++)
+        for (int i = start_line ; i <= end_line ; i++, cur_iter.forward_line ())
         {
-            TextIter iter;
-            get_iter_at_line (out iter, i);
-            insert (iter, text, -1);
+            // do not comment empty lines
+            if (cur_iter.ends_line ())
+                continue;
+
+            TextIter end_line_iter = cur_iter;
+            end_line_iter.forward_to_line_end ();
+
+            string line_contents = get_text (cur_iter, end_line_iter, false);
+
+            // do not comment lines containing only spaces
+            if (line_contents.strip () != "")
+                insert (cur_iter, "% ", -1);
         }
         end_user_action ();
     }
 
-    public void comment_selected_lines ()
-    {
-        insert_text_at_beginning_of_selected_lines ("% ");
-    }
-
     public void uncomment_selected_lines ()
     {
         TextIter start, end;
diff --git a/src/document_structure.vala b/src/document_structure.vala
index bebc273..db4bd58 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -777,7 +777,7 @@ public class DocumentStructure : GLib.Object
         /* comment a simple item */
         if (! Structure.is_section (type))
         {
-            comment_between (start_iter, end_iter);
+            _doc.comment_between (start_iter, end_iter);
             return true;
         }
 
@@ -820,29 +820,10 @@ public class DocumentStructure : GLib.Object
                 end_iter = null;
         }
 
-        comment_between (start_iter, end_iter);
+        _doc.comment_between (start_iter, end_iter);
         return true;
     }
 
-    // comment the lines between start_iter and end_iter included
-    private void comment_between (TextIter start_iter, TextIter? end_iter)
-    {
-        int start_line = start_iter.get_line ();
-        int end_line = start_line;
-
-        if (end_iter != null)
-            end_line = end_iter.get_line ();
-
-        _doc.begin_user_action ();
-        for (int line_index = start_line ; line_index <= end_line ; line_index++)
-        {
-            TextIter iter;
-            _doc.get_iter_at_line (out iter, line_index);
-            _doc.insert (iter, "% ", -1);
-        }
-        _doc.end_user_action ();
-    }
-
     // Returns true only if the bounds are correctly set.
     private bool get_exact_item_bounds (TreeIter tree_iter, out TextIter? start_iter,
         out TextIter? end_iter)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]