[latexila] Structure action: comment



commit 18313448489225e568be64188d48842fd09eeedd
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Mon Jun 27 00:50:56 2011 +0200

    Structure action: comment
    
    We can also comment TODO and FIXME comments (yes, comment a comment :)
    
    What still need to be done for the comment:
    - remove commented item from the model

 src/document_structure.vala |   94 +++++++++++++++++++++++++++++++++++++++++++
 src/main_window.vala        |   58 +++++++++++++++++++++++---
 src/structure.vala          |   58 ++++++++++++---------------
 src/structure_model.vala    |   61 ++++++++++++++++++++++------
 4 files changed, 219 insertions(+), 52 deletions(-)
---
diff --git a/src/document_structure.vala b/src/document_structure.vala
index ee5168a..a263743 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -93,6 +93,9 @@ public class DocumentStructure : GLib.Object
         return _model;
     }
 
+    /*************************************************************************/
+    // Parsing stuff
+
     // Parse the document. Returns false if finished, true otherwise.
     private bool parse_impl ()
     {
@@ -473,4 +476,95 @@ public class DocumentStructure : GLib.Object
                 return null;
         }
     }
+
+    /*************************************************************************/
+    // Actions: cut, copy, delete, select, comment, shift left/right
+
+    public void do_action (StructAction action_type, TreeIter tree_iter)
+    {
+        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;
+
+        _model.get (tree_iter,
+            StructColumn.TYPE, out type,
+            StructColumn.START_MARK, out start_mark,
+            StructColumn.END_MARK, out end_mark,
+            -1);
+
+        TextIter start_iter;
+        TextIter? end_iter = null;
+
+        _doc.get_iter_at_mark (out start_iter, start_mark);
+
+        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);
+    }
+
+    // comment the lines between start_iter and end_iter included
+    private void comment (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 ();
+    }
 }
diff --git a/src/main_window.vala b/src/main_window.vala
index beef23b..7f4270a 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -134,22 +134,22 @@ public class MainWindow : Window
         // Structure
         { "Structure", null, N_("S_tructure") },
         { "StructureCut", Stock.CUT, null, "",
-            N_("Cut the selected structure item"), Structure.on_cut },
+            N_("Cut the selected structure item"), on_structure_cut },
         { "StructureCopy", Stock.COPY, null, "",
-            N_("Copy the selected structure item"), Structure.on_copy },
+            N_("Copy the selected structure item"), on_structure_copy },
         { "StructureDelete", Stock.DELETE, null, "",
-            N_("Delete the selected structure item"), Structure.on_delete },
+            N_("Delete the selected structure item"), on_structure_delete },
         { "StructureSelect", Stock.SELECT_ALL, N_("_Select"), "",
             N_("Select the contents of the selected structure item"),
-            Structure.on_select },
+            on_structure_select },
         { "StructureComment", null, N_("_Comment"), null,
-            N_("Comment the selected structure item"), Structure.on_comment },
+            N_("Comment the selected structure item"), on_structure_comment },
         { "StructureShiftLeft", Stock.GO_BACK, N_("Shift _Left"), "",
             N_("Shift the selected structure item to the left (e.g. section â chapter"),
-            Structure.on_shift_left },
+            on_structure_shift_left },
         { "StructureShiftRight", Stock.GO_FORWARD, N_("Shift _Right"), "",
             N_("Shift the selected structure item to the right (e.g. chapter â section"),
-            Structure.on_shift_right },
+            on_structure_shift_right },
 
         // Help
         { "Help", null, N_("_Help") },
@@ -1792,6 +1792,50 @@ public class MainWindow : Window
         ProjectDialogs.manage_projects (this);
     }
 
+    /* Structure */
+
+    public void on_structure_cut ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.CUT);
+    }
+
+    public void on_structure_copy ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.COPY);
+    }
+
+    public void on_structure_delete ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.DELETE);
+    }
+
+    public void on_structure_select ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.SELECT);
+    }
+
+    public void on_structure_comment ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.COMMENT);
+    }
+
+    public void on_structure_shift_left ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.SHIFT_LEFT);
+    }
+
+    public void on_structure_shift_right ()
+    {
+        return_if_fail (_structure != null);
+        _structure.do_action (StructAction.SHIFT_RIGHT);
+    }
+
     /* Help */
 
     public void on_help_latex_reference ()
diff --git a/src/structure.vala b/src/structure.vala
index 835f056..5902156 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -38,6 +38,17 @@ public enum StructType
     N_TYPES
 }
 
