forked from enriquein/JavaSetterGetter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaSetterGetter.py
More file actions
87 lines (72 loc) · 2.96 KB
/
JavaSetterGetter.py
File metadata and controls
87 lines (72 loc) · 2.96 KB
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
import sublime, sublime_plugin
import re
DEBUG = False
java_field_pat = "(?P<indent>\s*)" + \
"(?P<access>protected|private)" + \
"(?: (?P<transient>transient|volatile))?" + \
"(?: (?P<static>static))?" + \
"(?: (?P<final>final))?" + \
"(?: (?P<type>[a-zA-Z0-9_$\<\>\,\.\s]+))" + \
"(?: (?P<varname>[a-zA-Z0-9_$]+))" + \
"(?:\s*=.+)?;"
java_field_pat = re.compile(java_field_pat)
def matchdict(text):
m = java_field_pat.match(text)
if m:
return m.groupdict()
else:
return {}
def getSelections(view):
position = 0
selected = []
sels = view.sel()
for sel in sels:
lineRegions = view.lines(sel)
for lineRegion in lineRegions:
position = max(position, lineRegion.end())
selected.extend(view.substr(lineRegion).split("\n"))
selection_matches = []
for line in selected:
md = matchdict(line)
if DEBUG:
print line, md
if md.get("access", None) is not None: # Make sure it's private or protected
if md.get("static", None) is None: # Make sure it's not static
selection_matches.append(md)
return {"position": position, "selections": selection_matches}
class JavaSetterGetterCommand(sublime_plugin.TextCommand):
def run(self, edit):
selections = getSelections(self.view)
selection_matches = selections["selections"]
properties = []
insert_position = selections["position"]
getter_arr = []
setter_arr = []
getterTemplate = """
{3}public {1} get{0}() {{
{3} return this.{2};
{3}}}"""
setterTemplate = """
{3}public void set{0}({1} {2}) {{
{3} this.{2} = {2};
{3}}}"""
for prop in selection_matches:
property_name = prop['varname']
capitalized_name = property_name[0].capitalize() + property_name[1:len(property_name)]
getter_arr.append(getterTemplate.format(capitalized_name, prop['type'], prop['varname'], prop['indent']))
if not prop["final"]:
setter_arr.append(setterTemplate.format(capitalized_name, prop['type'], prop['varname'], prop['indent']))
# don't print anything if we don't have any instance members
if len(getter_arr)==0 and len(setter_arr)==0:
return
try:
edit = self.view.begin_edit('java_setter_getter')
properties_text = "\n" + "\n".join(getter_arr) + "\n" + "\n".join(setter_arr)
insert_count = self.view.insert(edit, insert_position, properties_text)
self.view.sel().clear()
self.view.sel().add(sublime.Region(insert_position, (insert_position + insert_count)))
except Exception, ex:
if DEBUG:
print ex
finally:
self.view.end_edit(edit)