[gnome-builder] formatter: reenable source formatting for C code.
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] formatter: reenable source formatting for C code.
- Date: Mon, 8 Sep 2014 20:33:03 +0000 (UTC)
commit 0cbe5b79107f61481825ab2fb5d6343d804bce2a
Author: Christian Hergert <christian hergert me>
Date: Mon Sep 8 13:32:58 2014 -0700
formatter: reenable source formatting for C code.
src/editor/gb-editor-tab.c | 10 +++-
src/editor/gb-source-formatter.c | 80 +++++++++++++++++++++++++++++++++++---
2 files changed, 81 insertions(+), 9 deletions(-)
---
diff --git a/src/editor/gb-editor-tab.c b/src/editor/gb-editor-tab.c
index 1227275..cbd79fa 100644
--- a/src/editor/gb-editor-tab.c
+++ b/src/editor/gb-editor-tab.c
@@ -206,6 +206,7 @@ gb_editor_tab_reformat (GbEditorTab *tab)
{
GbEditorTabPrivate *priv;
GbSourceFormatter *formatter;
+ GtkSourceLanguage *language;
GtkTextBuffer *buffer;
GtkTextIter begin;
GtkTextIter end;
@@ -236,7 +237,8 @@ gb_editor_tab_reformat (GbEditorTab *tab)
char_offset = gtk_text_iter_get_line_offset (&iter);
line_number = gtk_text_iter_get_line (&iter);
- formatter = gb_source_formatter_new_from_language (NULL);
+ language = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (buffer));
+ formatter = gb_source_formatter_new_from_language (language);
if (!gb_source_formatter_format (formatter,
input,
@@ -247,7 +249,7 @@ gb_editor_tab_reformat (GbEditorTab *tab)
{
g_warning ("%s", error->message);
g_clear_error (&error);
- goto cleanup;
+ GOTO (cleanup);
}
gtk_text_buffer_begin_user_action (buffer);
@@ -255,7 +257,9 @@ gb_editor_tab_reformat (GbEditorTab *tab)
gb_source_view_clear_snippets (priv->source_view);
/* TODO: Keep the cursor on same CXCursor from Clang instead of the
- * same character offset within the buffer.
+ * same character offset within the buffer. We probably want
+ * to defer this to the formatter API since it will be language
+ * specific.
*/
gtk_text_buffer_set_text (buffer, output, -1);
diff --git a/src/editor/gb-source-formatter.c b/src/editor/gb-source-formatter.c
index 1b1fc2c..554e756 100644
--- a/src/editor/gb-source-formatter.c
+++ b/src/editor/gb-source-formatter.c
@@ -31,6 +31,7 @@ struct _GbSourceFormatterPrivate
enum {
PROP_0,
+ PROP_CAN_FORMAT,
PROP_LANGUAGE,
LAST_PROP
};
@@ -49,6 +50,46 @@ gb_source_formatter_new_from_language (GtkSourceLanguage *language)
NULL);
}
+static gchar *
+get_config_path (const gchar *lang_id)
+{
+ gchar *path;
+ gchar *name;
+
+ name = g_strdup_printf ("uncrustify.%s.cfg", lang_id);
+ path = g_build_filename (g_get_user_config_dir (),
+ "gnome-builder", "uncrustify", name,
+ NULL);
+ g_free (name);
+
+ return path;
+}
+
+gboolean
+gb_source_formatter_get_can_format (GbSourceFormatter *formatter)
+{
+ GtkSourceLanguage *language;
+ const gchar *lang_id;
+ gboolean ret;
+ gchar *path;
+
+ g_return_val_if_fail (GB_IS_SOURCE_FORMATTER (formatter), FALSE);
+
+ language = formatter->priv->language;
+
+ if (!language)
+ return FALSE;
+
+ lang_id = gtk_source_language_get_id (language);
+ path = get_config_path (lang_id);
+
+ ret = g_file_test (path, G_FILE_TEST_IS_REGULAR);
+
+ g_free (path);
+
+ return ret;
+}
+
gboolean
gb_source_formatter_format (GbSourceFormatter *formatter,
const gchar *input,
@@ -57,17 +98,28 @@ gb_source_formatter_format (GbSourceFormatter *formatter,
gchar **output,
GError **error)
{
+ GbSourceFormatterPrivate *priv;
GSubprocessFlags flags;
GSubprocess *proc;
gboolean ret = FALSE;
- char *stderr_buf = NULL;
+ gchar *stderr_buf = NULL;
+ gchar *config_path;
g_return_val_if_fail (GB_IS_SOURCE_FORMATTER (formatter), FALSE);
g_return_val_if_fail (input, FALSE);
- /*
- * TODO: Only format C-like code for now.
- */
+ priv = formatter->priv;
+
+ if (!gb_source_formatter_get_can_format (formatter))
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "Failed to locate uncrustify configuration.");
+ return FALSE;
+ }
+
+ config_path = get_config_path (gtk_source_language_get_id (priv->language));
flags = (G_SUBPROCESS_FLAGS_STDIN_PIPE |
G_SUBPROCESS_FLAGS_STDOUT_PIPE |
@@ -77,7 +129,7 @@ gb_source_formatter_format (GbSourceFormatter *formatter,
error,
"uncrustify",
"-c",
- "clutter-uncrustify.cfg",
+ config_path,
is_fragment ? "--frag" : NULL,
NULL);
@@ -99,6 +151,7 @@ gb_source_formatter_format (GbSourceFormatter *formatter,
finish:
g_clear_object (&proc);
g_free (stderr_buf);
+ g_free (config_path);
return ret;
}
@@ -130,6 +183,7 @@ gb_source_formatter_set_language (GbSourceFormatter *formatter,
priv->language = language;
g_object_notify_by_pspec (G_OBJECT (formatter), gParamSpecs[PROP_LANGUAGE]);
+ g_object_notify_by_pspec (G_OBJECT (formatter), gParamSpecs[PROP_CAN_FORMAT]);
}
static void
@@ -226,8 +280,13 @@ gb_source_formatter_get_property (GObject *object,
switch (prop_id)
{
+ case PROP_CAN_FORMAT:
+ g_value_set_boolean (value,
+ gb_source_formatter_get_can_format (formatter));
+ break;
case PROP_LANGUAGE:
- g_value_set_object (value, gb_source_formatter_get_language (formatter));
+ g_value_set_object (value,
+ gb_source_formatter_get_language (formatter));
break;
default:
@@ -264,6 +323,15 @@ gb_source_formatter_class_init (GbSourceFormatterClass *klass)
object_class->get_property = gb_source_formatter_get_property;
object_class->set_property = gb_source_formatter_set_property;
+ gParamSpecs [PROP_CAN_FORMAT] =
+ g_param_spec_boolean ("can-format",
+ _("Can Format"),
+ _("If the source language can be formatted."),
+ FALSE,
+ (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_CAN_FORMAT,
+ gParamSpecs [PROP_CAN_FORMAT]);
+
gParamSpecs[PROP_LANGUAGE] =
g_param_spec_object ("language",
_ ("Language"),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]