[gtksourceview/wip/update-test-widget: 1/2] test-widget: improve code for opening a file



commit 37a2320cdef705b216f06d315a93a0bf752fdcd4
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Sep 1 17:11:20 2013 +0200

    test-widget: improve code for opening a file

 tests/test-widget.c |  149 +++++++++++++++++++++++++++++----------------------
 1 files changed, 84 insertions(+), 65 deletions(-)
---
diff --git a/tests/test-widget.c b/tests/test-widget.c
index 130e800..48115d3 100644
--- a/tests/test-widget.c
+++ b/tests/test-widget.c
@@ -281,34 +281,35 @@ gtk_source_buffer_load_file (GtkSourceBuffer *buffer,
 static void
 remove_all_marks (GtkSourceBuffer *buffer)
 {
-       GtkTextIter s, e;
+       GtkTextIter start;
+       GtkTextIter end;
 
-       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &s, &e);
+       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
 
-       gtk_source_buffer_remove_source_marks (buffer, &s, &e, NULL);
+       gtk_source_buffer_remove_source_marks (buffer, &start, &end, NULL);
 }
 
 static GtkSourceLanguage *
-get_language_for_file (GtkTextBuffer *buffer, const gchar *filename)
+get_language_for_file (GtkTextBuffer *buffer,
+                      const gchar   *filename)
 {
        GtkSourceLanguageManager *manager;
        GtkSourceLanguage *language;
-       GtkTextIter start, end;
+       GtkTextIter start;
+       GtkTextIter end;
        gchar *text;
        gchar *content_type;
        gboolean result_uncertain;
 
        gtk_text_buffer_get_start_iter (buffer, &start);
-       if (gtk_text_buffer_get_char_count (buffer) < 1024)
-               gtk_text_buffer_get_end_iter (buffer, &end);
-       else
-               gtk_text_buffer_get_iter_at_offset (buffer, &end, 1024);
+       gtk_text_buffer_get_iter_at_offset (buffer, &end, 1024);
        text = gtk_text_buffer_get_slice (buffer, &start, &end, TRUE);
 
        content_type = g_content_type_guess (filename,
                                             (guchar*) text,
                                             strlen (text),
                                             &result_uncertain);
+
        if (result_uncertain)
        {
                g_free (content_type);
@@ -321,9 +322,9 @@ get_language_for_file (GtkTextBuffer *buffer, const gchar *filename)
                                                               content_type);
 
        g_message ("Detected '%s' mime type for file %s, chose language %s",
-                  content_type ? content_type : "(null)",
+                  content_type != NULL ? content_type : "(null)",
                   filename,
-                  language ? gtk_source_language_get_id (language) : "(none)");
+                  language != NULL ? gtk_source_language_get_id (language) : "(none)");
 
        g_free (content_type);
        g_free (text);
@@ -339,10 +340,12 @@ get_language_by_id (const gchar *id)
 }
 
 static GtkSourceLanguage *
-get_language (GtkTextBuffer *buffer, const gchar *filename)
+get_language (GtkTextBuffer *buffer,
+             const gchar   *filename)
 {
        GtkSourceLanguage *language = NULL;
-       GtkTextIter start, end;
+       GtkTextIter start;
+       GtkTextIter end;
        gchar *text;
        gchar *lang_string;
 
@@ -351,8 +354,10 @@ get_language (GtkTextBuffer *buffer, const gchar *filename)
        gtk_text_iter_forward_line (&end);
 
 #define LANG_STRING "gtk-source-lang:"
+
        text = gtk_text_iter_get_slice (&start, &end);
        lang_string = strstr (text, LANG_STRING);
+
        if (lang_string != NULL)
        {
                gchar **tokens;
@@ -363,84 +368,96 @@ get_language (GtkTextBuffer *buffer, const gchar *filename)
                tokens = g_strsplit_set (lang_string, " \t\n", 2);
 
                if (tokens != NULL && tokens[0] != NULL)
+               {
                        language = get_language_by_id (tokens[0]);
+               }
 
                g_strfreev (tokens);
        }
 
-       if (!language)
+       if (language == NULL)
+       {
                language = get_language_for_file (buffer, filename);
+       }
 
        g_free (text);
        return language;
 }
 
