TASK: Finish first working implementation and readme

This commit is contained in:
Daniel Siepmann 2017-05-16 22:37:51 +02:00
parent 68cc2faae8
commit 2a6c196693
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 42 additions and 45 deletions

View file

@ -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("<afile>:p")'
)
# Check whether 'FileWritePost' is necessary
@neovim.autocmd('BufWritePost', pattern='*', eval='expand("<afile>: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):

28
rplugin/readme.rst Normal file
View file

@ -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

1
rplugin/requirements.py Normal file
View file

@ -0,0 +1 @@
locket