Commits

Alexey Orlov  committed cec7da7

factored project_data into cvs_binding, allowed cvs setting override in the project

  • Participants
  • Parent commits 4b2d598

Comments (0)

Files changed (4)

File cvs_binding.py

 import sublime
+import os
 
-def load_default_diff_settings():
+def default_diff_settings():
     return sublime.load_settings("sublime-diff-default.sublime-settings")
 
-def load_diff_settings(cvs):
+def diff_settings(cvs):
     _settings = sublime.load_settings("sublime-diff-"+cvs+".sublime-settings")
     if not _settings:
-        _settings = load_default_diff_settings()
+        _settings = default_diff_settings()
     return _settings
 
-def setting_value(cvs, name):
-    return load_diff_settings(cvs).get(name, load_default_diff_settings().get(name))
+def query_from_project(name, window, default_val=None):
+    settings_dict = window.project_data()["settings"] if ("settings" in window.project_data()) else {}
+    return settings_dict[name] if (name in settings_dict) else default_val
 
 
-def generate_diff_command_line(cvs, filename=None):
-    return setting_value(cvs, "command") + (" \"" + filename + "\"" if filename else "")
+# cvs and root should be specified in project
+def cvs_name(window):
+    return query_from_project("sublime_diff.cvs", window)
+
+def cvs_root(window):
+    proj_dir = os.path.dirname(window.project_file_name())
+    root_dir = query_from_project("sublime_diff.root", window, proj_dir)
+
+    return root_dir if os.path.isabs(root_dir) else os.path.abspath(os.path.join(proj_dir, root_dir))
+
+
+def setting_value(name, window):
+    cvs = cvs_name(window)
+    val = query_from_project("sublime_diff."+cvs+"."+name, window)
+    return val if val else diff_settings(cvs).get(name, default_diff_settings().get(name))
+
+
+def cvs_path(window, path):
+    return os.path.abspath(os.path.join(cvs_root(window), path))

File diff_region.py

 
 import re
 
-from . import cvs_binding, project_data
+from . import cvs_binding
 
 def generate_hunks(view, start, end, filename, region_map):
-    range_pattern = cvs_binding.setting_value(project_data.cvs_name(view.window()), "re.range_info")
+    range_pattern = cvs_binding.setting_value("re.range_info", view.window())
 
     cur_char = start
     while cur_char < end:
 def generate_region_map(view):
     region_map = {}
 
-    file_src_pattern  = cvs_binding.setting_value(project_data.cvs_name(view.window()), "re.src_file")
-    file_dst_pattern  = cvs_binding.setting_value(project_data.cvs_name(view.window()), "re.dst_file")
-    hunk_line_pattern = cvs_binding.setting_value(project_data.cvs_name(view.window()), "re.hunk_line")
+    file_src_pattern  = cvs_binding.setting_value("re.src_file", view.window())
+    file_dst_pattern  = cvs_binding.setting_value("re.dst_file", view.window())
+    hunk_line_pattern = cvs_binding.setting_value("re.hunk_line", view.window())
 
     cur_char = 0
     while 1:
 def calculate_dst_file_pos(view, diff_region, dst_line, cursor_region):
     diff_text = view.substr(sublime.Region(diff_region.a, cursor_region.a))
 
-    del_pattern = cvs_binding.setting_value(project_data.cvs_name(view.window()), "re.removed_line")
+    del_pattern = cvs_binding.setting_value("re.removed_line", view.window())
 
     offset=0
     for line in diff_text.splitlines():

File project_data.py

-import sublime
-
-import os
-
-def query_from_project(name, window, default_val=None):
-    settings_dict = window.project_data()["settings"] if ("settings" in window.project_data()) else {}
-    return settings_dict[name] if (name in settings_dict) else default_val
-
-def cvs_name(window):
-    return query_from_project("sublime_diff.cvs", window)
-
-def cvs_root(window):
-    proj_dir = os.path.dirname(window.project_file_name())
-    root_dir = query_from_project("sublime_diff.root", window, proj_dir)
-
-    return root_dir if os.path.isabs(root_dir) else os.path.abspath(os.path.join(proj_dir, root_dir))
-
-def cvs_path(window, path):
-    return os.path.abspath(os.path.join(cvs_root(window), path))

File sublime_diff.py

 import os
 import subprocess
 
-from . import cvs_binding, project_data, diff_region
+from . import cvs_binding, diff_region
 
 def log_error(message):
     status_msg_end = message.find("\n")
         return False
 
     path = view.file_name()
-    root = project_data.cvs_root(view.window())
+    root = cvs_binding.cvs_root(view.window())
 
     return os.path.commonprefix([root, path]) == root if os.path.isabs(path) else os.path.exists(os.path.join(root, path))
 
 def run_diff_cmd(window, filename):
     # some cvs accept only relative path
     # and we run in correct cwd anyway
-    path = os.path.relpath(filename, project_data.cvs_root(window)) if filename and os.path.isabs(filename) else filename
+    path = os.path.relpath(filename, cvs_binding.cvs_root(window)) if filename and os.path.isabs(filename) else filename
 
-    cmd = cvs_binding.generate_diff_command_line(project_data.cvs_name(window), path)
-    cwd = project_data.cvs_root(window) + "/"
+    cmd = cvs_binding.setting_value("command", window) + (" \"" + path + "\"" if path else "")
+    cwd = cvs_binding.cvs_root(window) + "/"
 
     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, shell=True)
     result, err = p.communicate()
     def run(self):
         target_view = self.window.active_view()
 
-        if project_data.cvs_name(self.window) is None:
+        if cvs_binding.cvs_name(self.window) is None:
             log_error("project do not contain cvs info")
         elif not target_view:
             log_error("no active view")
     def run(self):
         target_view = self.window.active_view()
 
-        if project_data.cvs_name(self.window) is None:
+        if cvs_binding.cvs_name(self.window) is None:
             log_error("project do not contain cvs info")
         elif target_view and target_view.settings().get("sublime_diff.view"):
             open_diff_view(self.window, None, target_view)
                     row, col = diff_region.calculate_dst_file_pos(self.view, r, fileline, cursor)
 
                     if not os.path.isabs(filename):
-                        filename = project_data.cvs_path(self.view.window(), filename)
+                        filename = cvs_binding.cvs_path(self.view.window(), filename)
 
                     self.view.window().open_file(filename+":"+str(row)+":"+str(col), sublime.ENCODED_POSITION)
         else: