-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhightlight_duplicates.py
282 lines (217 loc) · 8.3 KB
/
hightlight_duplicates.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'''
Provides duplicated lines highlighter.
Config summary (see README.md for details):
# file settings
{
"highlight_duplicates_color": "invalid"
}
@author: Nate Mills <[email protected]>
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
@since: 2017-10-17
@author: Aurelien Scoubeau <[email protected]>
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
@since: 2012-09-26
'''
# inspired by https://github.com/SublimeText/TrailingSpaces
import sublime
import sublime_plugin
from collections import defaultdict
DEFAULT_COLOR_SCOPE_NAME = "invalid"
DEFAULT_IS_ENABLED = True
DEFAULT_IS_DISABLED = False
DEFAULT_MIN_LINE_LENGTH = 4
DEFAULT_MIN_DUPLICATE_COUNT = 1
DEFAULT_USE_SELECTION = False
def count_lines(lines, view):
'''Counts line occurrences of a view using a hash.
The lines are stripped and tested before count.
'''
counts = defaultdict(list)
for line in lines:
string = view.substr(line)
if trimWhiteSpace():
string = string.strip()
if ignoreCase():
string = string.lower()
if is_candidate(string):
counts[string].append(line)
return counts
def filter_counts(counts):
'''Filters the line counts by rejecting every line having a count
lower or equal to the "min_duplicate_count" user setting, which defaults to 1.
'''
filtered = dict()
threshold = getMinDuplicateCount()
for k, v in counts.items():
if len(v) > threshold:
filtered[k] = v
return filtered
def remove_first(counts):
'''Removes the first region of each duplicate line
'''
trimmed = dict()
for k, v in counts.items():
v.pop(0)
trimmed[k] = v
return trimmed
def merge_results(countsList):
'''Merges the individual region lists
into one list
'''
merged = []
for k in countsList:
for v in k:
merged.append(v)
return merged
def is_candidate(string):
'''Tells if a string is a LOC candidate.
A candidate is a string long enough after stripping some symbols.
'''
minLineLength = getMinLineLength()
ignoreList = getIgnoreList()
if len(ignoreList) > 0 and string.strip().lower() in ignoreList:
return False
return len(string.strip('{}()[]/')) >= minLineLength
def show_lines(regions, view):
'''Merges all duplicated regions in one set and highlights them.'''
all_regions = []
for r in regions:
all_regions.extend(r)
color_scope_name = getHighlightColor()
view.add_regions('DuplicatesHighlightListener',
all_regions, color_scope_name, '',
sublime.DRAW_OUTLINED)
def add_lines(regions, view):
'''Merges all duplicated regions in one set and highlights them.'''
view.sel().clear()
for r in regions:
for i in r:
view.sel().add(i)
def remove_lines(regions, view, edit):
for r in reversed(regions):
view.erase(edit, sublime.Region(r.begin()-1, r.end()))
def get_lines(view, useSelection=False):
# get all lines
if useSelection:
result = []
for sel in view.sel():
result.extend(view.lines(sel))
return result
else:
return view.lines(sublime.Region(0, view.size()))
def highlight_duplicates(view):
'''Main function that glues everything for hightlighting.'''
# get all lines
lines = get_lines(view)
# count and filter out non duplicated lines
duplicates = filter_counts(count_lines(lines, view))
# show duplicated lines
show_lines(duplicates.values(), view)
def select_duplicates(view):
'''Main function that glues everything for hightlighting.'''
useSelection = getUseSelection() and len(view.sel()[0]) > 0
# get all lines
lines = get_lines(view, useSelection)
# count and filter out non duplicated lines
duplicates = filter_counts(count_lines(lines, view))
# select duplicated lines
add_lines(duplicates.values(), view)
def remove_duplicates(view, edit):
'''Main function that glues everything for hightlighting.'''
useSelection = getUseSelection() and len(view.sel()[0]) > 0
# get all lines
lines = get_lines(view, useSelection)
# count and filter out non duplicated lines
duplicates = remove_first(filter_counts(count_lines(lines, view)))
# select duplicated lines
merged = merge_results(duplicates.values())
merged.sort(key=lambda elm: elm.begin())
remove_lines(merged, view, edit)
def downlight_duplicates(view):
'''Removes any region highlighted by this plugin across all views.'''
view.erase_regions('DuplicatesHighlightListener')
def update_settings(newSetting):
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
settings.set('highlight_duplicates_enabled', newSetting)
sublime.save_settings('highlight_duplicates.sublime-settings')
def isEnabled():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
return bool(settings.get('highlight_duplicates_enabled', DEFAULT_IS_ENABLED))
def trimWhiteSpace():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
return bool(settings.get('trim_white_space', DEFAULT_IS_ENABLED))
def ignoreCase():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
return bool(settings.get('ignore_case', DEFAULT_IS_DISABLED))
def getHighlightColor():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
return settings.get('highlight_duplicates_color', DEFAULT_COLOR_SCOPE_NAME)
def getMinLineLength():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
minLength = settings.get('min_line_length', DEFAULT_MIN_LINE_LENGTH)
if isinstance(minLength, int):
return minLength
else:
return DEFAULT_MIN_LINE_LENGTH
def getMinDuplicateCount():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
minLength = settings.get('min_duplicate_count',
DEFAULT_MIN_DUPLICATE_COUNT)
if isinstance(minLength, int):
return max(DEFAULT_MIN_DUPLICATE_COUNT, minLength)
else:
return DEFAULT_MIN_DUPLICATE_COUNT
def getIgnoreList():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
ignoreList = settings.get('ignore_list', [])
for i in range(len(ignoreList)):
ignoreList[i] = ignoreList[i].strip().lower()
return ignoreList
def getUseSelection():
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
return settings.get('use_selection', DEFAULT_USE_SELECTION)
class HighlightDuplicatesCommand(sublime_plugin.WindowCommand):
'''Actual Sublime command. Run it in the console with:
view.window().run_command('highlight_duplicates')
'''
def run(self):
# If toggling on, go ahead and perform a pass,
# else clear the highlighting in all views
if isEnabled():
highlight_duplicates(self.window.active_view())
else:
downlight_duplicates(self.window)
class DuplicatesHighlightListener(sublime_plugin.EventListener):
'''Handles sone events to automatically run the command.'''
def on_modified(self, view):
if isEnabled():
highlight_duplicates(view)
else:
downlight_duplicates(view)
def on_activated(self, view):
if isEnabled():
highlight_duplicates(view)
else:
downlight_duplicates(view)
def on_load(self, view):
if isEnabled():
highlight_duplicates(view)
else:
downlight_duplicates(view)
class ToggleHighlightDuplicatesCommand(sublime_plugin.WindowCommand):
def run(self):
# If toggling on, go ahead and perform a pass,
# else clear the highlighting in all views
if isEnabled():
update_settings(False)
downlight_duplicates(self.window.active_view())
else:
update_settings(True)
highlight_duplicates(self.window.active_view())
class ToggleSelectDuplicatesCommand(sublime_plugin.TextCommand):
def run(self, edit):
select_duplicates(self.view)
self.view.end_edit(edit)
class RemoveDuplicatesCommand(sublime_plugin.TextCommand):
def run(self, edit):
remove_duplicates(self.view, edit)