2017-05-16 22:09:12 +02:00
|
|
|
import neovim
|
2017-05-16 22:37:51 +02:00
|
|
|
import sys
|
2017-05-17 21:18:11 +02:00
|
|
|
import pathlib
|
2017-05-16 22:09:12 +02:00
|
|
|
import fileinput
|
|
|
|
import subprocess
|
2017-05-16 22:37:51 +02:00
|
|
|
import locket
|
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):
|
|
|
|
self.filename = filename
|
|
|
|
pwd = self.nvim.funcs.execute('pwd').strip()
|
|
|
|
self.relative_filename = self.filename.replace(pwd, '').lstrip('\/')
|
|
|
|
|
|
|
|
try:
|
2017-05-16 22:37:51 +02:00
|
|
|
with locket.lock_file(self.get_tags_file() + '.lock'):
|
2017-05-16 23:54:13 +02:00
|
|
|
self.log('Start updating tags')
|
2017-05-16 22:37:51 +02:00
|
|
|
self.strip_existing_tags()
|
|
|
|
self.generate_tags()
|
2017-05-16 23:54:13 +02:00
|
|
|
self.log('Tags for file: "%s"' % self.relative_filename)
|
2017-05-16 22:09:12 +02:00
|
|
|
except ValueError:
|
|
|
|
self.log('No tags file found')
|
|
|
|
|
|
|
|
def strip_existing_tags(self):
|
2017-05-17 21:18:25 +02:00
|
|
|
tags_f = self.get_tags_file()
|
|
|
|
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-16 22:09:12 +02:00
|
|
|
if self.relative_filename not in line:
|
2017-05-16 22:37:51 +02:00
|
|
|
sys.stdout.write(line)
|
2017-05-16 22:09:12 +02:00
|
|
|
|
|
|
|
def generate_tags(self):
|
2017-05-16 22:37:51 +02:00
|
|
|
subprocess.run([
|
2017-05-16 22:09:12 +02:00
|
|
|
self.ctags_cmd,
|
|
|
|
'-f',
|
|
|
|
self.get_tags_file(),
|
|
|
|
'-a',
|
|
|
|
"%s" % self.relative_filename
|
|
|
|
])
|
|
|
|
|
|
|
|
def get_tags_file(self):
|
2017-05-17 21:18:11 +02:00
|
|
|
path = pathlib.Path(self.filename)
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.nvim.out_write(message + "\n")
|