vwtags: Distinguish (tag kinds) between header, preset and viewport

For tagbar to correctly nest the tags, we need to remember kinds of
parent scopes as well. This wasn't done in the original taskwiki
implementation.
This commit is contained in:
Tomas Janousek 2020-07-29 23:27:59 +02:00 committed by Tomas Babej
parent 07dc2b8ceb
commit 1e3ccb16f8
3 changed files with 21 additions and 19 deletions

View file

@ -15,39 +15,41 @@ from taskwiki import regexp
def match_header(line, syntax): def match_header(line, syntax):
m = re.search(regexp.VIEWPORT[syntax], line) m = re.search(regexp.VIEWPORT[syntax], line)
if m: if m:
return m return 'viewport', m
m = re.search(regexp.PRESET[syntax], line) m = re.search(regexp.PRESET[syntax], line)
if m: if m:
return m return 'preset', m
m = re.search(regexp.HEADER[syntax], line) m = re.search(regexp.HEADER[syntax], line)
if m: if m:
return m return 'header', m
return None return None, None
def process(file_content, filename, syntax): def process(file_content, filename, syntax):
state = [""]*6 parents = [None] * 6
for lnum, line in enumerate(file_content): for lnum, line in enumerate(file_content):
m = match_header(line, syntax) cur_kind_long, m = match_header(line, syntax)
if not m: if not m:
continue continue
cur_lvl = len(m.group('header_start')) cur_lvl = len(m.group('header_start')) - 1
cur_tag = m.group('name').strip() cur_tag = m.group('name').strip()
cur_searchterm = "^" + m.group(0).rstrip("\r\n") + "$" cur_searchterm = "^" + m.group(0).rstrip("\r\n") + "$"
cur_kind = "h" cur_kind = cur_kind_long[0]
state[cur_lvl-1] = cur_tag assert cur_lvl < len(parents)
for i in range(cur_lvl, 6): parents[cur_lvl] = cur_kind_long, cur_tag
state[i] = "" for i in range(cur_lvl + 1, len(parents)):
parents[i] = None
scope = "&&&".join( parent_scopes = [p for p in parents[0:cur_lvl] if p]
[state[i] for i in range(0, cur_lvl-1) if state[i] != ""]) scope = "&&&".join([tag for _, tag in parent_scopes])
if scope: if scope:
scope = "\theader:" + scope parent_kind, _ = parent_scopes[-1]
scope = "\t" + parent_kind + ":" + scope
yield('{0}\t{1}\t/{2}/;"\t{3}\tline:{4}{5}'.format( yield('{0}\t{1}\t/{2}/;"\t{3}\tline:{4}{5}'.format(
cur_tag, filename, cur_searchterm, cur_kind, str(lnum+1), scope)) cur_tag, filename, cur_searchterm, cur_kind, str(lnum+1), scope))

View file

@ -346,9 +346,9 @@ class Meta(object):
if tagbar_available: if tagbar_available:
vim.vars['tagbar_type_vimwiki'] = { vim.vars['tagbar_type_vimwiki'] = {
'ctagstype': 'default', 'ctagstype': 'default',
'kinds': ['h:header'], 'kinds': ['h:header', 'p:preset', 'v:viewport'],
'sro': '&&&', 'sro': '&&&',
'kind2scope': {'h': 'header'}, 'kind2scope': {'h': 'header', 'p': 'preset', 'v': 'viewport'},
'sort': 0, 'sort': 0,
'ctagsbin': os.path.join(BASE_DIR, 'extra/vwtags.py'), 'ctagsbin': os.path.join(BASE_DIR, 'extra/vwtags.py'),
'ctagsargs': cache().markup_syntax 'ctagsargs': cache().markup_syntax

View file

@ -77,9 +77,9 @@ class TestTagsViewportsPresets(MultiSyntaxTagsTest):
expected_output = [ expected_output = [
re.compile(r"^a file.wiki /.*/;\" h line:1$"), re.compile(r"^a file.wiki /.*/;\" h line:1$"),
re.compile(r"^b file.wiki /.*/;\" h line:2 header:a$"), re.compile(r"^b file.wiki /.*/;\" v line:2 header:a$"),
re.compile(r"^c file.wiki /.*/;\" h line:3 header:a$"), re.compile(r"^c file.wiki /.*/;\" p line:3 header:a$"),
re.compile(r"^d file.wiki /.*/;\" h line:4 header:a&&&c$"), re.compile(r"^d file.wiki /.*/;\" v line:4 preset:a&&&c$"),
] ]