[gedit-code-assistance/wip/indent] [indent] Split indentation in indent and alignment



commit 63716b86aacf0d8b47c57da2004ca1991f6d56f8
Author: Jesse van den Kieboom <jessevdk gmail com>
Date:   Fri Apr 11 16:56:37 2014 +0200

    [indent] Split indentation in indent and alignment

 indentbackends/c/gca-c-backend.vala |   28 ++++++++++++++++++----------
 src/gca-indent-backend.vala         |   35 +++++++++++++++++++++++++++--------
 src/gca-view.vala                   |   16 ++++++++--------
 3 files changed, 53 insertions(+), 26 deletions(-)
---
diff --git a/indentbackends/c/gca-c-backend.vala b/indentbackends/c/gca-c-backend.vala
index da8e80c..acc2364 100644
--- a/indentbackends/c/gca-c-backend.vala
+++ b/indentbackends/c/gca-c-backend.vala
@@ -108,15 +108,19 @@ class Backend : Object, Gca.IndentBackend
                return c;
        }
 
-       uint get_indent(Gedit.Document document, Gtk.TextIter place)
+       IndentLevel get_indent(Gedit.Document document, Gtk.TextIter place)
        {
-               uint amount = 0;
+               var amount = IndentLevel() {
+                       indent = 0,
+                       alignment = 0
+               };
+
                var iter = place;
 
                // if we are in the first line then 0 is fine
                if (iter.get_line() == 0)
                {
-                       return 0;
+                       return amount;
                }
 
                // are we a comment?
@@ -130,19 +134,19 @@ class Backend : Object, Gca.IndentBackend
                iter.set_line_offset(0);
                if (!iter.backward_char())
                {
-                       return 0;
+                       return amount;
                }
 
                if (!move_to_no_space(ref iter, false))
                {
-                       return 0;
+                       return amount;
                }
 
                if (document.iter_has_context_class(iter, "comment"))
                {
                        if (!document.iter_backward_to_context_class_toggle(ref iter, "comment"))
                        {
-                               return 0;
+                               return amount;
                        }
                        else
                        {
@@ -185,14 +189,14 @@ class Backend : Object, Gca.IndentBackend
 
                                if (get_first_char_in_line(place) != '{')
                                {
-                                       amount += get_indent_width();
+                                       amount.indent += get_indent_width();
                                }
                        }
                }
                else if (c == '{')
                {
                        amount = get_line_indents(iter);
-                       amount += get_indent_width();
+                       amount.indent += get_indent_width();
                }
                else if (c == '}')
                {
@@ -207,7 +211,8 @@ class Backend : Object, Gca.IndentBackend
                        if (find_open_char(ref copy, '(', ')', true))
                        {
                                // if we found it we want to align to the position of the first parameter
-                               amount = get_amount_indents_from_position(copy) + 1;
+                               amount = get_amount_indents_from_position(copy);
+                               amount.alignment += 1;
                        }
                        else
                        {
@@ -230,7 +235,10 @@ class Backend : Object, Gca.IndentBackend
                }
                else if (get_first_char_in_line(place) == '#')
                {
-                       amount = 0;
+                       amount = IndentLevel() {
+                               indent = 0,
+                               alignment = 0
+                       };
                }
 
                return amount;
diff --git a/src/gca-indent-backend.vala b/src/gca-indent-backend.vala
index eba6ee1..3f3f49d 100644
--- a/src/gca-indent-backend.vala
+++ b/src/gca-indent-backend.vala
@@ -20,21 +20,28 @@
 namespace Gca
 {
 
+public struct IndentLevel
+{
+       uint indent;
+       uint alignment;
+}
+
 public interface IndentBackend : Object
 {
        public abstract Gedit.View view { get; construct set; }
 
        /* These are the chars that trigger an extra indentation, i.e { */
        public abstract string[] get_triggers();
+
        /* It returns the indentation level */
-       public abstract uint get_indent(Gedit.Document document, Gtk.TextIter place);
+       public abstract IndentLevel get_indent(Gedit.Document document, Gtk.TextIter place);
 
        public uint get_indent_width()
        {
                return view.indent_width < 0 ? view.tab_width : view.indent_width;
        }
 
-       public uint get_line_indents(Gtk.TextIter place)
+       protected IndentLevel get_line_indents(Gtk.TextIter place)
        {
                var start = place;
                start.set_line_offset(0);
@@ -54,7 +61,7 @@ public interface IndentBackend : Object
                return get_amount_indents_from_position(start);
        }
 
-       public uint get_amount_indents_from_position(Gtk.TextIter place)
+       protected IndentLevel get_amount_indents_from_position(Gtk.TextIter place)
        {
                var indent_width = get_indent_width();
 
@@ -62,26 +69,37 @@ public interface IndentBackend : Object
                start.set_line_offset(0);
 
                int rest = 0;
-               uint amount = 0;
                var c = start.get_char();
+               bool seen_space = false;
+
+               var ret = IndentLevel() {
+                       indent = 0,
+                       alignment = 0
+               };
+
                while (start.compare(place) < 0)
                {
                        if (c == '\t')
                        {
-                               if (rest != 0)
+                               if (!seen_space)
+                               {
+                                       ret.indent += indent_width;
+                               }
+                               else
                                {
                                        rest = 0;
+                                       ret.alignment += indent_width;
                                }
-                               amount += indent_width;
                        }
                        else
                        {
+                               seen_space = true;
                                rest++;
                        }
 
                        if (rest == indent_width)
                        {
-                               amount += indent_width;
+                               ret.alignment += indent_width;
                                rest = 0;
                        }
 
@@ -93,7 +111,8 @@ public interface IndentBackend : Object
                        c = start.get_char();
                }
 
-               return amount + rest;
+               ret.alignment += rest;
+               return ret;
        }
 }
 
diff --git a/src/gca-view.vala b/src/gca-view.vala
index 58e8e15..e44e8b7 100644
--- a/src/gca-view.vala
+++ b/src/gca-view.vala
@@ -288,23 +288,25 @@ class View : Object
                return check;
        }
 
-       private string get_indent_string_from_indent_level(uint level)
+       private string get_indent_string_from_indent_level(IndentLevel level)
        {
                string indent = "";
 
                if (d_view.insert_spaces_instead_of_tabs)
                {
-                       indent = string.nfill(level, ' ');
+                       indent = string.nfill(level.indent, ' ');
                }
                else
                {
                        var indent_width = d_indent_backend.get_indent_width();
-                       uint tabs = level / indent_width;
-                       uint spaces = level % indent_width;
+                       uint tabs = level.indent / indent_width;
+                       uint spaces = level.indent % indent_width;
 
                        indent = string.nfill(tabs, '\t').concat(string.nfill(spaces, ' '));
                }
 
+               indent += string.nfill(level.alignment, ' ');
+
                return indent;
        }
 
@@ -359,11 +361,9 @@ class View : Object
 
                if (indent)
                {
-                       uint indent_level;
-
-                       indent_level = d_indent_backend.get_indent(buf, cur);
+                       var indent_level = d_indent_backend.get_indent(buf, cur);
 
-                       print("indent level: %u\n", indent_level);
+                       print("indent level: (%u, %u)\n", indent_level.indent, indent_level.alignment);
 
                        var start = cur;
                        start.set_line_offset(0);


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