diff --git a/highlight_duplicates.sublime-settings b/highlight_duplicates.sublime-settings index 2ae0654..daf2557 100644 --- a/highlight_duplicates.sublime-settings +++ b/highlight_duplicates.sublime-settings @@ -11,12 +11,15 @@ // If true, letter casing is ignored for checking for matching lines "ignore_case" : false, - // The minimun length a line needs to be in order to be included as a matching line + // The minimum length a line needs to be in order to be included as a matching line "min_line_length" : 4, // The minimum number of duplicate lines needed to be found, in order to be included as a matching line "min_duplicate_count" : 1, // Lines that match entries in this list are ignored as matching lines - "ignore_list" : [] + "ignore_list" : [], + + // If there is anything selected, that will be used instead of the whole file + "use_selection" : false } diff --git a/hightlight_duplicates.py b/hightlight_duplicates.py index d46dc89..b99b520 100644 --- a/hightlight_duplicates.py +++ b/hightlight_duplicates.py @@ -26,6 +26,8 @@ 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. @@ -48,7 +50,7 @@ def filter_counts(counts): lower or equal to the "min_duplicate_count" user setting, which defaults to 1. ''' filtered = dict() - threshold = getMinDuplicateCount(); + threshold = getMinDuplicateCount() for k, v in counts.items(): if len(v) > threshold: filtered[k] = v @@ -94,8 +96,8 @@ def show_lines(regions, view): all_regions.extend(r) color_scope_name = getHighlightColor() view.add_regions('DuplicatesHighlightListener', - all_regions, color_scope_name, '', - sublime.DRAW_OUTLINED) + all_regions, color_scope_name, '', + sublime.DRAW_OUTLINED) def add_lines(regions, view): @@ -111,32 +113,43 @@ def remove_lines(regions, view, edit): 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 = view.lines(sublime.Region(0, view.size())) + 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 = view.lines(sublime.Region(0, view.size())) + 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 = view.lines(sublime.Region(0, view.size())) + lines = get_lines(view, useSelection) # count and filter out non duplicated lines duplicates = remove_first(filter_counts(count_lines(lines, view))) # select duplicated lines @@ -146,30 +159,36 @@ def remove_duplicates(view, edit): def downlight_duplicates(view): - '''Removes any region highlighted by this plugin accross all views.''' + '''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) @@ -178,14 +197,17 @@ def getMinLineLength(): 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) + 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', []) @@ -195,11 +217,16 @@ def getIgnoreList(): 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 @@ -211,6 +238,7 @@ def run(self): class DuplicatesHighlightListener(sublime_plugin.EventListener): '''Handles sone events to automatically run the command.''' + def on_modified(self, view): if isEnabled(): highlight_duplicates(view) diff --git a/messages.json b/messages.json index 4132982..37af027 100644 --- a/messages.json +++ b/messages.json @@ -1,4 +1,6 @@ { "1.3.0": "messages/1.3.0.txt", - "1.4.0": "messages/1.4.0.txt" + "1.4.0": "messages/1.4.0.txt", + "1.6.0": "messages/1.6.0.txt", + "1.7.0": "messages/1.7.0.txt" } diff --git a/messages/1.7.0.txt b/messages/1.7.0.txt new file mode 100644 index 0000000..a44219d --- /dev/null +++ b/messages/1.7.0.txt @@ -0,0 +1,10 @@ +Highlight Duplicates v1.7.0 + + • Added Use Selected Content Setting + + Option to make Select Duplicates and Remove Duplicates only affect selected lines. + + Default: + "use_selection": false + + diff --git a/readme.md b/readme.md index 9477bca..e3904ee 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,6 @@ **Highlight duplicated lines.** This is a [Sublime Text 3](http://www.sublimetext.com/3) (and ST2) plugin. - ## Installation **Using Package Control** ([installation instructions](https://packagecontrol.io/installation)) @@ -11,8 +10,7 @@ This is a [Sublime Text 3](http://www.sublimetext.com/3) (and ST2) plugin. Press ctrl+shift+p (cmd+shift+p for OSX), then use the 'Package Control: Install Package' command. Search for 'HighlightDuplicates', and press enter to install. - -**Manually** +### Manually Go to your `Packages` subdirectory under ST3's data directory: @@ -25,41 +23,39 @@ Then clone this repository: git clone git://github.com/lordbrom/HighlightDuplicates.git - That's it! - ## Commands ### Toggle Highlighting Using this command will turn the plugin on and off. This allows you to see the highlighted only when you want without having to disable/enable to plugin via the Package Control. - ### Select Duplicates Using this command will select the rows that would be highlighted when Highlight Duplicates is toggled on. The duplicate rows do not need to be highlighted in order for this command to work. - ### Remove Duplicates Using this command will remove all duplicate lines, after the first instance, from the file. This command is similar to the build in "Permute Lines > Unique" function found in the Edit menu, however this will follow the plugin's settings for what is considered a duplicate. For example... + ``` html 1: 2: Content 3: 4: ``` + Would result in + ``` html -1: -2: Content -3: + 1: + 2: Content + 3: ``` - _Note:_ By default none of these commands have a key binding, and can only be used via the Command Palette. You can set a key binding by adding any of the following lines to the key binding file. (Preferences > Key Bindings) ``` js @@ -71,6 +67,7 @@ _Note:_ By default none of these commands have a key binding, and can only be us ## Options ### Change The Highlighting Color + `highlight_duplicates_color: "invalid"` The highlighting color can be changed by providing a scope name such @@ -79,42 +76,47 @@ as "invalid", "comment", etc... If you'd like to use a custom color, it should be defined as a color scope in your theme file. - ### Trim White Space + `trim_white_space: true` If this setting is true, the leading and trailing white space will be removed before being compared to other lines. This setting also affects which lines are selected when using the 'Select Duplicates' command. For example, if `"trim_white_space" : true` the following 2 lines will be counted as duplicates. + ``` html 1: 2: ``` + However, the following lines would not be counted as duplicates. The reason for this is because there is white space in line 1 that is not leading or trailing, which does not appear in line 2. + ``` html 1: 2: ``` - ### Ignore Case + `ignore_case: false` If this setting is true, upper and lower case letters will be considered the same. This setting also affects which lines are selected when using the 'Select Duplicates' command. For example, if `"ignore_case" : true` the following 2 lines will be counted as duplicates. + ``` html 1: 2: ``` - ### Min Line Length + `min_line_length: 4` Lines with fewer characters than specified in this setting, will be ignored for all functions. Setting this to 1 will cause all non empty lines to be possible duplicates. For example, by default, only lines 7 and 8 will be selected when using the "select duplicate" command. If this setting is set to 2, all the lines except lines 1 and 2 will be selected when using the "select duplicate" command. + ``` html 1: 1 2: 1 @@ -126,9 +128,8 @@ For example, by default, only lines 7 and 8 will be selected when using the "sel 8: 1234 ``` - - ### Min Duplicate Count + `min_duplicate_count: 1` The number of matching lines, beyond the first, that need to be found in order to be counted as duplicates. @@ -143,19 +144,23 @@ For example, setting this option to `2`, will make it so only lines 3-5 are high 5: this ``` - - ### Ignore List + `ignore_list: []` Lines matching entires in this list, will be ignored for all functions. Leading and trailing white space, as well as letter case, will be ignored when checking lines against the ignore list. -` - "ignore_list": ["This line will be ignored"] -` +`"ignore_list": ["This line will be ignored"]` + ``` html 1: This line will be ignored 2: This line will be ignored 3: This line will not be ignored 4: This line will not be ignored ``` + +### Use Selection + +`use_selection: false` + +If set to true, the "Select Duplicate" and "Remove Duplicate" commands will only use lines that have selected content. If nothing is selected or the option is set to false, the entire document will be included.