[gnome-builder] snippets: allow multiple expansions within a tab-stop



commit 070e4dcc41c17a8a0243e5f3e1f940a982e98e85
Author: Christian Hergert <christian hergert me>
Date:   Wed Sep 10 03:05:18 2014 -0700

    snippets: allow multiple expansions within a tab-stop
    
    You can now have multiple expansions within a tab-stop for a snippet.
    This can be done using `` to make sub-expressions.
    
    For example:
    
      ${3:Hello `$1|capitalize` `$3|upper`}
    
    The whole region will be selected when moving to that tab-stop.

 src/editor/gb-source-snippet-context.c |   46 ++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/src/editor/gb-source-snippet-context.c b/src/editor/gb-source-snippet-context.c
index 0237e25..c62ca7b 100644
--- a/src/editor/gb-source-snippet-context.c
+++ b/src/editor/gb-source-snippet-context.c
@@ -416,6 +416,29 @@ apply_filters (GString     *str,
   return input;
 }
 
+static gchar *
+scan_forward (const gchar  *input,
+              const gchar **endpos,
+              gunichar      needle)
+{
+  const gchar *begin = input;
+
+  for (; *input; input = g_utf8_next_char (input))
+    {
+      gunichar c = g_utf8_get_char (input);
+
+      if (c == needle)
+        {
+          *endpos = input;
+          return g_strndup (begin, (input - begin));
+        }
+    }
+
+  *endpos = NULL;
+
+  return NULL;
+}
+
 gchar *
 gb_source_snippet_context_expand (GbSourceSnippetContext *context,
                                   const gchar            *input)
@@ -501,6 +524,29 @@ gb_source_snippet_context_expand (GbSourceSnippetContext *context,
         }
       else if (is_dynamic && c == '|')
         return apply_filters (str, input + 1);
+      else if (c == '`')
+        {
+          const gchar *endpos = NULL;
+          gchar *slice;
+
+          slice = scan_forward (input + 1, &endpos, '`');
+
+          if (slice)
+            {
+              gchar *expanded;
+
+              input = endpos;
+
+              expanded = gb_source_snippet_context_expand (context, slice);
+
+              g_string_append (str, expanded);
+
+              g_free (expanded);
+              g_free (slice);
+
+              continue;
+            }
+        }
       else if (c == '\t')
         {
           if (priv->use_spaces)


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