[gnome-text-editor] document: always prefer Python 3 to Python 2



commit c318300d020e2d0bdab023d3017d761e1c6221b1
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 5 17:03:00 2022 -0800

    document: always prefer Python 3 to Python 2
    
    If we guess text/x-python, just use Python 3 language instead.

 src/editor-document.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
---
diff --git a/src/editor-document.c b/src/editor-document.c
index 9b0a2f0..fd29b82 100644
--- a/src/editor-document.c
+++ b/src/editor-document.c
@@ -98,6 +98,26 @@ enum {
 static GParamSpec *properties [N_PROPS];
 static GSettings *shared_settings;
 
+static GtkSourceLanguage *
+guess_language (GtkSourceLanguageManager *manager,
+                const char               *filename,
+                const char               *content_type)
+{
+  /* Apply content-type overrides */
+  if (filename != NULL && content_type != NULL)
+    {
+      /* shared-mime-info calls it text/x-python rather than text/x-python3
+       * if the shebang is something like "env python" vs "env python3". We
+       * always want to default to Python 3.
+       */
+      if (g_str_has_suffix (filename, ".py") &&
+          g_str_equal (content_type, "text/x-python"))
+        content_type = "text/x-python3";
+    }
+
+  return gtk_source_language_manager_guess_language (manager, filename, content_type);
+}
+
 static void
 load_free (Load *load)
 {
@@ -205,7 +225,7 @@ editor_document_guess_content_type (EditorDocument *self)
 
   content_type = g_content_type_guess (filename, (const guchar *)content, strlen (content), &uncertain);
   manager = gtk_source_language_manager_get_default ();
-  language = gtk_source_language_manager_guess_language (manager, filename, content_type);
+  language = guess_language (manager, filename, content_type);
 
   if (language)
     gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (self), language);
@@ -1110,7 +1130,7 @@ editor_document_query_info_cb (GObject      *object,
 
   editor_document_set_readonly (self, readonly);
 
-  language = gtk_source_language_manager_guess_language (lm, filename, content_type);
+  language = guess_language (lm, filename, content_type);
   gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (self), language);
   gtk_source_buffer_set_highlight_syntax (GTK_SOURCE_BUFFER (self), language != NULL);
 
@@ -1549,7 +1569,7 @@ editor_document_guess_language_query_cb (GObject      *object,
   content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
   filename = g_file_get_basename (file);
   lm = gtk_source_language_manager_get_default ();
-  language = gtk_source_language_manager_guess_language (lm, filename, content_type);
+  language = guess_language (lm, filename, content_type);
   self = g_task_get_source_object (task);
 
   g_assert (EDITOR_IS_DOCUMENT (self));


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