[latexila/structure-actions: 1/2] Structure: create the TextMarks at the exact places



commit d3accf75fdd56c6480bd7c4e020075d7b06870b4
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Fri Jul 1 19:00:07 2011 +0200

    Structure: create the TextMarks at the exact places
    
    Before, the TextMarks were created at the beginning of lines. Now it's
    located just before the item starts (i.e. before the '\' or '%').
    
    The function used is get_iter_at_line_index().

 TODO                        |    3 -
 src/document_structure.vala |  110 ++++++++++++++++---------------------------
 src/structure.vala          |    4 +-
 3 files changed, 43 insertions(+), 74 deletions(-)
---
diff --git a/TODO b/TODO
index c9fcfd1..31ebec9 100644
--- a/TODO
+++ b/TODO
@@ -25,9 +25,6 @@ LaTeXila 2.2
 	- Insert in middle is very slow comparated to insert at the end
 	  => Insert environments as soon as we find the \begin, and then update the item (or delete it)
 
-	- Create the TextMarks at the right places with get_iter_at_line_index(),
-	  and simplify the code for actions
-
 	- Take into account \end{document}
 
 - Write some documentation:
diff --git a/src/document_structure.vala b/src/document_structure.vala
index 12a3e0b..cf23203 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -49,7 +49,6 @@ public class DocumentStructure : GLib.Object
         CAPTION
     }
 
-
     private unowned TextBuffer _doc;
     private int _nb_marks = 0;
     private static const string MARK_NAME_PREFIX = "struct_item_";
@@ -140,9 +139,6 @@ public class DocumentStructure : GLib.Object
         int nb_lines = _doc.get_line_count ();
         int stop_parsing_line = _start_parsing_line + MAX_NB_LINES_TO_PARSE;
 
-        TextIter cur_line_iter;
-        _doc.get_iter_at_line (out cur_line_iter, cur_line);
-
         // The parsing is done line-by-line.
         while (cur_line < nb_lines)
         {
@@ -169,20 +165,22 @@ public class DocumentStructure : GLib.Object
             {
                 LowLevelType? type;
                 string? contents;
+                int? start_match_index;
                 int? end_match_index;
 
                 bool item_found = search_low_level_item (line, start_index, out type,
-                    out contents, null, out end_match_index);
+                    out contents, out start_match_index, out end_match_index);
 
                 if (! item_found)
                     break;
 
-                handle_item (type, contents, cur_line_iter);
+                TextIter iter;
+                _doc.get_iter_at_line_index (out iter, cur_line, start_match_index);
+                handle_item (type, contents, iter);
 
                 start_index = end_match_index;
             }
 
-            cur_line_iter.forward_line ();
             cur_line++;
         }
 
@@ -378,7 +376,7 @@ public class DocumentStructure : GLib.Object
         return true;
     }
 
-    private void handle_item (LowLevelType type, string? contents, TextIter cur_line_iter)
+    private void handle_item (LowLevelType type, string? contents, TextIter iter)
     {
         // we are currently in a verbatim env
         if (_in_verbatim_env)
@@ -391,7 +389,7 @@ public class DocumentStructure : GLib.Object
 
         // the low-level type is common with the high-level type
         else if (type < LowLevelType.NB_COMMON_TYPES)
-            add_item ((StructType) type, contents, cur_line_iter);
+            add_item ((StructType) type, contents, iter);
 
         // begin of a verbatim env
         else if (type == LowLevelType.BEGIN_VERBATIM)
@@ -399,7 +397,7 @@ public class DocumentStructure : GLib.Object
 
         // begin of a figure or table env
         else if (type == LowLevelType.BEGIN_FIGURE || type == LowLevelType.BEGIN_TABLE)
-            create_new_environment (type, cur_line_iter);
+            create_new_environment (type, iter);
 
         // a caption (we take only the first)
         else if (type == LowLevelType.CAPTION && _env_data != null
@@ -415,7 +413,7 @@ public class DocumentStructure : GLib.Object
         // end of a figure or table env
         else if (verify_end_environment_type (type))
         {
-            _env_data.end_mark = create_text_mark_from_iter (cur_line_iter);
+            _env_data.end_mark = create_text_mark_from_iter (iter);
             add_item_data (_env_data, true);
         }
     }
@@ -662,22 +660,16 @@ public class DocumentStructure : GLib.Object
             -1);
 
         /* search 'start_iter' */
-        TextIter line_iter;
-        _doc.get_iter_at_mark (out line_iter, start_mark);
-        int line_num = line_iter.get_line ();
-
-        int? start_match_index;
-        int? end_match_index;
+        _doc.get_iter_at_mark (out start_iter, start_mark);
 
