-
Notifications
You must be signed in to change notification settings - Fork 31
/
avalonHelper.py
139 lines (124 loc) · 4.5 KB
/
avalonHelper.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import sublime
import sublime_plugin
import re
import sys
import os
import webbrowser
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
PACKAGES_PATH = sublime.packages_path() or os.path.dirname(BASE_PATH)
_msAttrList = []
_widgetList = []
_dataAttrList = []
msAttrList = []
widgetList = []
dataAttrList = []
specialList = []
# widget replacer
def widget_place(widgetName, is_completion = True, indents = ""):
str = indents
if widgetName in specialList:
if is_completion:
str += "ms-" + widgetName + "=\"\$,\$${1:" + widgetName + "}Opts\""
else:
str += "ms-" + widgetName + "=\"$,$" + widgetName + "Opts\""
else:
if not is_completion:
str += "ms-widget=\"" + widgetName + "," + widgetName + "Name,$" + widgetName + "Opts" + "\""
else:
str += "ms-widget=\"" + widgetName + ",${1:" + widgetName + "}Name,\$${1:" + widgetName + "}Opts" + "\""
return str
# load syntax
def on_load():
path = BASE_PATH
languagefile = sublime.load_settings("helper.sublime-settings")
global settings
settings = sublime.load_settings("avalonHelper.sublime-settings")
global _widgetList
global _msAttrList
global _dataAttrList
global widgetList
global msAttrList
global dataAttrList
msAttrList = languagefile.get("ms").split(",")
widgetList = languagefile.get("widget").split(",")
dataAttrList = languagefile.get("data").split(",")
loop = 0
for v in widgetList:
if v.count("="):
v = v.split("=")[0]
widgetList[loop] = v
specialList.append(v)
loop = loop + 1
_widgetList.append(("widget " + v + "\t组件", widget_place(v, True)))
for v in msAttrList:
_msAttrList.append((v + "\tbind", "" + v + "=\"${1:}\""))
for v in dataAttrList:
_dataAttrList.append((v + "\tbind", "" + v + "=\"${1:}\""))
# define parttens
msLike = re.compile(r"^ms")
msWidget = re.compile(r"^ms\-w[^\s\"\']+")
dataLike = re.compile(r"^d(ata\-)?")
blankLike= re.compile(r"[\s\"\']+")
indexLike= re.compile(r"^[^<\s\n]+\s")
# add ms-widget partten
widgetLike = re.compile(r"^w[idget]")
class AvalonHelperCompletionsPackageEventListener(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
select = view.sel()[0]
self.view = view
indent = self.get_indent(len(prefix))
if select.empty():
if widgetLike.match(prefix):
if not indent:
return _widgetList
_list = []
for i,v in _widgetList:
_list.append((i, indent + v))
return _list;
if prefix in widgetList:
replacer = widget_place(prefix, True, indent)
return [(prefix + "\tWidget", replacer)]
if prefix == "m" or msLike.match(prefix):
if not indent:
return _msAttrList
_list = []
for i,v in _msAttrList:
_list.append((i, indent + v ))
return _list
if dataLike.match(prefix):
_list = []
for i,v in _dataAttrList:
_list.append((i, indent + v ))
for v in widgetList:
_list.append(("data-" + v + "\toptions", indent + "data-" + v + "-${1:OptName}" + "=\"\""))
return _list
return None
def get_indent(self, length=0):
if not settings.get("autoBr"):
return ''
nowLine = self.view.line(self.view.sel()[0])
prevline = self.view.substr(nowLine).replace("\t", " ").strip()
## 加个判断,如果本身已经在行首就不要做什么了
left = self.view.substr(sublime.Region(nowLine.a, self.view.sel()[0].a-length)).strip()
if not left:
return ''
tabs = indexLike.findall(prevline)
if tabs:
c = 0
else:
c = len(prevline.split(" ")[0]) + 1
indents = ""
while len(indents) < c:
indents += " "
return "\n" + indents
# command
class HelperCommand(sublime_plugin.TextCommand):
def run(self, edit, type): # 打开文档
url = settings.get(type)
webbrowser.open(url)
pass
if int(sublime.version()) < 3000:
on_load()
else:
def plugin_loaded():
on_load()