diff --git a/rplugin/python3/neotags.py b/rplugin/python3/neotags.py index a85954f..1dfca96 100644 --- a/rplugin/python3/neotags.py +++ b/rplugin/python3/neotags.py @@ -1,7 +1,9 @@ import neovim import os +import sys import fileinput import subprocess +import locket @neovim.plugin @@ -15,57 +17,30 @@ class NeotagsPlugin(object): self.ctags_cmd = 'ctags' # Perhaps fetch debugging settings? - # Check whether a single command is enough as it's triggered twice. - @neovim.autocmd( - 'BufWritePost,FileWritePost', - pattern='*', - eval='expand(":p")' - ) + # Check whether 'FileWritePost' is necessary + @neovim.autocmd('BufWritePost', pattern='*', eval='expand(":p")') def update_tags_for_file(self, filename): self.filename = filename pwd = self.nvim.funcs.execute('pwd').strip() self.relative_filename = self.filename.replace(pwd, '').lstrip('\/') - self.log('Start updating tags for abs: ' + self.filename) - self.log('Start updating tags for rel: ' + self.relative_filename) - - # TODO: Add locking try: - self.strip_existing_tags() - self.generate_tags() + with locket.lock_file(self.get_tags_file() + '.lock'): + self.log('Start updating tags for: ' + self.relative_filename) + self.strip_existing_tags() + self.generate_tags() + self.log('Tags updated for: ' + self.relative_filename) except ValueError: self.log('No tags file found') def strip_existing_tags(self): - tagsf = self.get_tags_file() - backup = '.bak' - - file = fileinput.FileInput(files=tagsf, inplace=True, backup=backup) - try: - for line in file: + with fileinput.input(files=self.get_tags_file(), inplace=True, backup='.bak') as f: + for line in f: if self.relative_filename not in line: - self.log('Keep line: ' + line) - print(line) - else: - self.log('Remove line: ' + line) - finally: - file.close() - try: - os.unlink(tagsf + backup) - except FileNotFoundError: - pass - # with fileinput.input(files=tagsf, inplace=True, backup=backup) as f: - # self.log('Lookup' + self.relative_filename) - # for line in f: - # self.log('Line: ' + line) - # if self.relative_filename not in line: - # self.log('Keep line') - # print(line) - # else: - # self.log('Remove line') + sys.stdout.write(line) def generate_tags(self): - finished = subprocess.run([ + subprocess.run([ self.ctags_cmd, '-f', self.get_tags_file(), @@ -73,13 +48,6 @@ class NeotagsPlugin(object): "%s" % self.relative_filename ]) - if finished.stderr: - self.log('Error:' + finished.stderr) - if finished.stdout: - self.log('Output:' + finished.stdout) - if finished.returncode: - self.log('Code:' + finished.returncode) - def get_tags_file(self): start_dir = os.path.dirname(self.filename) for dirpath, __, filenames in os.walk(start_dir, topdown=False): diff --git a/rplugin/readme.rst b/rplugin/readme.rst new file mode 100644 index 0000000..7b61666 --- /dev/null +++ b/rplugin/readme.rst @@ -0,0 +1,28 @@ +About +===== + +Basic ctags auto update plugin for Neovim. + +Makes use of neovims async plugin API, so non blocking. + +Will not use some checks but lookup the whole path to find tags file. + +Install +======= + +Install python requirements in python3:: + + pip3 install -r requirements.txt + +TODO +==== + +* Provide configuration through vim + + * ctags binary to use + + * Tags file name to use + + * Logging / Debugging + +* Handle possible issues with ctags execution diff --git a/rplugin/requirements.py b/rplugin/requirements.py new file mode 100644 index 0000000..983979e --- /dev/null +++ b/rplugin/requirements.py @@ -0,0 +1 @@ +locket