import sublime
import sublime_plugin
from os.path import isfile

from ..core import oa_setting, diff_override
from ..core import PackageListCollectionThread, ContextHelper


###----------------------------------------------------------------------------


class OverrideAuditDiffOverrideCommand(ContextHelper,sublime_plugin.TextCommand):
    """
    Open a diff of a given override. If the current view represents the
    override, it will be saved before the diff happens.
    """
    def run(self, edit, **kwargs):
        target = self.view_target(self.view, **kwargs)
        ctx = self.view_context(target, False, **kwargs)

        # Defer save of the buffer until the command finishes executing
        sView = None
        if oa_setting("save_on_diff") and ctx.is_diff is not None and not ctx.is_diff:
            sView = target

        callback = lambda thread: self._loaded(thread, target.window(), sView,
                                               ctx.package, ctx.override)
        PackageListCollectionThread(target.window(), "Collecting Package List",
                                    callback, name_list=ctx.package).start()

    def _loaded(self, thread, window, save_view, package, override):
        if save_view is not None:
            if save_view.is_dirty() and isfile(save_view.file_name()):
                save_view.run_command("save")

        pkg_list = thread.pkg_list
        diff_override(window, pkg_list[package], override,
                      diff_only=True, force_reuse=True)

    def description(self, **kwargs):
        ctx = self.view_context(None, False, **kwargs)
        if ctx.source == "settings":
            return "OverrideAudit: Diff this Override"

        stub = "OverrideAudit: Diff Override"
        if ctx.has_target():
            return "%s '%s'" % (stub, ctx.override)
        else:
            return stub

    def is_visible(self, **kwargs):
        if self.always_visible(**kwargs):
            return True

        ctx = self.view_context(None, False, **kwargs)
        if ctx.has_target():
            return not ctx.is_diff if ctx.has_diff() else True

        return False

    def is_enabled(self, **kwargs):
        ctx = self.view_context(None, False, **kwargs)
        if self.override_exists(ctx):
            return not ctx.is_diff if ctx.has_diff() else True

        return False


###----------------------------------------------------------------------------

@@ -1964,24 +1964,77 @@
 eturn True%0A%0A
+        view = self.view_target(self.view, **kwargs)%0A
         ctx 
@@ -2045,36 +2045,36 @@
 lf.view_context(
-None
+view
 , False, **kwarg
@@ -2068,32 +2068,33 @@
 alse, **kwargs)%0A
+%0A
         if ctx.h
@@ -2096,32 +2096,73 @@
 ctx.has_target()
+ and not self.override_unknown(view, ctx)
 :%0A            re
@@ -2261,32 +2261,85 @@
 elf, **kwargs):%0A
+        view = self.view_target(self.view, **kwargs)%0A
         ctx = se
@@ -2346,36 +2346,36 @@
 lf.view_context(
-None
+view
 , False, **kwarg
@@ -2384,24 +2384,65 @@
 %0A        if 
+not self.override_unknown(view, ctx) and 
 self.overrid

Exclude unknowns from being diffable in the menu