[meld] Make nullsourceview provide no-op common API, rather than being a flag



commit d70e76e51a3c0731abd72babacaf6a98943649a6
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Fri Aug 14 01:50:19 2009 +1000

    Make nullsourceview provide no-op common API, rather than being a flag
    
    The nullsourceview class acts as a flag telling us whether we have any
    sourceview-compatible package installed. This commit changes the class so
    that it provides no-op methods for all relevant sourceview API, allowing
    us to simplify our treatment of sourceview in FileDiff.

 meld/filediff.py          |   50 ++++++++++++++++++--------------------------
 meld/util/sourceviewer.py |   37 +++++++++++++++++++++++---------
 2 files changed, 47 insertions(+), 40 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index bbe3a56..e194f0f 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -108,12 +108,11 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.map_widgets_into_lists( ["textview", "fileentry", "diffmap", "scrolledwindow", "linkmap", "statusimage", "msgarea_mgr"] )
         self._update_regexes()
         self.warned_bad_comparison = False
-        if srcviewer:
-            for v in self.textview:
-                v.set_buffer(srcviewer.GtkTextBuffer())
-                v.set_show_line_numbers(self.prefs.show_line_numbers)
-                v.set_insert_spaces_instead_of_tabs(self.prefs.spaces_instead_of_tabs)
-                srcviewer.set_tab_width(v, self.prefs.tab_size)
+        for v in self.textview:
+            v.set_buffer(srcviewer.GtkTextBuffer())
+            v.set_show_line_numbers(self.prefs.show_line_numbers)
+            v.set_insert_spaces_instead_of_tabs(self.prefs.spaces_instead_of_tabs)
+            srcviewer.set_tab_width(v, self.prefs.tab_size)
         self.keymask = 0
         self.load_font()
         self.deleted_lines_pending = -1
@@ -323,31 +322,27 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
                 tabs.set_tab(i, pango.TAB_LEFT, i*value*self.pango_char_width)
             for i in range(3):
                 self.textview[i].set_tabs(tabs)
-            if srcviewer:
-                for t in self.textview:
-                    srcviewer.set_tab_width(t, value)
+            for t in self.textview:
+                srcviewer.set_tab_width(t, value)
         elif key == "use_custom_font" or key == "custom_font":
             self.load_font()
         elif key == "show_line_numbers":
-            if srcviewer:
-                for t in self.textview:
-                    t.set_show_line_numbers( value )
+            for t in self.textview:
+                t.set_show_line_numbers( value )
         elif key == "use_syntax_highlighting":
-            if srcviewer:
-                for i in range(self.num_panes):
-                    srcviewer.set_highlighting_enabled_from_file(
-                        self.textbuffer[i],
-                        self.bufferdata[i].filename,
-                        self.prefs.use_syntax_highlighting )
+            for i in range(self.num_panes):
+                srcviewer.set_highlighting_enabled_from_file(
+                    self.textbuffer[i],
+                    self.bufferdata[i].filename,
+                    self.prefs.use_syntax_highlighting )
         elif key == "regexes":
             self._update_regexes()
         elif key == "edit_wrap_lines":
             for t in self.textview:
                 t.set_wrap_mode(self.prefs.edit_wrap_lines)
         elif key == "spaces_instead_of_tabs":
-            if srcviewer:
-                for t in self.textview:
-                    t.set_insert_spaces_instead_of_tabs(value)
+            for t in self.textview:
+                t.set_insert_spaces_instead_of_tabs(value)
 
     def _update_linkmap_buttons(self):
         for l in self.linkmap[:self.num_panes - 1]:
@@ -650,10 +645,9 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.queue_draw()
         self.scheduler.add_task(self._update_highlighting().next)
         self._connect_buffer_handlers()
-        if srcviewer:
-            for i in range(len(files)):
-                if files[i]:
-                    srcviewer.set_highlighting_enabled_from_file(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
+        for i in range(len(files)):
+            if files[i]:
+                srcviewer.set_highlighting_enabled_from_file(self.textbuffer[i], files[i], self.prefs.use_syntax_highlighting)
         yield 0
 
     def on_msgarea_identical_response(self, msgarea, respid):
@@ -855,16 +849,14 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         names = [self._get_pane_label(i) for i in range(2)]
         prefix = os.path.commonprefix( names )
         names = [n[prefix.rfind("/") + 1:] for n in names]
-        if srcviewer:
-            dialog.textview.set_buffer(srcviewer.GtkTextBuffer())
+        dialog.textview.set_buffer(srcviewer.GtkTextBuffer())
         dialog.textview.modify_font(fontdesc)
         buf = dialog.textview.get_buffer()
         lines = []
         for line in difflib.unified_diff(texts[0], texts[1], names[0], names[1]):
             buf.insert( buf.get_end_iter(), line )
             lines.append(line)
-        if srcviewer:
-            srcviewer.set_highlighting_enabled_from_mimetype(buf, "text/x-diff", True)
+        srcviewer.set_highlighting_enabled_from_mimetype(buf, "text/x-diff", True)
         result = dialog.widget.run()
         dialog.widget.destroy()
         if result >= 0:
diff --git a/meld/util/sourceviewer.py b/meld/util/sourceviewer.py
index 20c8181..4b78479 100644
--- a/meld/util/sourceviewer.py
+++ b/meld/util/sourceviewer.py
@@ -20,10 +20,14 @@
 import os
 
 class _srcviewer(object):
+    # Module name to be imported for the sourceviewer class
     srcviewer_module = None
+    # instance of the imported sourceviewer module
+    gsv = None
 
     def __init__(self):
-        self.gsv = __import__(self.srcviewer_module)
+        if self.srcviewer_module is not None:
+            self.gsv = __import__(self.srcviewer_module)
         self.glm = None
         self.version_check()
         self.GtkTextView = None
@@ -154,20 +158,31 @@ class gtksourceview24(_gtksourceview2):
     def get_language_from_file(self, filename):
         return self.get_language_manager().guess_language(filename)
 
-class nullsourceview(object):
+class nullsourceview(_srcviewer):
     """Implement the sourceviewer API when no real one is available
     """
 
-    def __init__(self):
-        self.override = {}
+    get_language_from_file = lambda *args: None
+    set_highlight = lambda *args: None
+    set_tab_width = lambda *args: None
+    get_language_from_mime_type = lambda *args: None
+    set_tab_width = lambda *args: None
+
+    def overrides(self):
+        import gobject
+        import gtk
 
-    def __nonzero__(self):
-        # This is only defined so that we can use this module
-        # without needing a separate boolean, like the following:
-        # from sourceviewer import srcviewer
-        # if srcviewer:
-        #     srcviewer.set_tab_width(tab, self.prefs.tab_size)
-        return False
+        class NullTextView(gtk.TextView):
+            set_tab_width = lambda *args: None
+            set_show_line_numbers = lambda *args: None
+            set_insert_spaces_instead_of_tabs = lambda *args: None
+        gobject.type_register(NullTextView)
+
+        self.GtkTextView = NullTextView
+        self.GtkTextBuffer = gtk.TextBuffer
+
+    def version_check(self):
+        pass
 
 def _get_srcviewer():
     for srcv in (gtksourceview24, gtksourceview22, gtksourceview, sourceview):



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