[gnome-builder/wip/slaf/beautifier: 7/15] beautifier plugin: add clang-format support



commit 3a62c073f37cea37c9377e9f352a26359021e758
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Thu Dec 1 22:55:14 2016 +0100

    beautifier plugin: add clang-format support

 .beautifier/c/clang-format.cfg             |   60 ++++++++++++++++++++
 .beautifier/c/config.ini                   |    5 ++
 plugins/beautifier/gb-beautifier-config.c  |    7 +--
 plugins/beautifier/gb-beautifier-config.h  |    1 +
 plugins/beautifier/gb-beautifier-process.c |   82 +++++++++++++++++++++++++++-
 5 files changed, 150 insertions(+), 5 deletions(-)
---
diff --git a/.beautifier/c/clang-format.cfg b/.beautifier/c/clang-format.cfg
new file mode 100644
index 0000000..1ad20cc
--- /dev/null
+++ b/.beautifier/c/clang-format.cfg
@@ -0,0 +1,60 @@
+---
+# C format
+Language: Cpp
+
+BasedOnStyle: WebKit
+
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlinesLeft: false
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterReturnType: AllDefinitions
+AlwaysBreakBeforeMultilineStrings: true
+BinPackArguments: false
+BinPackParameters: false
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: GNU
+BreakBeforeTernaryOperators: false
+ColumnLimit: 100
+CommentPragmas:  '^ IWYU pragma:'
+ContinuationIndentWidth: 2
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+IndentCaseLabels: false
+IndentWidth: 2
+IndentWrappedFunctionNames: false
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SpaceAfterCStyleCast: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: Always
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp11
+TabWidth: 4
+UseTab: Never
+...
+
diff --git a/.beautifier/c/config.ini b/.beautifier/c/config.ini
index ed2abca..22f9bf3 100644
--- a/.beautifier/c/config.ini
+++ b/.beautifier/c/config.ini
@@ -9,3 +9,8 @@ default = gnome-builder.cfg
 
 command = uncrustify
 name = GNOME Builder project
+
+[clang-format.cfg]
+
+command = clang-format
+name = GNOME Builder project clang-format
diff --git a/plugins/beautifier/gb-beautifier-config.c b/plugins/beautifier/gb-beautifier-config.c
index c8f627d..d2c5d48 100644
--- a/plugins/beautifier/gb-beautifier-config.c
+++ b/plugins/beautifier/gb-beautifier-config.c
@@ -155,10 +155,9 @@ add_entries_from_config_ini_file (GbBeautifierWorkbenchAddin *self,
               g_autofree gchar *file_path = NULL;
 
               if (0 == g_strcmp0 (command, "uncrustify"))
-                {
-                  g_clear_pointer (&command, g_free);
-                  entry.command = GB_BEAUTIFIER_CONFIG_COMMAND_UNCRUSTIFY;
-                }
+                entry.command = GB_BEAUTIFIER_CONFIG_COMMAND_UNCRUSTIFY;
+              else if (0 == g_strcmp0 (command, "clang-format"))
+                entry.command = GB_BEAUTIFIER_CONFIG_COMMAND_CLANG_FORMAT;
               else
                 goto fail;
 
diff --git a/plugins/beautifier/gb-beautifier-config.h b/plugins/beautifier/gb-beautifier-config.h
index 12b7a99..94d8cfe 100644
--- a/plugins/beautifier/gb-beautifier-config.h
+++ b/plugins/beautifier/gb-beautifier-config.h
@@ -27,6 +27,7 @@ G_BEGIN_DECLS
 
 typedef enum
 {
+  GB_BEAUTIFIER_CONFIG_COMMAND_CLANG_FORMAT,
   GB_BEAUTIFIER_CONFIG_COMMAND_UNCRUSTIFY,
 } GbBeautifierConfigCommand;
 
diff --git a/plugins/beautifier/gb-beautifier-process.c b/plugins/beautifier/gb-beautifier-process.c
index 76233ab..2c7dace 100644
--- a/plugins/beautifier/gb-beautifier-process.c
+++ b/plugins/beautifier/gb-beautifier-process.c
@@ -34,6 +34,9 @@ typedef struct
   GbBeautifierConfigCommand   command;
   GFile                      *src_file;
   GFile                      *config_file;
+  GFile                      *tmp_workdir_file;
+  GFile                      *tmp_src_file;
+  GFile                      *tmp_config_file;
   gchar                      *lang_id;
   gchar                      *text;
 } ProcessState;
