[gnome-builder] ctags: try to avoid activating various keywords



commit cc287c26f87c3c778b4822124c1a06ae388e8fa3
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 8 17:15:43 2017 -0800

    ctags: try to avoid activating various keywords
    
    We don't have a really great way to deal with this using the
    completion system provided by GtkSourceView (as there is not a
    way to get the activation key to perform the desired indent or
    space). But we can at least avoid activating the item and leave
    the insertion cursor where it was.
    
    To do this better, we need to build a new completion engine for
    Builder/GSV that does things a bit differently.
    
    Either way, this avoids some common keywords. If you're trying
    to get a newline afterwards, you'll have to hit enter a second
    time (as this will just dismiss the completion window).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=790101

 src/plugins/ctags/ide-ctags-completion-provider.c |   38 +++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
---
diff --git a/src/plugins/ctags/ide-ctags-completion-provider.c 
b/src/plugins/ctags/ide-ctags-completion-provider.c
index a42ed8c..3edbf42 100644
--- a/src/plugins/ctags/ide-ctags-completion-provider.c
+++ b/src/plugins/ctags/ide-ctags-completion-provider.c
@@ -31,6 +31,8 @@
 static void provider_iface_init  (GtkSourceCompletionProviderIface *iface);
 static void provider2_iface_init (IdeCompletionProviderInterface   *iface);
 
+static GHashTable *reserved;
+
 G_DEFINE_DYNAMIC_TYPE_EXTENDED (IdeCtagsCompletionProvider,
                                 ide_ctags_completion_provider,
                                 IDE_TYPE_OBJECT,
@@ -115,6 +117,26 @@ ide_ctags_completion_provider_class_init (IdeCtagsCompletionProviderClass *klass
 
   object_class->dispose = ide_ctags_completion_provider_dispose;
   object_class->finalize = ide_ctags_completion_provider_finalize;
+
+  reserved = g_hash_table_new (g_str_hash, g_str_equal);
+  g_hash_table_insert (reserved, "break", NULL);
+  g_hash_table_insert (reserved, "continue", NULL);
+  g_hash_table_insert (reserved, "default", NULL);
+  g_hash_table_insert (reserved, "do", NULL);
+  g_hash_table_insert (reserved, "elif", NULL);
+  g_hash_table_insert (reserved, "else", NULL);
+  g_hash_table_insert (reserved, "enum", NULL);
+  g_hash_table_insert (reserved, "for", NULL);
+  g_hash_table_insert (reserved, "goto", NULL);
+  g_hash_table_insert (reserved, "if", NULL);
+  g_hash_table_insert (reserved, "pass", NULL);
+  g_hash_table_insert (reserved, "return", NULL);
+  g_hash_table_insert (reserved, "struct", NULL);
+  g_hash_table_insert (reserved, "sizeof", NULL);
+  g_hash_table_insert (reserved, "switch", NULL);
+  g_hash_table_insert (reserved, "typedef", NULL);
+  g_hash_table_insert (reserved, "union", NULL);
+  g_hash_table_insert (reserved, "while", NULL);
 }
 
 static void
@@ -304,12 +326,21 @@ ide_ctags_completion_provider_match (GtkSourceCompletionProvider *provider,
 }
 
 static gboolean
+is_reserved_word (GtkTextBuffer *buffer,
+                  const gchar   *word)
+{
+  /* TODO: Check by language */
+  return g_hash_table_contains (reserved, word);
+}
+
+static gboolean
 ide_ctags_completion_provider_activate_proposal (GtkSourceCompletionProvider *provider,
                                                  GtkSourceCompletionProposal *proposal,
                                                  GtkTextIter                 *iter)
 {
   IdeCtagsCompletionProvider *self = (IdeCtagsCompletionProvider *)provider;
   IdeCtagsCompletionItem *item = (IdeCtagsCompletionItem *)proposal;
+  g_auto(GStrv) contexts = NULL;
   GtkTextBuffer *buffer;
   GtkTextIter begin;
 
@@ -334,6 +365,13 @@ ide_ctags_completion_provider_activate_proposal (GtkSourceCompletionProvider *pr
       g_autofree gchar *current_text = gtk_text_iter_get_slice (&begin, iter);
       g_autofree gchar *proposal_text = gtk_source_completion_proposal_get_text (proposal);
 
+      /*
+       * If this is a keyword for the language, we probably didn't mean
+       * to activate this completion proposal.
+       */
+      if (is_reserved_word (buffer, current_text))
+        return TRUE;
+
       if (g_str_has_prefix (proposal_text, current_text))
         {
           gtk_text_buffer_begin_user_action (buffer);


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