2017-05-16 22:09:12 +02:00
|
|
|
import neovim
|
|
|
|
import fileinput
|
2017-05-17 21:52:30 +02:00
|
|
|
import pathlib
|
2017-05-16 22:09:12 +02:00
|
|
|
import subprocess
|
2017-05-17 21:52:30 +02:00
|
|
|
import sys
|
|
|
|
import traceback
|
2017-05-16 22:09:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@neovim.plugin
|
|
|
|
class NeotagsPlugin(object):
|
|
|
|
|
|
|
|
def __init__(self, nvim):
|
|
|
|
self.nvim = nvim
|
|
|
|
# TODO: Make configurable
|
|
|
|
self.tags_filename = 'tags'
|
|
|
|
# TODO: Make configurable
|
|
|
|
self.ctags_cmd = 'ctags'
|
|
|
|
# Perhaps fetch debugging settings?
|
|
|
|
|
2017-05-16 22:37:51 +02:00
|
|
|
# Check whether 'FileWritePost' is necessary
|
|
|
|
@neovim.autocmd('BufWritePost', pattern='*', eval='expand("<afile>:p")')
|
2017-05-16 22:09:12 +02:00
|
|
|
def update_tags_for_file(self, filename):
|
2017-05-17 21:52:30 +02:00
|
|
|
self.log('Triggered for "%s"' % filename)
|
|
|
|
|
2017-05-16 22:09:12 +02:00
|
|
|
pwd = self.nvim.funcs.execute('pwd').strip()
|
2017-05-17 21:52:30 +02:00
|
|
|
relative_filename = filename.replace(pwd, '').lstrip('\/')
|
|
|
|
tags_file = self.get_tags_file(filename)
|
2017-05-16 22:09:12 +02:00
|
|
|
|
|
|
|
try:
|
2017-05-17 21:52:30 +02:00
|
|
|
self.log('Start updating tags for "%s"' % filename)
|
|
|
|
self.strip_existing_tags(tags_file, relative_filename)
|
|
|
|
self.generate_tags(tags_file, relative_filename)
|
|
|
|
self.log('Tags for file "%s"' % filename)
|
|
|
|
except:
|
|
|
|
self.log(
|
|
|
|
'Failed to update tags for "%s", reason: %s' % (
|
|
|
|
filename, traceback.format_exc()
|
|
|
|
)
|
|
|
|
)
|
2017-05-16 22:09:12 +02:00
|
|
|
|
2017-05-17 21:52:30 +02:00
|
|
|
def strip_existing_tags(self, tags_f, filename):
|
2017-05-17 21:18:25 +02:00
|
|
|
with fileinput.input(files=tags_f, inplace=True, backup='.bak') as f:
|
2017-05-16 22:37:51 +02:00
|
|
|
for line in f:
|
2017-05-17 21:52:30 +02:00
|
|
|
if filename not in line:
|
2017-05-16 22:37:51 +02:00
|
|
|
sys.stdout.write(line)
|
2017-05-16 22:09:12 +02:00
|
|
|
|
2017-05-17 21:52:30 +02:00
|
|
|
def generate_tags(self, tags_file, filename):
|
2017-05-16 22:37:51 +02:00
|
|
|
subprocess.run([
|
2017-05-16 22:09:12 +02:00
|
|
|
self.ctags_cmd,
|
|
|
|
'-f',
|
2017-05-17 21:52:30 +02:00
|
|
|
tags_file,
|
2017-05-16 22:09:12 +02:00
|
|
|
'-a',
|
2017-05-17 21:52:30 +02:00
|
|
|
"%s" % filename
|
2017-05-16 22:09:12 +02:00
|
|
|
])
|
|
|
|
|
2017-05-17 21:52:30 +02:00
|
|
|
def get_tags_file(self, filename):
|
|
|
|
path = pathlib.Path(filename)
|
2017-05-17 21:18:11 +02:00
|
|
|
|
|
|
|
self.log('Test: ' + str(path.with_name(self.tags_filename)))
|
|
|
|
if path.with_name(self.tags_filename).is_file():
|
|
|
|
return str(path.with_name(self.tags_filename))
|
|
|
|
|
|
|
|
for folder in path.parents:
|
|
|
|
self.log('Test: ' + str(folder.with_name(self.tags_filename)))
|
|
|
|
if folder.with_name(self.tags_filename).is_file():
|
|
|
|
return str(folder.with_name(self.tags_filename))
|
2017-05-16 22:09:12 +02:00
|
|
|
|
|
|
|
raise ValueError('No tags file found in parent folders of given file')
|
|
|
|
|
|
|
|
def log(self, message):
|
2017-05-17 21:52:30 +02:00
|
|
|
self.nvim.out_write('neotags > ' + message + "\n")
|