@@ -52,8 +55,16 @@ process_state_free (gpointer data)
 
   gb_beautifier_helper_remove_tmp_file (state->self, state->src_file);
   g_clear_object (&state->src_file);
-
   g_clear_object (&state->config_file);
+
+  g_file_delete (state->tmp_config_file, NULL, NULL);
+  g_file_delete (state->tmp_src_file, NULL, NULL);
+  g_file_delete (state->tmp_workdir_file, NULL, NULL);
+
+  g_clear_object (&state->tmp_workdir_file);
+  g_clear_object (&state->tmp_config_file);
+  g_clear_object (&state->tmp_src_file);
+
   g_free (state->lang_id);
   g_free (state->text);
 
@@ -98,6 +109,73 @@ gb_beautifier_process_create_for_uncrustify (GbBeautifierWorkbenchAddin *self,
   return subprocess;
 }
 
+static GSubprocess *
+gb_beautifier_process_create_for_clang_format (GbBeautifierWorkbenchAddin *self,
+                                               ProcessState               *state,
+                                               GError                     *error)
+{
+  g_autoptr(GSubprocessLauncher) launcher = NULL;
+  GSubprocess *subprocess = NULL;
+  GPtrArray *args;
+  gchar *config_path;
+  gchar *src_path;
+  g_autofree gchar *tmp_workdir = NULL;
+  g_autofree gchar *tmp_config_path = NULL;
+  g_autofree gchar *tmp_src_path = NULL;
+
+  g_assert (GB_IS_BEAUTIFIER_WORKBENCH_ADDIN (self));
+  g_assert (state != NULL);
+
+  config_path = g_file_get_path (state->config_file);
+  src_path = g_file_get_path (state->src_file);
+
+  g_assert (!ide_str_empty0 (config_path));
+  g_assert (!ide_str_empty0 (src_path));
+  g_assert (!ide_str_empty0 (state->lang_id));
+
+  if (NULL == (tmp_workdir = g_dir_make_tmp ("gnome-builder-beautify-XXXXXX",
+                                             &error)))
+    return NULL;
+
+  state->tmp_workdir_file = g_file_new_for_path (tmp_workdir);
+  tmp_config_path = g_build_filename (tmp_workdir,
+                                      ".clang-format",
+                                      NULL);
+  state->tmp_config_file = g_file_new_for_path (tmp_config_path);
+  if (!g_file_copy (state->config_file,
+                    state->tmp_config_file,
+                    G_FILE_COPY_OVERWRITE,
+                    NULL, NULL, NULL,
+                    &error))
+    return NULL;
+
+  tmp_src_path = g_build_filename (tmp_workdir,
+                                   "src_file",
+                                   NULL);
+  state->tmp_src_file = g_file_new_for_path (tmp_src_path);
+  if (!g_file_copy (state->src_file,
+                    state->tmp_src_file,
+                    G_FILE_COPY_OVERWRITE,
+                    NULL, NULL, NULL,
+                    &error))
+    return NULL;
+
+  args = g_ptr_array_new ();
+  g_ptr_array_add (args, "clang-format");
+  g_ptr_array_add (args, "-style=file");
+  g_ptr_array_add (args, tmp_src_path);
+  g_ptr_array_add (args, NULL);
+
+  launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_PIPE);
+  g_subprocess_launcher_set_cwd (launcher, tmp_workdir);
+  subprocess = g_subprocess_launcher_spawnv (launcher,
+                                             (const gchar * const *)args->pdata,
+                                             &error);
+
+  g_ptr_array_free (args, TRUE);
+  return subprocess;
+}
+
 static void
 process_communicate_utf8_cb (GObject      *object,
                              GAsyncResult *result,
@@ -175,6 +253,8 @@ create_tmp_file_cb (GObject      *object,
 
   if (state->command == GB_BEAUTIFIER_CONFIG_COMMAND_UNCRUSTIFY)
     process = gb_beautifier_process_create_for_uncrustify (self, state, error);
+  else if (state->command == GB_BEAUTIFIER_CONFIG_COMMAND_CLANG_FORMAT)
+    process = gb_beautifier_process_create_for_clang_format (self, state, error);
   else
     g_assert_not_reached ();
 


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