[gnome-builder] auto-indent: format function declaration parameters on the fly.
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] auto-indent: format function declaration parameters on the fly.
- Date: Sat, 20 Sep 2014 22:12:27 +0000 (UTC)
commit 4d523221d67c374b0483764e146b22728440c284
Author: Christian Hergert <christian hergert me>
Date: Sat Sep 20 15:07:51 2014 -0700
auto-indent: format function declaration parameters on the fly.
src/editor/c-parse-helper.c | 42 +++++++++++++
src/editor/c-parse-helper.h | 1 +
src/editor/gb-source-auto-indenter-c.c | 100 ++++++++++++++++++++++++++++---
3 files changed, 133 insertions(+), 10 deletions(-)
---
diff --git a/src/editor/c-parse-helper.c b/src/editor/c-parse-helper.c
index 29b189e..d8a62f7 100644
--- a/src/editor/c-parse-helper.c
+++ b/src/editor/c-parse-helper.c
@@ -43,6 +43,7 @@ parameter_copy (const Parameter *src)
copy->name = g_strdup (src->name);
copy->type = g_strdup (src->type);
copy->ellipsis = src->ellipsis;
+ copy->n_star = src->n_star;
return copy;
}
@@ -98,6 +99,45 @@ parameter_validate (Parameter *param)
return TRUE;
}
+static void
+parameter_compute (Parameter *param)
+{
+ const gchar *tmp;
+ gchar *rev;
+ guint n_star = 0;
+
+ rev = g_utf8_strreverse (param->type, -1);
+
+ for (tmp = rev; tmp; tmp = g_utf8_next_char (tmp))
+ {
+ switch (g_utf8_get_char (tmp))
+ {
+ case ' ':
+ break;
+
+ case '*':
+ n_star++;
+ break;
+
+ default:
+ if (n_star)
+ {
+ gchar *cleaned;
+
+ cleaned = g_strstrip (g_utf8_strreverse (tmp, -1));
+ g_free (param->type);
+ param->type = cleaned;
+ }
+ goto finish;
+ }
+ }
+
+finish:
+ param->n_star = n_star;
+
+ g_free (rev);
+}
+
GSList *
parse_parameters (const gchar *text)
{
@@ -179,6 +219,8 @@ parse_parameters (const gchar *text)
param.name = g_strstrip (g_utf8_strreverse (name_rev, -1));
param.type = g_strstrip (g_utf8_strreverse (name_sep, -1));
+ parameter_compute (¶m);
+
if (parameter_validate (¶m))
{
ret = g_slist_append (ret, parameter_copy (¶m));
diff --git a/src/editor/c-parse-helper.h b/src/editor/c-parse-helper.h
index b7a8dda..418f518 100644
--- a/src/editor/c-parse-helper.h
+++ b/src/editor/c-parse-helper.h
@@ -28,6 +28,7 @@ typedef struct
gchar *type;
gchar *name;
guint ellipsis : 1;
+ guint n_star : 4;
} Parameter;
gboolean parameter_validate (Parameter *param);
diff --git a/src/editor/gb-source-auto-indenter-c.c b/src/editor/gb-source-auto-indenter-c.c
index ee2627a..2dde2bd 100644
--- a/src/editor/gb-source-auto-indenter-c.c
+++ b/src/editor/gb-source-auto-indenter-c.c
@@ -775,6 +775,95 @@ maybe_space_before_paren (GbSourceAutoIndenterC *c,
}
static gchar *
+format_parameter (const Parameter *param,
+ guint max_type,
+ guint max_star)
+{
+ GString *str;
+ guint i;
+
+ if (param->ellipsis)
+ return g_strdup ("...");
+
+ str = g_string_new (param->type);
+
+ for (i = str->len; i < max_type; i++)
+ g_string_append_c (str, ' ');
+
+ g_string_append_c (str, ' ');
+
+ for (i = max_star; i > 0; i--)
+ {
+ if (i <= param->n_star)
+ g_string_append_c (str, '*');
+ else
+ g_string_append_c (str, ' ');
+ }
+
+ g_string_append (str, param->name);
+
+ return g_string_free (str, FALSE);
+}
+
+static gchar *
+format_parameters (GtkTextIter *begin,
+ GSList *params)
+{
+ GtkTextIter line_start;
+ GtkTextIter first_char;
+ GString *str;
+ GSList *iter;
+ gchar *slice;
+ gchar *join_str;
+ guint max_star = 0;
+ guint max_type = 0;
+
+ for (iter = params; iter; iter = iter->next)
+ {
+ Parameter *p = iter->data;
+ max_star = MAX (max_star, p->n_star);
+ max_type = MAX (max_type, strlen (p->type));
+ }
+
+ str = g_string_new (NULL);
+
+ ITER_INIT_LINE_START (&line_start, begin);
+
+ gtk_text_iter_assign (&first_char, begin);
+ backward_to_line_first_char (&first_char);
+
+ slice = gtk_text_iter_get_slice (&line_start, &first_char);
+ str = g_string_new ("\n");
+ g_string_append (str, slice);
+ g_free (slice);
+
+ while (gtk_text_iter_compare (&first_char, begin) < 0)
+ {
+ g_string_append (str, " ");
+ if (!gtk_text_iter_forward_char (&first_char))
+ break;
+ }
+
+ join_str = g_string_free (str, FALSE);
+ str = g_string_new (NULL);
+
+ for (iter = params; iter; iter = iter->next)
+ {
+ gchar *param_str;
+
+ if (iter != params)
+ g_string_append (str, join_str);
+
+ param_str = format_parameter (iter->data, max_type, max_star);
+ g_string_append (str, param_str);
+ }
+
+ g_free (join_str);
+
+ return g_string_free (str, FALSE);
+}
+
+static gchar *
maybe_align_parameters (GbSourceAutoIndenterC *c,
GtkTextIter *begin,
GtkTextIter *end)
@@ -799,16 +888,7 @@ maybe_align_parameters (GbSourceAutoIndenterC *c,
(gtk_text_iter_compare (begin, end) < 0) &&
(text = gtk_text_iter_get_slice (begin, end)) &&
(params = parse_parameters (text)))
- {
- GSList *iter;
-
- for (iter = params; iter; iter = iter->next)
- {
- Parameter *p = iter->data;
-
- g_print (">%s< >%s<\n", p->type, p->name);
- }
- }
+ ret = format_parameters (begin, params);
g_slist_foreach (params, (GFunc)parameter_free, NULL);
g_slist_free (params);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]