/
AutoPHPDollar.py
148 lines (113 loc) · 4.44 KB
/
AutoPHPDollar.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
'''
AutoPHPDollar - sublime text 2 plugin, automatically adds "$" before variables
Copyright (c) 2012, Gennadiy Kovalev
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import re
import sublime
import sublime_plugin
settings = sublime.load_settings('AutoPHPDollar.sublime-settings')
ignore_names = settings.get("ignore names", [])
rules = settings.get("rules", [])
ignore_after = settings.get("ignore after", [])
def get_patterns(view):
return rules + [
#already known variables
#quick hack: ignore variables adjaced to ", my personal preferense
{
"search": r"(?<![\$\w\d\>\"\'])"
+ variables_set_to_regex(find_variables(view))
+ "(?=[^\w^\d])",
"replace": r"$\1"
},
#remove if const, function, class before
{
"search": variables_set_to_regex(ignore_after) + r"(\s+)\$",
"replace": r"\1\2"
}
]
def in_list(region, list):
if len(list) == 0:
return True
for item in list:
if item.contains(region):
return True
return False
def find_variables(view):
#find all php variables in current view
variables = set()
regions = view.find_all("\$\w[\w\d]*")
for region in regions:
text = view.substr(region)[1:]
if text in ignore_names:
continue
variables.add(text)
return variables
def syntax_name(view):
syntax = os.path.basename(view.settings().get('syntax'))
syntax = os.path.splitext(syntax)[0]
return syntax
def variables_set_to_regex(variables):
regex = "(this|"
for variable in variables:
regex += variable + "|"
regex = regex[0:-1] + ")"
return regex
def apply_patterns(text, patterns):
for pattern in patterns:
text = re.sub(
pattern["search"],
pattern["replace"],
text
)
return text
class CphpCommand(sublime_plugin.TextCommand):
def run(self, edit, corrected, text, a, b):
view = self.view
to_cursor = sublime.Region(a, b)
view.replace(edit, to_cursor, corrected)
class CphpListener(sublime_plugin.EventListener):
def on_modified(self, view):
if syntax_name(view) == "PHP":
# avoid heavy calculations for "hold delete/backspace and wait"
action = view.command_history(0, True)[0]
if action == "left_delete" or action == "right_delete":
return
#do not change text again if undo requested
if view.command_history(1, True)[0] == "cphp":
return
#generate patterns
#worst way, first point to optimize
patterns = get_patterns(view)
#get list of <? .. ?> segments
php_regions = view.find_all(r"<\?[\w\W]+?(\?>|\z)")
#get list of commented regions
comments = view.find_all(r"(#|//).+|/\*[\w\W]+?\*/")
#strings should be modified in the reversed order
#to keep upper regions positions correct
selection = view.sel()
for i in range(len(selection) - 1, -1, -1):
#do not make changes inside comments
if len(comments) and in_list(selection[i], comments):
continue
#do not make any changes outside <? ... ?> segments
if not in_list(selection[i], php_regions):
continue
line = view.line(selection[i])
#fix line position, do not place cursor to the end of line
to_cursor = sublime.Region(line.a, selection[i].b)
text = view.substr(to_cursor)
corrected = apply_patterns(text, patterns)
if corrected != text:
view.run_command("cphp", {"corrected": corrected, "text": text, "a": line.a, "b": selection[i].b});