-        bool found = get_low_level_item_bounds (item_type, item_contents, line_num, true,
-            out start_match_index, out end_match_index);
+        // Place 'end_iter' to the end of the low level type. If it's not the good place,
+        // it will be changed below.
+        bool found = get_low_level_item_bounds (item_type, item_contents, start_iter,
+            true, out end_iter);
 
         if (! found)
             return false;
 
-        // set 'start_iter'
-        _doc.get_iter_at_line_index (out start_iter, line_num, start_match_index);
-
         /* search 'end_iter' */
 
         // a section
@@ -707,75 +699,55 @@ public class DocumentStructure : GLib.Object
                 StructColumn.TEXT, out item_contents,
                 -1);
 
-            _doc.get_iter_at_mark (out line_iter, start_mark);
-            line_num = line_iter.get_line ();
-
-            found = get_low_level_item_bounds (item_type, item_contents, line_num, true,
-                out start_match_index, null);
+            _doc.get_iter_at_mark (out end_iter, start_mark);
 
-            if (! found)
-                return false;
-
-            _doc.get_iter_at_line_index (out end_iter, line_num, start_match_index);
-            return true;
+            return get_low_level_item_bounds (item_type, item_contents, end_iter, true,
+                null);
         }
 
-        // an other common type
+        // an other common type: the end iter is already at the good place
         else if (item_type < StructType.NB_COMMON_TYPES)
-        {
-            _doc.get_iter_at_line_index (out end_iter, line_num, end_match_index);
             return true;
-        }
 
         // an environment
         if (end_mark == null)
             return false;
 
-        _doc.get_iter_at_mark (out line_iter, end_mark);
-        line_num = line_iter.get_line ();
-
-        found = get_low_level_item_bounds (item_type, item_contents, line_num, false,
-            null, out end_match_index);
+        TextIter end_env_iter;
+        _doc.get_iter_at_mark (out end_env_iter, end_mark);
 
-        if (! found)
-            return false;
-
-        _doc.get_iter_at_line_index (out end_iter, line_num, end_match_index);
-
-        return true;
+        return get_low_level_item_bounds (item_type, item_contents, end_env_iter, false,
+            out end_iter);
     }
 
     private bool get_low_level_item_bounds (StructType item_type, string item_contents,
-        int line_num, bool is_start, out int? start_match_index, out int? end_match_index)
+        TextIter start_match_iter, bool is_start, out TextIter? end_match_iter)
     {
+        int line_num = start_match_iter.get_line ();
         string line = get_document_line_contents (line_num);
 
         /* parse the line */
-        int start_index = 0;
-        int line_length = line.length;
-
-        while (true)
-        {
-            if (line_length <= start_index)
-                break;
-
-            LowLevelType? low_level_type;
-            string? contents;
-
-            bool found = search_low_level_item (line, start_index, out low_level_type,
-                out contents, out start_match_index, out end_match_index);
+        int start_index = start_match_iter.get_line_index ();
+        LowLevelType? low_level_type;
+        string? contents;
+        int? start_match_index;
+        int? end_match_index;
 
-            if (! found)
-                break;
+        bool found = search_low_level_item (line, start_index, out low_level_type,
+            out contents, out start_match_index, out end_match_index);
 
-            if (contents == null)
-                contents = "";
+        // If an item is found, it should be located at exactly the same place.
+        if (! found || start_index != start_match_index)
+            return false;
 
-            // compare the item found with the structure item
-            if (same_items (item_type, item_contents, low_level_type, contents, is_start))
-                return true;
+        if (contents == null)
+            contents = "";
 
-            start_index = end_match_index;
+        // compare the item found with the structure item
+        if (same_items (item_type, item_contents, low_level_type, contents, is_start))
+        {
+            _doc.get_iter_at_line_index (out end_match_iter, line_num, end_match_index);
+            return true;
         }
 
         return false;
diff --git a/src/structure.vala b/src/structure.vala
index 968aebc..e8394a7 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -414,8 +414,8 @@ public class Structure : VBox
         doc.get_iter_at_mark (out text_iter, mark);
         doc.place_cursor (text_iter);
 
-        // scroll to cursor, line at the top
-        _main_window.active_view.scroll_to_mark (doc.get_insert (), 0, true, 0, 0);
+        // scroll to cursor, line at the top (no horizontal scroll)
+        _main_window.active_view.scroll_to_mark (doc.get_insert (), 0, true, 1, 0);
 
         /* select the corresponding item in the simple list */
         set_actions_sensitivity (type);



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