-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcheatsheet.py
112 lines (88 loc) · 3.95 KB
/
cheatsheet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
import sublime, sublime_plugin
import json
_cheatsheetExtension = '.cheatsheet'
_commandExtension = '.sublime-commands'
_pluginHome = os.path.dirname(os.path.realpath(__file__));
_defaultDatabase = os.path.join(_pluginHome, 'database')
_defaultCommandsFile = os.path.join(_pluginHome, 'Default' + _commandExtension)
_localDatabase = os.path.join(os.path.expanduser('~'), '.cheatsheet')
_localCommandsFile = os.path.join(_localDatabase, 'Local' + _commandExtension)
_localCommandsFileSymlink = os.path.join(_pluginHome, 'Local' + _commandExtension)
def plugin_loaded():
vindow = sublime.active_window()
sublime.set_timeout(lambda: vindow.run_command('cheatsheet_refresh_local_database'), 2000)
class CheatsheetOpenCommand(sublime_plugin.WindowCommand):
def run(self, **args):
cheatsheet = os.path.join(_defaultDatabase, args['filename'])
self.dest_view = self.window.open_file(cheatsheet)
class CheatsheetOpenLocalCommand(sublime_plugin.WindowCommand):
def run(self, **args):
cheatsheet = os.path.join(_localDatabase, args['filename'])
self.dest_view = self.window.open_file(cheatsheet)
class CheatsheetRefreshLocalDatabaseCommand(sublime_plugin.WindowCommand):
def run(self, **args):
if os.path.isdir(_localDatabase):
self.loadCheatsheetCommands()
self.getCheatsheetFiles()
self.filterOutInvalidCheatsheets()
self.registerNewCheatsheets()
self.saveCheatsheetCommands()
self.symlinkCommandsFile()
def loadCheatsheetCommands(self):
if os.path.isfile(_localCommandsFile):
with open(_localCommandsFile, 'r') as input:
self.commands = json.load(input)
else:
self.commands = []
def getCheatsheetFiles(self):
self.cheatsheets = {
self.getRelativePath(root, filename) : self.getFilenameWithoutExtension(filename)
for root, dirnames, filenames in os.walk(_localDatabase)
for filename in filenames
if filename.endswith(_cheatsheetExtension)
}
def getRelativePath(self, root, filename):
return os.path.relpath(os.path.join(root, filename), _localDatabase)
def getFilenameWithoutExtension(self, filename):
return filename.replace(_cheatsheetExtension, '')
def filterOutInvalidCheatsheets(self):
self.commands = [
command for command in self.commands
if self.isValidCheatsheetCommand(command)
]
def isValidCheatsheetCommand(self, command):
return not self.isCheatsheetOpenCommand(command) or self.isValidCheatsheetOpenCommand(command)
def isValidCheatsheetOpenCommand(self, command):
return self.isCheatsheetOpenCommand(command) and self.cheatsheetExists(command['args']['filename'])
def isCheatsheetOpenCommand(self, command):
return command['command'] == 'cheatsheet_open_local'
def cheatsheetExists(self, cheatsheet):
return cheatsheet in self.cheatsheets.keys()
def getNewCheatsheets(self):
return {
path : cheatsheet
for path, cheatsheet in self.cheatsheets.items()
if self.isNewCheatsheet(path)
}
def isNewCheatsheet(self, path):
for command in self.commands:
if self.isCheatsheetOpenCommand(command) and command['args']['filename'] == path:
return False
return True
def registerNewCheatsheets(self):
for path, cheatsheet in self.getNewCheatsheets().items():
self.commands.append(self.newCheatsheetOpenCommand(cheatsheet, path))
def newCheatsheetOpenCommand(self, cheatsheet, path):
return {
'caption': 'Cheatsheet: ' + cheatsheet,
'command': 'cheatsheet_open_local',
'args': { 'filename': path }
}
def saveCheatsheetCommands(self):
with open(_localCommandsFile, 'w') as output:
json.dump(self.commands, output, sort_keys = True, indent = 2, separators = (',', ': '))
def symlinkCommandsFile(self):
if os.path.exists(_localCommandsFileSymlink):
os.remove(_localCommandsFileSymlink)
os.symlink(_localCommandsFile, _localCommandsFileSymlink)