[latexila/structure-actions] Structure action: comment (finished)



commit 13bba1147ab77a3cb2036529b91970024b2ba71b
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Mon Jun 27 18:18:49 2011 +0200

    Structure action: comment (finished)
    
    We can also comment TODO and FIXME comments (yes, comment a comment :)

 src/document_structure.vala |   48 +++++++++++++++++++++++++++++++++++++++---
 src/structure.vala          |    2 +-
 src/structure_model.vala    |   31 ++++++++++++++++++++++++++-
 3 files changed, 75 insertions(+), 6 deletions(-)
---
diff --git a/src/document_structure.vala b/src/document_structure.vala
index a087abe..a263743 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -482,9 +482,15 @@ public class DocumentStructure : GLib.Object
 
     public void do_action (StructAction action_type, TreeIter tree_iter)
     {
-        if (action_type != StructAction.COMMENT)
+        if (action_type == StructAction.COMMENT)
+        {
+            do_comment (tree_iter);
             return;
+        }
+    }
 
+    private void do_comment (TreeIter tree_iter)
+    {
         StructType type;
         TextMark start_mark = null;
         TextMark end_mark = null;
@@ -495,9 +501,6 @@ public class DocumentStructure : GLib.Object
             StructColumn.END_MARK, out end_mark,
             -1);
 
-        if (Structure.is_section (type))
-            return;
-
         TextIter start_iter;
         TextIter? end_iter = null;
 
@@ -506,6 +509,43 @@ public class DocumentStructure : GLib.Object
         if (end_mark != null)
             _doc.get_iter_at_mark (out end_iter, end_mark);
 
+        /* comment a simple item */
+        if (! Structure.is_section (type))
+        {
+            comment (start_iter, end_iter);
+            return;
+        }
+
+        /* comment a section */
+
+        // get next sibling or parent
+        TreeIter? next_section_iter = null;
+        try
+        {
+            next_section_iter = _model.get_next_sibling_or_parent (tree_iter);
+        }
+        catch (StructError e)
+        {
+            stderr.printf ("Structure: get next sibling or parent: %s\n", e.message);
+            return;
+        }
+
+        // the end of the section is the end of the document
+        if (next_section_iter == null)
+            _doc.get_end_iter (out end_iter);
+
+        // go one line backward
+        else
+        {
+            _model.get (next_section_iter,
+                StructColumn.START_MARK, out end_mark,
+                -1);
+
+            _doc.get_iter_at_mark (out end_iter, end_mark);
+            if (! end_iter.backward_line ())
+                end_iter = null;
+        }
+
         comment (start_iter, end_iter);
     }
 
diff --git a/src/structure.vala b/src/structure.vala
index ca173b9..5902156 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -528,7 +528,7 @@ public class Structure : VBox
         _action_copy.sensitive = true;
         _action_delete.sensitive = true;
         _action_select.sensitive = true;
-        _action_comment.sensitive = type != StructType.TODO && type != StructType.FIXME;
+        _action_comment.sensitive = true;
 
         _action_shift_left.sensitive =
             StructType.PART < type && type <= StructType.SUBPARAGRAPH;
diff --git a/src/structure_model.vala b/src/structure_model.vala
index 4dfaf45..9854189 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -46,6 +46,10 @@ public enum StructListColumn
     N_COLUMNS
 }
 
+public errordomain StructError {
+    GENERAL
+}
+
 public class StructureModel : TreeModel, GLib.Object
 {
     private Type[] _column_types;
@@ -341,7 +345,7 @@ public class StructureModel : TreeModel, GLib.Object
 
 
     /*************************************************************************/
-    // Custom methods (add an item)
+    // Custom methods
 
     public void add_item_at_end (StructData item)
     {
@@ -427,6 +431,31 @@ public class StructureModel : TreeModel, GLib.Object
         }
     }
 
+    // With the iter returned, we can simply go one line backward and we have the end of
+    // the section. If null is returned, the end of the section is the end of the doc.
+    public TreeIter? get_next_sibling_or_parent (TreeIter section_iter) throws StructError
+    {
+        if (! iter_is_valid (section_iter))
+            throw new StructError.GENERAL ("iter is not valid.");
+
+        unowned Node<StructData?> cur_node = get_node_from_iter (section_iter);
+
+        if (! Structure.is_section (cur_node.data.type))
+            throw new StructError.GENERAL ("iter is not a section.");
+
+        while (cur_node != null && cur_node != _tree)
+        {
+            unowned Node<StructData?>? next_sibling_node = cur_node.next_sibling ();
+
+            if (next_sibling_node != null)
+                return create_iter_at_node (next_sibling_node);
+
+            cur_node = cur_node.parent;
+        }
+
+        return null;
+    }
+
     private void insert_item_at_position (StructData item, Node<StructData?> parent,
         int pos)
     {



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