diff --git a/Default.sublime-commands b/Default.sublime-commands index 76dd588..89000ca 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -2,5 +2,9 @@ { "caption": "Highlight Duplicates: Toggle highlighting", "command": "toggle_highlight_duplicates" + }, + { + "caption": "Select Duplicates", + "command": "toggle_select_duplicates" } -] \ No newline at end of file +] diff --git a/highlight_duplicates.sublime-settings b/highlight_duplicates.sublime-settings index 70a1a96..45d0eea 100644 --- a/highlight_duplicates.sublime-settings +++ b/highlight_duplicates.sublime-settings @@ -3,5 +3,8 @@ "highlight_duplicates_color" : "invalid", // By default plugin is enabled or disabled (true|false) - "highlight_duplicates_enabled" : true + "highlight_duplicates_enabled" : true, + + // If true, whitespace gets trimmed off each line before checking for matching lines + "ignore_white_space" : true } \ No newline at end of file diff --git a/hightlight_duplicates.py b/hightlight_duplicates.py index 8351275..41cd130 100644 --- a/hightlight_duplicates.py +++ b/hightlight_duplicates.py @@ -31,7 +31,10 @@ def count_lines(lines, view): ''' counts = defaultdict(list) for line in lines: - string = view.substr(line).strip() + if bool(settings.get('ignore_white_space', DEFAULT_IS_ENABLED)): + string = view.substr(line).strip() + else: + string = view.substr(line) if is_candidate(string): counts[string].append(line) return counts @@ -67,6 +70,15 @@ def show_lines(regions, view): sublime.DRAW_OUTLINED) +def add_lines(regions, view): + '''Merges all duplicated regions in one set and highlights them.''' + all_regions = [] + for r in regions: + for i in r: + print(i) + view.sel().add(i) + + def highlight_duplicates(view): '''Main function that glues everything for hightlighting.''' # get all lines @@ -77,6 +89,17 @@ def highlight_duplicates(view): show_lines(duplicates.values(), view) + +def select_duplicates(view): + '''Main function that glues everything for hightlighting.''' + # get all lines + lines = view.lines(sublime.Region(0, view.size())) + # count and filter out non duplicated lines + duplicates = filter_counts(count_lines(lines, view)) + # select duplicated lines + add_lines(duplicates.values(), view) + + def downlight_duplicates(window): '''Removes any region highlighted by this plugin accross all views.''' for view in window.views(): @@ -122,3 +145,8 @@ def run(self): highlight_duplicates(self.window.active_view()) else: downlight_duplicates(self.window) + + +class ToggleSelectDuplicatesCommand(sublime_plugin.WindowCommand): + def run(self): + select_duplicates(self.window.active_view())