-
Notifications
You must be signed in to change notification settings - Fork 0
/
elmtags.py
executable file
·110 lines (93 loc) · 3.47 KB
/
elmtags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python
"""
Blog Post:
https://www.maximewack.com/post/tagbar/
Code Gist:
https://gist.github.com/MaximeWack/388c393b7db290dd732f0a2d403118c5
"""
from __future__ import print_function
help_text = """
Extracts tags from Elm files. Useful for the Tagbar plugin.
Usage:
Install Tagbar (http://majutsushi.github.io/tagbar/). Then, put this file
anywhere and add the following to your .vimrc:
(SRI's EDITS:
NOTE:
There are many variations of the following options on the web, but
the following options (modified with my location of elmtags.py) seems
to be only one to work for me atm.
Also Mac OS seems to prevent runnning executables downloaded over the
internet and tagbar fails without any notifications of failure. So
instead of investiagting that further, I'm currently settling for using a
script. Since tagbar needs an executable, turn this script into one
with `chmod u+x ./elmtags.py`
let g:tagbar_type_elm = {
\ 'ctagstype':'elm'
\ , 'kinds':['h:header', 'i:import', 't:type', 'f:function', 'e:exposing']
\ , 'sro':'&&&'
\ , 'kind2scope':{ 'h':'header', 'i':'import'}
\ , 'sort':0
\ , 'ctagsbin':'~/code/personal/dotfiles/elmtags.py'
\ , 'ctagsargs': ''
\ }
ENDOF SRI'S EDITS)
"""
import sys
import re
if len(sys.argv) < 2:
print(help_text)
exit()
filename = sys.argv[1]
re_header = re.compile(r"^-- (.*)$")
re_import = re.compile(r"^import ([^ \n]+)( as ([^ \n]+))?( exposing (\([^)]+\)))?")
re_type = re.compile(r"^type( alias)? ([^ \n]+)( =)?$")
re_function = re.compile(r"^([^ ]+) : (.*)$")
file_content = []
try:
with open(filename, "r") as vim_buffer:
file_content = vim_buffer.readlines()
except:
exit()
cur_head = ""
for lnum, line in enumerate(file_content):
match_header = re_header.match(line)
match_import = re_import.match(line)
match_type = re_type.match(line)
match_function = re_function.match(line)
cur_searchterm = ""
cur_kind = ""
args = ""
lines = ""
if match_header:
cur_head = match_header.group(1)
cur_tag = cur_head
cur_searchterm = "^-- " + cur_tag + "$"
cur_kind = "h"
elif match_import:
cur_tag = match_import.group(1)
cur_searchterm = "^import " + cur_tag
cur_kind = "i"
args = "\theader:" + cur_head
if match_import.group(3):
args = args + "\tsignature:(" + cur_tag + ")"
cur_tag = match_import.group(3)
if match_import.group(5):
exposing = match_import.group(5).strip("()").replace(" ", "").split(",")
for exposed in exposing:
lines = lines + '\n{0}\t{1}\t/{2}/;"\t{3}\tline:{4}{5}'.format(exposed, filename, cur_searchterm, "e", str(lnum+1), "\taccess:public\timport:" + cur_head + "&&&" + cur_tag)
elif match_type:
cur_tag = match_type.group(2)
if match_type.group(1) == " alias":
args = "\taccess:protected"
cur_searchterm = "^type " + cur_tag + "$"
cur_kind = "t"
args = args + "\theader:" + cur_head
elif match_function:
cur_tag = match_function.group(1)
cur_searchterm = "^" + cur_tag + " :"
cur_kind = "f"
args = "\theader:" + cur_head + "\tsignature:(" + match_function.group(2) + ")"
else:
continue
print('{0}\t{1}\t/{2}/;"\t{3}\tline:{4}{5}'.format(
cur_tag, filename, cur_searchterm, cur_kind, str(lnum+1), args) + lines)