diff --git a/readme.md b/readme.md index 5338989..bf6291c 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,7 @@ - Access the root directory of a project with a customizable action - Check version control status for all added projects - List todos in a project directory + - Add projects automatically in background (currently only support neovim) - Bookmarks - Keep a list of bookmarked locations - TODOs diff --git a/rplugin/python3/denite/kind/projectile.py b/rplugin/python3/denite/kind/projectile.py index 5362c66..43896de 100644 --- a/rplugin/python3/denite/kind/projectile.py +++ b/rplugin/python3/denite/kind/projectile.py @@ -11,7 +11,7 @@ from os.path import basename, isdir, normpath from ..kind.directory import Kind as Directory -from denite.util import expand, input, path2project +from denite.util import expand, path2project class Kind(Directory): @@ -36,15 +36,15 @@ def action_add(self, context): 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_root = path2project(self.vim, boofer, '.git,.svn,.hg') pj_name = basename(normpath(pj_root)) new_data = {} - project_root = input(self.vim, context, 'Project Root: ', pj_root) + project_root = str(self.vim.call('denite#util#input', 'Project Root: ', pj_root, '')) if not len(project_root): project_root = pj_root - project_name = input(self.vim, context, 'Project Name: ', pj_name) + project_name = str(self.vim.call('denite#util#input', 'Project Name: ', pj_name, '')) if not len(project_name): project_name = pj_name @@ -61,10 +61,18 @@ def action_add(self, context): json_info = json.load(g) except json.JSONDecodeError: json_info = [] - json_info.append(new_data) + + # 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(json_info, f, indent=2) + json.dump(projects, f, indent=2) def action_delete(self, context): """Remove a project from *projects.json*.""" @@ -96,4 +104,10 @@ def action_custom(self, context): 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') diff --git a/rplugin/python3/denite/source/sauce.py b/rplugin/python3/denite/source/sauce.py deleted file mode 100644 index 6b58436..0000000 --- a/rplugin/python3/denite/source/sauce.py +++ /dev/null @@ -1,40 +0,0 @@ -"""A Denite source for Denite sources.""" -# ============================================================================== -# FILE: sauce.py -# AUTHOR: Clay Dunston -# License: MIT License -# Last Modified: 2017-12-30 -# ============================================================================== - - -from .base import Base - - -class Source(Base): - """I wanna be the very best, like no one ever was.""" - - def __init__(self, vim): - """To catch them is my real test, to train them is my cause.""" - super().__init__(vim) - - self.name = 'sauce' - self.kind = 'command' - self.vars = {} - - def on_init(self, context): - """I will travel across the land, searching far and wide.""" - context['__sauces'] = self.vim.call('projectile#CommandCompletion', 'Denite ') - - def gather_candidates(self, context): - """Each Denite source, to understand, the power that's insiiide.""" - candidates = [] - - for sauce in context['__sauces']: - if sauce != 'Denite': - candidates.append({ - 'word': sauce, - 'action__command': 'Denite ' + sauce, - }) - - return candidates - diff --git a/rplugin/python3/projectile/__init__.py b/rplugin/python3/projectile/__init__.py new file mode 100644 index 0000000..aacc427 --- /dev/null +++ b/rplugin/python3/projectile/__init__.py @@ -0,0 +1,13 @@ +import pynvim +from projectile.projectile import Projectile + + +@pynvim.plugin +class ProjectileHandlers(object): + def __init__(self, nvim): + self._nvim = nvim + self._projectile = Projectile(self._nvim) + + @pynvim.autocmd('BufRead', pattern='*', sync=False) + def on_bufread(self): + self._projectile.auto_add_project() diff --git a/rplugin/python3/projectile/projectile.py b/rplugin/python3/projectile/projectile.py new file mode 100644 index 0000000..c4a2e31 --- /dev/null +++ b/rplugin/python3/projectile/projectile.py @@ -0,0 +1,46 @@ +import json +import datetime +from os.path import basename, isdir, normpath +from denite.util import expand, path2project + + +class Projectile(object): + def __init__(self, nvim): + self._nvim = nvim + self._data_dir = self._nvim.eval('g:projectile#data_dir') + + def auto_add_project(self): + data_file = expand(self._data_dir + '/projects.json') + boofer = self._nvim.current.buffer.name + pj_root = path2project(self._nvim, boofer, '.git,.hg,.svn') + + is_pj = (isdir("{}/.git".format(pj_root)) + or isdir("{}/.hg".format(pj_root)) + or isdir("{}/.svn".format(pj_root))) + if is_pj: + is_new_pj = True + with open(data_file, 'r') as g: + try: + json_info = json.load(g) + except json.JSONDecodeError: + json_info = [] + + projects = json_info[:] + for i in range(len(projects)): + if projects[i]['root'] == pj_root: + is_new_pj = False + break + + if is_new_pj: + pj_name = basename(normpath(pj_root)) + new_data = { + 'name': pj_name, + 'root': pj_root, + 'timestamp': str(datetime.datetime.now().isoformat()), + 'description': '', + 'vcs': is_pj + } + + projects.append(new_data) + with open(data_file, 'w') as f: + json.dump(projects, f, indent=2)