[gnome-builder/wip/libide] libide: allow setting content type from buffer contents



commit a786ea9be6960e8f042bca3b6440ef1a717e28e8
Author: Christian Hergert <christian hergert me>
Date:   Sat Mar 14 15:21:45 2015 -0700

    libide: allow setting content type from buffer contents

 libide/ide-file.c     |   31 ++++++++++++++++++++++++++++---
 libide/ide-internal.h |    4 +++-
 2 files changed, 31 insertions(+), 4 deletions(-)
---
diff --git a/libide/ide-file.c b/libide/ide-file.c
index 7b47b4e..c9df629 100644
--- a/libide/ide-file.c
+++ b/libide/ide-file.c
@@ -27,6 +27,7 @@ struct _IdeFile
 {
   IdeObject      parent_instance;
 
+  gchar         *content_type;
   GFile         *file;
   IdeLanguage   *language;
   gchar         *path;
@@ -46,6 +47,22 @@ G_DEFINE_TYPE (IdeFile, ide_file, IDE_TYPE_OBJECT)
 
 static GParamSpec *gParamSpecs [LAST_PROP];
 
+void
+_ide_file_set_content_type (IdeFile     *self,
+                            const gchar *content_type)
+{
+  g_assert (IDE_IS_FILE (self));
+  g_assert (content_type);
+
+  if (0 != g_strcmp0 (self->content_type, content_type))
+    {
+      g_clear_pointer (&self->content_type, g_free);
+      g_clear_object (&self->language);
+      self->content_type = g_strdup (content_type);
+      g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_LANGUAGE]);
+    }
+}
+
 static const gchar *
 ide_file_remap_language (const gchar *lang_id)
 {
@@ -84,6 +101,7 @@ static void
 ide_file_create_language (IdeFile *self)
 {
   g_assert (IDE_IS_FILE (self));
+  g_assert (self->path);
 
   if (g_once_init_enter (&self->language))
     {
@@ -92,16 +110,22 @@ ide_file_create_language (IdeFile *self)
       IdeLanguage *language = NULL;
       const gchar *lang_id = NULL;
       g_autofree gchar *content_type = NULL;
-      g_autofree gchar *filename = NULL;
+      const gchar *filename;
       IdeContext *context;
-      gboolean uncertain = TRUE;
+      gboolean uncertain = FALSE;
 
       context = ide_object_get_context (IDE_OBJECT (self));
       filename = g_file_get_basename (self->file);
-      content_type = g_content_type_guess (filename, NULL, 0, &uncertain);
+
+      if (self->content_type)
+        content_type = g_strdup (self->content_type);
+      else
+        content_type = g_content_type_guess (filename, NULL, 0, &uncertain);
 
       if (uncertain)
         g_clear_pointer (&content_type, g_free);
+      else if (self->content_type == NULL)
+        self->content_type = g_strdup (content_type);
 
       manager = gtk_source_language_manager_get_default ();
       srclang = gtk_source_language_manager_guess_language (manager, filename, content_type);
@@ -313,6 +337,7 @@ ide_file_finalize (GObject *object)
   g_clear_object (&self->source_file);
   g_clear_object (&self->language);
   g_clear_pointer (&self->path, g_free);
+  g_clear_pointer (&self->content_type, g_free);
 
   G_OBJECT_CLASS (ide_file_parent_class)->finalize (object);
 }
diff --git a/libide/ide-internal.h b/libide/ide-internal.h
index d5333a5..2c36419 100644
--- a/libide/ide-internal.h
+++ b/libide/ide-internal.h
@@ -65,7 +65,9 @@ void                _ide_diagnostician_add_provider    (IdeDiagnostician      *s
 void                _ide_diagnostician_remove_provider (IdeDiagnostician      *self,
                                                         IdeDiagnosticProvider *provider);
 IdeDiagnostics     *_ide_diagnostics_new               (GPtrArray             *ar);
-GtkSourceFile      *_ide_file_get_source_file          (IdeFile               *file);
+GtkSourceFile      *_ide_file_set_content_type         (IdeFile               *self,
+                                                        const gchar           *content_type);
+GtkSourceFile      *_ide_file_get_source_file          (IdeFile               *self);
 void                _ide_project_set_name              (IdeProject            *project,
                                                         const gchar           *name);
 void                _ide_search_context_add_provider   (IdeSearchContext      *context,


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