-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprojectile.py
113 lines (95 loc) · 4.2 KB
/
projectile.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
"""Kind using JSON to persist data for projects."""
# =============================================================================
# FILE: projectile.py
# AUTHOR: Clay Dunston <[email protected]>
# License: MIT
# Last Modified: 2017-12-29
# =============================================================================
import json
import datetime
from os.path import basename, isdir, normpath
from ..kind.directory import Kind as Directory
from denite.util import expand, path2project
class Kind(Directory):
"""Kind using JSON to persist data for projects."""
def __init__(self, vim):
"""Initialize thyself."""
super().__init__(vim)
self.name = 'projectile'
self.default_action = 'open'
self.persist_actions += ['delete']
self.redraw_actions += ['delete']
self.vars = {
'exclude_filetypes': ['denite'],
'date_format': '%d %b %Y %H:%M:%S',
'data_dir': vim.vars.get('projectile#data_dir', '~/.cache/projectile'),
'user_cmd': vim.vars.get('projectile#directory_command'),
}
def action_add(self, context):
"""Add a project to ``projectile#data_dir``/projects.json."""
data_file = expand(self.vars['data_dir'] + '/projects.json')
root_dir = self.vim.call('getcwd')
boofer = self.vim.current.buffer.name
pj_root = path2project(self.vim, boofer, '.git,.svn,.hg')
pj_name = basename(normpath(pj_root))
new_data = {}
project_root = str(self.vim.call('denite#util#input', 'Project Root: ', pj_root, ''))
if not len(project_root):
project_root = pj_root
project_name = str(self.vim.call('denite#util#input', 'Project Name: ', pj_name, ''))
if not len(project_name):
project_name = pj_name
new_data = {
'name': project_name,
'root': project_root,
'timestamp': str(datetime.datetime.now().isoformat()),
'description': '',
'vcs': isdir("{}/.git".format(root_dir)) # TODO: Also check for .hg/ and .svn
}
with open(data_file, 'r') as g:
try:
json_info = json.load(g)
except json.JSONDecodeError:
json_info = []
# remove old project information
projects = json_info[:]
for i in range(len(projects)):
if projects[i]['root'] == project_root and projects[i]['name'] == project_name:
projects.pop(i)
break
projects.append(new_data)
with open(data_file, 'w') as f:
json.dump(projects, f, indent=2)
def action_delete(self, context):
"""Remove a project from *projects.json*."""
target = context['targets'][0]
target_date = target['timestamp']
target_name = target['name']
data_file = expand(self.vars['data_dir'] + '/projects.json')
confirmation = self.vim.call('confirm', "Remove {}?".format(target_name), "&Yes\n&No")
if confirmation == 2:
return
else:
with open(data_file, 'r') as g:
content = json.load(g)
projects = content[:]
for i in range(len(projects)):
if projects[i]['timestamp'] == target_date:
projects.pop(i)
break
with open(data_file, 'w') as f:
json.dump(projects, f, indent=2)
def action_custom(self, context):
"""Execute a custom action defined by ``g:projectile#directory_command``."""
target = context['targets'][0]
user_cmd = self.vim.vars.get('projectile#directory_command')
if not isdir(target['action__path']):
return
destination = expand(target['action__path'])
self.vim.call('execute', '{} {}'.format(user_cmd, destination))
def action_open(self, context):
target = context['targets'][0]
if not isdir(target['action__path']):
return
self.vim.command('lcd {}'.format(target['action__path']))
self.vim.command('Denite file/rec')