+public enum StructAction
+{
+    CUT,
+    COPY,
+    DELETE,
+    SELECT,
+    COMMENT,
+    SHIFT_LEFT,
+    SHIFT_RIGHT
+}
+
 public class Structure : VBox
 {
     private unowned MainWindow _main_window;
@@ -55,6 +66,7 @@ public class Structure : VBox
     private VPaned _vpaned;
 
     private TreeView _tree_view;
+    private DocumentStructure _document_structure = null;
     private StructureModel? _model = null;
 
     private TreeView _list_view;
@@ -384,7 +396,7 @@ public class Structure : VBox
         TextMark mark;
         StructType type;
         _model.get (tree_iter,
-            StructColumn.MARK, out mark,
+            StructColumn.START_MARK, out mark,
             StructColumn.TYPE, out type,
             -1);
 
@@ -454,18 +466,18 @@ public class Structure : VBox
         if (doc == null)
             return;
 
-        DocumentStructure doc_struct = doc.get_structure ();
+        _document_structure = doc.get_structure ();
 
         if (force_parse)
-            doc_struct.parse ();
+            _document_structure.parse ();
 
-        if (doc_struct.parsing_done)
-            set_model (doc_struct.get_model ());
+        if (_document_structure.parsing_done)
+            set_model (_document_structure.get_model ());
 
-        doc_struct.notify["parsing-done"].connect (() =>
+        _document_structure.notify["parsing-done"].connect (() =>
         {
-            if (doc_struct.parsing_done)
-                set_model (doc_struct.get_model ());
+            if (_document_structure.parsing_done)
+                set_model (_document_structure.get_model ());
         });
     }
 
@@ -516,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;
@@ -524,32 +536,14 @@ public class Structure : VBox
         _action_shift_right.sensitive = type < StructType.SUBPARAGRAPH;
     }
 
-    public static void on_cut ()
-    {
-    }
-
-    public static void on_copy ()
+    public void do_action (StructAction action_type)
     {
-    }
+        TreeIter selected_iter;
+        int selected_row = Utils.get_selected_row (_tree_view, out selected_iter);
 
-    public static void on_delete ()
-    {
-    }
+        return_if_fail (selected_row != -1);
 
-    public static void on_select ()
-    {
-    }
-
-    public static void on_comment ()
-    {
-    }
-
-    public static void on_shift_left ()
-    {
-    }
-
-    public static void on_shift_right ()
-    {
+        _document_structure.do_action (action_type, selected_iter);
     }
 
     public static string get_icon_from_type (StructType type)
diff --git a/src/structure_model.vala b/src/structure_model.vala
index 7bdefe9..9854189 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -32,7 +32,8 @@ public enum StructColumn
     PIXBUF,
     TEXT,
     TOOLTIP,
-    MARK,
+    START_MARK,
+    END_MARK,
     TYPE,
     N_COLUMNS
 }
@@ -45,6 +46,10 @@ public enum StructListColumn
     N_COLUMNS
 }
 
+public errordomain StructError {
+    GENERAL
+}
+
 public class StructureModel : TreeModel, GLib.Object
 {
     private Type[] _column_types;
@@ -60,22 +65,23 @@ public class StructureModel : TreeModel, GLib.Object
     public StructureModel ()
     {
         _column_types = new Type[StructColumn.N_COLUMNS];
-        _column_types[StructColumn.PIXBUF]  = typeof (string);
-        _column_types[StructColumn.TEXT]    = typeof (string);
-        _column_types[StructColumn.TOOLTIP] = typeof (string);
-        _column_types[StructColumn.MARK]    = typeof (TextMark);
-        _column_types[StructColumn.TYPE]    = typeof (StructType);
+        _column_types[StructColumn.PIXBUF]      = typeof (string);
+        _column_types[StructColumn.TEXT]        = typeof (string);
+        _column_types[StructColumn.TOOLTIP]     = typeof (string);
+        _column_types[StructColumn.START_MARK]  = typeof (TextMark);
+        _column_types[StructColumn.END_MARK]    = typeof (TextMark);
+        _column_types[StructColumn.TYPE]        = typeof (StructType);
 
         StructData empty_data = {};
         _tree = new Node<StructData?> (empty_data);
 
         new_stamp ();
 
-        _list_labels = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_includes = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_tables = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_figures = new Gee.ArrayList<unowned Node<StructData?>> ();
-        _list_todo_and_fixme = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_labels            = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_includes          = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_tables            = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_figures           = new Gee.ArrayList<unowned Node<StructData?>> ();
+        _list_todo_and_fixme    = new Gee.ArrayList<unowned Node<StructData?>> ();
     }
 
     // A new stamp should be generated each time the data in the model change
@@ -192,10 +198,14 @@ public class StructureModel : TreeModel, GLib.Object
                 val = data.text;
                 break;
 
-            case StructColumn.MARK:
+            case StructColumn.START_MARK:
                 val = data.start_mark;
                 break;
 
+            case StructColumn.END_MARK:
+                val = data.end_mark;
+                break;
+
             case StructColumn.TYPE:
                 val = data.type;
                 break;
@@ -335,7 +345,7 @@ public class StructureModel : TreeModel, GLib.Object
 
 
     /*************************************************************************/
-    // Custom methods (add an item)
+    // Custom methods
 
     public void add_item_at_end (StructData item)
     {
@@ -421,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]