-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathevents.py
157 lines (128 loc) · 5.74 KB
/
events.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sublime
import sublime_plugin
import os
import time
import re
from . import context
from . import util
from .salesforce.lib.panel import Printer
from .salesforce.lib import lightning
class SFDCEventListener(sublime_plugin.EventListener):
""" Tag attribute description completion when hover
"""
def on_hover(self, view, pt, hover_zone):
tag_defs = lightning.tag_defs
word = view.substr(view.extract_scope(pt-1))
matched_regions = view.find_all("<\\w+[:-]*\\w+[\\s\\S]*?>")
matched_region = None
for mr in matched_regions:
if mr.contains(pt):
matched_region = mr
if matched_region:
matched_str = view.substr(matched_region)[1:-1]
matched_tag = matched_str.split(" ")[0].strip()
matched_attr = view.substr(view.extract_scope(pt-1))[:-1]
if matched_tag in tag_defs and matched_attr in tag_defs[matched_tag]["attribs"]:
tag_attrib = tag_defs[matched_tag]["attribs"][matched_attr]
if "description" in tag_attrib and tag_attrib["description"]:
view.show_popup(
"""<h3 style="padding: 5px; border-bottom: 1px solid white">{name}</h3>
<div style="height:150px">
{desc}
</div>
""".format(name=matched_attr, desc=tag_attrib["description"]),
sublime.HIDE_ON_MOUSE_MOVE_AWAY
)
def on_new(self, view):
"""
1. Everytime when you open a new view, default syntax is Apex
2. Set Status with current default project
"""
view.set_syntax_file("Packages/Java/Java.tmLanguage")
util.display_active_project(view)
def on_load_async(self, view):
""" Set Status with current default project
"""
util.display_active_project(view)
# Add types settings for build_package_xml command
if view.file_name() != None:
cname = os.path.basename(view.file_name())
if cname and "package.xml" in cname.lower():
with open(view.file_name(), "rb") as fp:
content = fp.read()
try:
types = util.build_package_types(content)
view.settings().set("types", types)
except:
pass
# Open controller or extension
# print (view.file_name())
# if view.file_name() and view.file_name().endswith(".page"):
# matched_region = view.find('(controller="\\w+#"|extensions="\\w+#")', 0)
# print (view.substr(matched_region))
# if not matched_region: return
# matched_block = view.substr(matched_region).strip()
# print (matched_block)
def on_post_save_async(self, view):
settings = context.get_settings();
if not view.file_name(): return
file_name = view.file_name().replace("\\", "/")
workspace = settings["workspace"].replace("\\", "/")
if workspace not in file_name: return
if settings.get('auto_update_on_save'):
view.run_command('save_to_server')
def on_activated(self, view):
"""
1. Switch project to which view file is in
2. Sync sidebar with view file
3. Set Status with current default project
"""
settings = context.get_settings()
file_name = view.file_name()
if settings["auto_switch_project_on_file_activated"] and file_name:
project_name = util.get_path_attr(file_name)[0]
if project_name in settings["projects"].keys():
util.switch_project(project_name)
if settings["reveal_file_in_sidebar_on_file_activated"]:
view.window().run_command("reveal_in_side_bar")
util.display_active_project(view)
def on_query_completions(self, view, prefix, locations):
if not view.match_selector(locations[0], "text.html - source"):
return []
location = locations[0]
pt = locations[0] - len(prefix) - 1
ch = view.substr(sublime.Region(pt, pt + 1))
# If char is not # or current view is not file, just skip
if ch != "#" or not view.file_name(): return
begin = view.full_line(pt).begin()
matched_region = view.find('(controller="\\w+#"|extensions="\\w+#")', begin)
if not matched_region: return
matched_block = view.substr(matched_region).strip()
# Delete the input #
view.run_command("left_delete")
# Get the Class Name
class_name = view.substr(view.word(pt-1))
# Get the template type
if "controller" in matched_block:
template_name = "Controller"
elif "extensions" in matched_block:
template_name = "StandardController Extension"
# Check whether project of current page is active
settings = context.get_settings();
base, name = os.path.split(view.file_name())
src, meta_type = os.path.split(base)
default_project = settings["default_project_name"]
if default_project not in src:
Printer.get('error').write("Current page is not in active project")
return
# Check whether the class is exist
file_name = os.path.join(src, "classes", class_name+".cls")
if os.path.exists(file_name):
Printer.get('error').write("%s is already exist" % class_name)
return
sublime.active_window().run_command("create_component", {
"template_name": template_name,
"component_name": class_name,
"component_type": "ApexClass",
"markup_or_body": "Body"
})