From 1e3ccb16f85d59b7912927dd44faabf5833fd311 Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Wed, 29 Jul 2020 23:27:59 +0200 Subject: [PATCH] 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. --- extra/vwtags.py | 30 ++++++++++++++++-------------- taskwiki/main.py | 4 ++-- tests/test_vwtags.py | 6 +++--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/extra/vwtags.py b/extra/vwtags.py index 41ce172..ae03f63 100755 --- a/extra/vwtags.py +++ b/extra/vwtags.py @@ -15,39 +15,41 @@ from taskwiki import regexp def match_header(line, syntax): m = re.search(regexp.VIEWPORT[syntax], line) if m: - return m + return 'viewport', m m = re.search(regexp.PRESET[syntax], line) if m: - return m + return 'preset', m m = re.search(regexp.HEADER[syntax], line) if m: - return m + return 'header', m - return None + return None, None def process(file_content, filename, syntax): - state = [""]*6 + parents = [None] * 6 for lnum, line in enumerate(file_content): - m = match_header(line, syntax) + cur_kind_long, m = match_header(line, syntax) if not m: continue - cur_lvl = len(m.group('header_start')) + cur_lvl = len(m.group('header_start')) - 1 cur_tag = m.group('name').strip() cur_searchterm = "^" + m.group(0).rstrip("\r\n") + "$" - cur_kind = "h" + cur_kind = cur_kind_long[0] - state[cur_lvl-1] = cur_tag - for i in range(cur_lvl, 6): - state[i] = "" + assert cur_lvl < len(parents) + parents[cur_lvl] = cur_kind_long, cur_tag + for i in range(cur_lvl + 1, len(parents)): + parents[i] = None - scope = "&&&".join( - [state[i] for i in range(0, cur_lvl-1) if state[i] != ""]) + parent_scopes = [p for p in parents[0:cur_lvl] if p] + scope = "&&&".join([tag for _, tag in parent_scopes]) 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( cur_tag, filename, cur_searchterm, cur_kind, str(lnum+1), scope)) diff --git a/taskwiki/main.py b/taskwiki/main.py index 8e85cb6..fb781ac 100644 --- a/taskwiki/main.py +++ b/taskwiki/main.py @@ -346,9 +346,9 @@ class Meta(object): if tagbar_available: vim.vars['tagbar_type_vimwiki'] = { 'ctagstype': 'default', - 'kinds': ['h:header'], + 'kinds': ['h:header', 'p:preset', 'v:viewport'], 'sro': '&&&', - 'kind2scope': {'h': 'header'}, + 'kind2scope': {'h': 'header', 'p': 'preset', 'v': 'viewport'}, 'sort': 0, 'ctagsbin': os.path.join(BASE_DIR, 'extra/vwtags.py'), 'ctagsargs': cache().markup_syntax diff --git a/tests/test_vwtags.py b/tests/test_vwtags.py index 5620eca..c8e52c6 100644 --- a/tests/test_vwtags.py +++ b/tests/test_vwtags.py @@ -77,9 +77,9 @@ class TestTagsViewportsPresets(MultiSyntaxTagsTest): expected_output = [ re.compile(r"^a file.wiki /.*/;\" h line:1$"), - re.compile(r"^b file.wiki /.*/;\" h line:2 header:a$"), - re.compile(r"^c file.wiki /.*/;\" h line:3 header:a$"), - re.compile(r"^d file.wiki /.*/;\" h line:4 header:a&&&c$"), + re.compile(r"^b file.wiki /.*/;\" v line:2 header:a$"), + re.compile(r"^c file.wiki /.*/;\" p line:3 header:a$"), + re.compile(r"^d file.wiki /.*/;\" v line:4 preset:a&&&c$"), ]