From f80a55153ac85cdd08ffd9007a8c4817e713b843 Mon Sep 17 00:00:00 2001 From: Randy Lai Date: Wed, 27 Aug 2014 14:47:06 -0700 Subject: [PATCH] some code refactor to increase readability --- Default.sublime-commands | 2 +- changelist.py | 50 +++++++++++++++++++++++++++++----------- jsonio.py | 32 ------------------------- 3 files changed, 38 insertions(+), 46 deletions(-) delete mode 100644 jsonio.py diff --git a/Default.sublime-commands b/Default.sublime-commands index 0b226c5..ae1f619 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -1,7 +1,7 @@ [ { "caption": "Change List: Clean", - "command": "maintain_change_list" + "command": "clean_change_list" }, { "caption": "Change List: Show", diff --git a/changelist.py b/changelist.py index 7a3e8c9..ea324d6 100644 --- a/changelist.py +++ b/changelist.py @@ -3,17 +3,40 @@ import sys import json import codecs -try: - from .jsonio import JsonIO -except: - from jsonio import JsonIO -is_ST2 = int(sublime.version()) < 3000 +class Jfile: + def __init__(self, fpath, encoding="utf-8"): + self.encoding = encoding + self.fpath = fpath + + def load(self, default=[]): + self.fdir = os.path.dirname(self.fpath) + if not os.path.isdir(self.fdir): + os.makedirs(self.fdir) + if os.path.exists(self.fpath): + f = codecs.open(self.fpath, "r+", encoding=self.encoding) + try: + data = json.load(f) + except: + data = default + f.close() + else: + f = codecs.open(self.fpath, "w+", encoding=self.encoding) + data = default + f.close() + return data -key_prefix = "cl" + def save(self, data, indent=4): + f = codecs.open(self.fpath, "w+", encoding=self.encoding) + f.write(json.dumps(data, ensure_ascii=False, indent=indent)) + f.close() + + def remove(self): + if os.path.exists(self.fpath): os.remove(self.fpath) -def CLjson(): - return os.path.join(sublime.packages_path(), 'User', 'ChangeList.json') + +st2 = int(sublime.version()) < 3000 +key_prefix = "cl" # Change List object class CList(): @@ -37,6 +60,7 @@ def get_clist(cls, view): def __init__(self, view): self.view = view + self.jpath = os.path.join(sublime.packages_path(), 'User', 'ChangeList.json') def push_key(self): view = self.view @@ -120,7 +144,7 @@ def goto(self, index, show_at_bottom=False): def save(self): view = self.view vname = view.file_name() - jfile = JsonIO(CLjson()) + jfile = Jfile(self.jpath) data = jfile.load(default={}) def f(s): return str(s.begin())+","+str(s.end()) if s.begin()!=s.end() else str(s.begin()) @@ -132,7 +156,7 @@ def f(s): def load(self): view = self.view vname = view.file_name() - jfile = JsonIO(CLjson()) + jfile = Jfile(self.jpath) data = jfile.load(default={}) def f(s): return sublime.Region(int(s[0]),int(s[1])) if len(s)==2 \ @@ -193,7 +217,7 @@ def f(i,key): return "%3d: %s" % (view.rowcol(begin)[0]+1, view.substr(view.line(begin)).strip()) display_list = [ f(i,key) for i,key in enumerate(reversed(this_clist.key_list))] self.savept = [s for s in view.sel()] - if is_ST2: + if st2: self.window.show_quick_panel(display_list, self.on_done, sublime.MONOSPACE_FONT) else: @@ -209,7 +233,7 @@ def on_done(self, action): view.run_command("jump_to_change", {"index" : -action-1, 'show_at_bottom': True}) -class MaintainChangeList(sublime_plugin.WindowCommand): +class CleanChangeList(sublime_plugin.WindowCommand): def show_quick_panel(self, options, done): sublime.set_timeout(lambda: self.window.show_quick_panel(options, done), 10) @@ -236,7 +260,7 @@ def on_done(self, confirm): return action = self.action - jfile = JsonIO(CLjson()) + jfile = Jfile(self.jpath) if action==0: data = jfile.load(default={}) for item in [item for item in data if not os.path.exists(item)]: diff --git a/jsonio.py b/jsonio.py deleted file mode 100644 index 4b05883..0000000 --- a/jsonio.py +++ /dev/null @@ -1,32 +0,0 @@ -import os, codecs -import json - -class JsonIO: - def __init__(self, fpath, encoding="utf-8"): - self.encoding = encoding - self.fpath = fpath - - def load(self, default=[]): - self.fdir = os.path.dirname(self.fpath) - if not os.path.isdir(self.fdir): - os.makedirs(self.fdir) - if os.path.exists(self.fpath): - f = codecs.open(self.fpath, "r+", encoding=self.encoding) - try: - data = json.load(f) - except: - data = default - f.close() - else: - f = codecs.open(self.fpath, "w+", encoding=self.encoding) - data = default - f.close() - return data - - def save(self, data, indent=4): - f = codecs.open(self.fpath, "w+", encoding=self.encoding) - f.write(json.dumps(data, ensure_ascii=False, indent=indent)) - f.close() - - def remove(self): - if os.path.exists(self.fpath): os.remove(self.fpath)