[gnome-builder] beautifier-plugin: add support for internal commands



commit ed8624167bac1e2d73f6eda659be766a76ad1967
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Sat Feb 25 23:09:23 2017 +0100

    beautifier-plugin: add support for internal commands
    
    by adding [internal] in front of the command pattern,
    the command is searched in the Builder data dir

 plugins/beautifier/README.md                       |    3 +
 .../beautifier_plugin/internal/align_makefile.py   |   54 ++++++++++++++++++++
 plugins/beautifier/gb-beautifier-config.c          |   31 +++++++++--
 3 files changed, 82 insertions(+), 6 deletions(-)
---
diff --git a/plugins/beautifier/README.md b/plugins/beautifier/README.md
index bb987fd..d3b24cd 100644
--- a/plugins/beautifier/README.md
+++ b/plugins/beautifier/README.md
@@ -106,6 +106,9 @@ or
       @s@ for the selected text put in a file.
       @c@ for the config file define by the config key.
 
+By adding [internal] in front of the command pattern, the command is
+searched in the Builder data dir.
+
 - name = the real name to display in the menu
 
 - config = the config file name (located in the same folder as the config.ini file)
diff --git a/plugins/beautifier/beautifier_plugin/internal/align_makefile.py 
b/plugins/beautifier/beautifier_plugin/internal/align_makefile.py
new file mode 100755
index 0000000..592f14b
--- /dev/null
+++ b/plugins/beautifier/beautifier_plugin/internal/align_makefile.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+
+"""
+For every line in the makefile ending in a backslash, locate the longest
+line and then reformat all lines to match the proper tailing whitespace.
+"""
+
+import os
+import sys
+
+_MAX_LENGTH = 120
+
+filename = sys.argv[1]
+
+def count_prefix_tabs(line):
+    count = 0
+    for ch in line:
+        if ch == '\t':
+            count += 1
+        else:
+            break
+    return count
+
+def count_visual_chars(line):
+    n_tabs = count_prefix_tabs(line)
+    return n_tabs * 8 + len(line.replace('\t',''))
+
+lines = None
+longest = 0
+
+# find the longest line, stripping the prefix \t (which will be
+# rewritten) and the trailing space before the \\.
+with open(filename, 'r') as stream:
+    lines = list(stream.readlines())
+    for line in lines:
+        if line.endswith('\\\n'):
+            line = line[:-2].rstrip()
+            length = count_visual_chars(line)
+            # if the line length is pathalogical, ignore it
+            if length < _MAX_LENGTH:
+                longest = max(longest, length)
+
+# Now print to stdout the file with the line modifications
+    for line in lines:
+        if not line.endswith('\\\n'):
+            print(line, end='')
+            continue
+
+        line = line[:-2].rstrip()
+        short = count_visual_chars(line)
+
+        line = line + (longest - short) * ' ' + ' \\\n'
+        print(line, end='')
+
diff --git a/plugins/beautifier/gb-beautifier-config.c b/plugins/beautifier/gb-beautifier-config.c
index 6cee0ad..cba9d24 100644
--- a/plugins/beautifier/gb-beautifier-config.c
+++ b/plugins/beautifier/gb-beautifier-config.c
@@ -25,6 +25,20 @@
 #include "gb-beautifier-private.h"
 #include "gb-beautifier-config.h"
 
+static const gchar *
+get_datadir ()
+{
+  PeasEngine *engine;
+  PeasPluginInfo *info;
+  const gchar *datadir = NULL;
+
+  engine = peas_engine_get_default ();
+  if (NULL != (info = peas_engine_get_plugin_info (engine, "beautifier_plugin")))
+    datadir = peas_plugin_info_get_data_dir (info);
+
+  return datadir;
+}
+
 static void
 config_entry_clear_func (gpointer data)
 {
@@ -152,6 +166,7 @@ add_entries_from_config_ini_file (GbBeautifierWorkbenchAddin *self,
           g_autofree gchar *config_path = NULL;
           g_autoptr(GFile) config_file = NULL;
           g_auto(GStrv) strv = NULL;
+          const gchar *datadir;
           gint argc;
           gchar *profile;
           gboolean has_command;
@@ -208,7 +223,15 @@ add_entries_from_config_ini_file (GbBeautifierWorkbenchAddin *self,
             }
           else
             {
-              command_pattern = g_key_file_get_string (key_file, profile, "command-pattern", NULL);
+              command = g_key_file_get_string (key_file, profile, "command-pattern", NULL);
+              if (g_str_has_prefix (command, "[internal]"))
+                {
+                  datadir = get_datadir ();
+                  command_pattern = g_build_filename (datadir, "internal", command + 10, NULL);
+                }
+              else
+                command_pattern = g_strdup (command);
+
               if (g_strstr_len (command_pattern, -1, "@c@") == NULL && config_file != NULL)
                 {
                   g_warning ("beautifier plugin: @c@ in \"%s\" command-pattern key but no config file set",
@@ -400,8 +423,6 @@ GArray *
 gb_beautifier_config_get_entries (GbBeautifierWorkbenchAddin *self)
 {
   IdeContext *context;
-  PeasEngine *engine;
-  PeasPluginInfo *info;
   IdeVcs *vcs;
   GArray *entries;
   GArray *map = NULL;
@@ -444,10 +465,8 @@ gb_beautifier_config_get_entries (GbBeautifierWorkbenchAddin *self)
     }
 
   /* System wide config */
-  engine = peas_engine_get_default ();
-  if (NULL != (info = peas_engine_get_plugin_info (engine, "beautifier_plugin")))
+  if (NULL != (datadir = get_datadir ()))
     {
-      datadir = peas_plugin_info_get_data_dir (info);
       configdir = g_build_filename (datadir, "data", NULL);
 
       map = gb_beautifier_config_get_map (self, configdir);


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