[latexila] Completion: UTF-8 friendly: calltip position



commit a2247a4efbe07dac3dd2cfcab5050e4868bec99f
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Wed Jan 18 22:09:25 2012 +0100

    Completion: UTF-8 friendly: calltip position

 src/completion.vala |   62 ++++++++++++++++++++++++++++++++------------------
 src/utils.vala      |    2 +-
 2 files changed, 41 insertions(+), 23 deletions(-)
---
diff --git a/src/completion.vala b/src/completion.vala
index 3fa0225..4e7a8bc 100644
--- a/src/completion.vala
+++ b/src/completion.vala
@@ -321,10 +321,10 @@ public class CompletionProvider : GLib.Object, SourceCompletionProvider
         return_if_fail (_commands.has_key (arg_cmd));
 
         CompletionCommand command = _commands[arg_cmd];
-        int num = get_argument_num (command.args, arguments);
-        if (num != -1)
+        int arg_num = get_argument_num (command.args, arguments);
+        if (arg_num != -1)
         {
-            string info = get_command_info (command, num);
+            string info = get_command_info (command, arg_num);
             show_calltip_info (info);
         }
     }
@@ -334,28 +334,18 @@ public class CompletionProvider : GLib.Object, SourceCompletionProvider
         if (_calltip_window == null)
             init_calltip_window ();
 
-        MainWindow win = Latexila.get_default ().active_window;
+        _calltip_window_label.set_markup (markup);
 
-        // calltip at a fixed place (after the '{' or '[' of the current arg)
-        TextIter pos;
-        TextBuffer buffer = win.active_view.buffer;
-        buffer.get_iter_at_mark (out pos, buffer.get_insert ());
-        string text = get_text_line_at_iter (pos);
-        for (long i = text.length - 1 ; i >= 0 ; i--)
-        {
-            if (text[i] == '[' || text[i] == '{')
-            {
-                if (Utils.char_is_escaped (text, i))
-                    continue;
-                pos.backward_chars ((int) (text.length - 1 - i));
-                break;
-            }
-        }
+        MainWindow window = Latexila.get_default ().active_window;
+        _calltip_window.set_transient_for (window);
 
-        _calltip_window_label.set_markup (markup);
+        // Calltip at a fixed place (after the '{' or '[' of the current argument).
+        TextIter cursor_pos;
+        TextBuffer buffer = window.active_view.buffer;
+        buffer.get_iter_at_mark (out cursor_pos, buffer.get_insert ());
+        TextIter begin_arg_pos = get_begin_arg_pos (cursor_pos);
+        _calltip_window.move_to_iter (window.active_view, begin_arg_pos);
 
-        _calltip_window.set_transient_for (win);
-        _calltip_window.move_to_iter (win.active_view, pos);
         _calltip_window.show_all ();
     }
 
@@ -516,6 +506,34 @@ public class CompletionProvider : GLib.Object, SourceCompletionProvider
     /*************************************************************************/
     // Parsing
 
+    // Get the position of the beginning of the argument. For example:
+    // \blah{foobar}
+    // The iter will be between the '{' and the 'f'.
+    private TextIter get_begin_arg_pos (TextIter in_arg_pos)
+    {
+        string text = get_text_line_at_iter (in_arg_pos);
+        int cur_index = text.length - 1;
+        int prev_index = cur_index;
+        unichar cur_char;
+
+        while (Utils.string_get_prev_char (text, ref prev_index, out cur_char))
+        {
+            if ((cur_char == '[' || cur_char == '{')
+                && ! Utils.char_is_escaped (text, cur_index))
+            {
+                break;
+            }
+
+            cur_index = prev_index;
+        }
+
+        TextIter begin_arg_pos = in_arg_pos;
+        begin_arg_pos.set_visible_line_index (cur_index);
+        begin_arg_pos.forward_char ();
+
+        return begin_arg_pos;
+    }
+
     private string? get_latex_command_at_iter (TextIter iter)
     {
         string text = get_text_line_at_iter (iter);
diff --git a/src/utils.vala b/src/utils.vala
index 52104bb..52bf6b7 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -63,7 +63,7 @@ namespace Utils
     // The opposite of string.get_next_char ().
     // TODO remove this function when it is included upstream
     // See https://bugzilla.gnome.org/show_bug.cgi?id=655185
-    private bool string_get_prev_char (string str, ref int index, out unichar c)
+    public bool string_get_prev_char (string str, ref int index, out unichar c)
     {
         c = str.get_char (index);
         if (index <= 0 || c == '\0')



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