-static gboolean
-open_file (GtkSourceBuffer *buffer, const gchar *filename)
+static void
+print_language_style_ids (GtkSourceLanguage *language)
 {
-       GtkSourceLanguage *language = NULL;
-       gchar *freeme = NULL;
-       gboolean success = FALSE;
+       gchar **styles;
 
-       if (!g_path_is_absolute (filename))
+       g_assert (language != NULL);
+
+       styles = gtk_source_language_get_style_ids (language);
+
+       if (styles == NULL)
        {
-               gchar *curdir = g_get_current_dir ();
-               freeme = g_build_filename (curdir, filename, NULL);
-               filename = freeme;
-               g_free (curdir);
+               g_print ("No styles in language '%s'\n",
+                        gtk_source_language_get_name (language));
        }
+       else
+       {
+               gchar **ids;
+               g_print ("Styles in language '%s':\n",
+                        gtk_source_language_get_name (language));
 
-       remove_all_marks (buffer);
-
-       success = gtk_source_buffer_load_file (buffer, filename);
+               for (ids = styles; *ids != NULL; ids++)
+               {
+                       const gchar *name = gtk_source_language_get_style_name (language, *ids);
 
-       if (!success)
-               goto out;
+                       g_print ("- %s (name: '%s')\n", *ids, name);
+               }
 
-       language = get_language (GTK_TEXT_BUFFER (buffer), filename);
+               g_strfreev (styles);
+       }
 
-       if (language == NULL)
-               g_print ("No language found for file `%s'\n", filename);
+       g_print ("\n");
+}
 
-       gtk_source_buffer_set_language (buffer, language);
-       g_object_set_data_full (G_OBJECT (buffer),
-                               "filename", g_strdup (filename),
-                               (GDestroyNotify) g_free);
+static void
+open_file (GtkSourceBuffer *buffer,
+          const gchar     *filename)
+{
+       GtkSourceLanguage *language = NULL;
+       gchar *absolute_filename;
 
-       if (language != NULL)
+       if (g_path_is_absolute (filename))
        {
-               gchar **styles;
-
-               styles = gtk_source_language_get_style_ids (language);
-
-               if (styles == NULL)
-                       g_print ("No styles in language '%s'\n", gtk_source_language_get_name (language));
-               else
-               {
-                       gchar **ids;
-                       g_print ("Styles in in language '%s':\n", gtk_source_language_get_name (language));
-
-                       ids = styles;
-
-                       while (*ids != NULL)
-                       {
-                               const gchar *name;
+               absolute_filename = g_strdup (filename);
+       }
+       else
+       {
+               gchar *curdir = g_get_current_dir ();
+               absolute_filename = g_build_filename (curdir, filename, NULL);
+               g_free (curdir);
+       }
 
-                               name = gtk_source_language_get_style_name (language, *ids);
+       remove_all_marks (buffer);
 
-                               g_print ("- %s (name: '%s')\n", *ids, name);
+       if (!gtk_source_buffer_load_file (buffer, absolute_filename))
+       {
+               g_free (absolute_filename);
+               return;
+       }
 
-                               ++ids;
-                       }
+       g_object_set_data_full (G_OBJECT (buffer),
+                               "filename", absolute_filename,
+                               (GDestroyNotify) g_free);
 
-                       g_strfreev (styles);
-               }
+       language = get_language (GTK_TEXT_BUFFER (buffer), absolute_filename);
+       gtk_source_buffer_set_language (buffer, language);
 
-               g_print("\n");
+       if (language != NULL)
+       {
+               print_language_style_ids (language);
+       }
+       else
+       {
+               g_print ("No language found for file '%s'\n", absolute_filename);
        }
-out:
-       g_free (freeme);
-       return success;
 }
 
 
@@ -674,11 +691,15 @@ open_file_cb (GtkAction *action, gpointer user_data)
                                               NULL);
 
        if (last_dir == NULL)
+       {
                last_dir = g_strdup (TOP_SRCDIR "/gtksourceview");
+       }
 
        if (last_dir != NULL && g_path_is_absolute (last_dir))
+       {
                gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
                                                     last_dir);
+       }
 
        response = gtk_dialog_run (GTK_DIALOG (chooser));
 
@@ -737,8 +758,6 @@ paginate (GtkPrintOperation        *operation,
                n_pages = gtk_source_print_compositor_get_n_pages (compositor);
                gtk_print_operation_set_n_pages (operation, n_pages);
 
-
-
                return TRUE;
        }
 


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