diff --git a/highlight_duplicates.sublime-settings b/highlight_duplicates.sublime-settings index 6f0cdda..91d755f 100644 --- a/highlight_duplicates.sublime-settings +++ b/highlight_duplicates.sublime-settings @@ -8,5 +8,7 @@ // If true, whitespace gets trimmed off each line before checking for matching lines "trim_white_space" : true, - "ignore_case" : false + "ignore_case" : false, + + "min_line_length" : 4 } diff --git a/hightlight_duplicates.py b/hightlight_duplicates.py index 3987109..746aa77 100644 --- a/hightlight_duplicates.py +++ b/hightlight_duplicates.py @@ -24,8 +24,9 @@ DEFAULT_COLOR_SCOPE_NAME = "invalid" DEFAULT_IS_ENABLED = True DEFAULT_IS_DISABLED = False +DEFAULT_MIN_LINE_LENGTH = 4 -def count_lines(lines, view): +def count_lines(lines, view, minLineLength): '''Counts line occurrences of a view using a hash. The lines are stripped and tested before count. ''' @@ -36,7 +37,7 @@ def count_lines(lines, view): string = string.strip() if ignoreCase(): string = string.lower() - if is_candidate(string): + if is_candidate(string, minLineLength): counts[string].append(line) return counts @@ -73,11 +74,11 @@ def merge_results(countsList): return merged -def is_candidate(string): +def is_candidate(string, minLineLength): '''Tells if a string is a LOC candidate. A candidate is a string long enough after stripping some symbols. ''' - return len(string.strip('{}()[]/')) > 3 + return len(string.strip('{}()[]/')) >= minLineLength def show_lines(regions, view): @@ -111,7 +112,7 @@ def highlight_duplicates(view): # 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)) + duplicates = filter_counts(count_lines(lines, view, getMinLineLength())) # show duplicated lines show_lines(duplicates.values(), view) @@ -122,7 +123,7 @@ def select_duplicates(view): # 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)) + duplicates = filter_counts(count_lines(lines, view, getMinLineLength())) # select duplicated lines add_lines(duplicates.values(), view) @@ -133,7 +134,7 @@ def remove_duplicates(view, edit): # get all lines lines = view.lines(sublime.Region(0, view.size())) # count and filter out non duplicated lines - duplicates = remove_first(filter_counts(count_lines(lines, view))) + duplicates = remove_first(filter_counts(count_lines(lines, view, getMinLineLength()))) # select duplicated lines merged = merge_results(duplicates.values()) merged.sort(key=lambda elm: elm.begin()) @@ -165,6 +166,15 @@ 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 + + class HighlightDuplicatesCommand(sublime_plugin.WindowCommand): '''Actual Sublime command. Run it in the console with: diff --git a/messages.json b/messages.json index e0d969e..4132982 100644 --- a/messages.json +++ b/messages.json @@ -1,3 +1,4 @@ { - "1.3.0": "messages/1.3.0.txt" -} \ No newline at end of file + "1.3.0": "messages/1.3.0.txt", + "1.4.0": "messages/1.4.0.txt" +} diff --git a/messages/1.4.0.txt b/messages/1.4.0.txt new file mode 100644 index 0000000..6f8a1b8 --- /dev/null +++ b/messages/1.4.0.txt @@ -0,0 +1,8 @@ +Highlight Duplicates v1.4.0 + + • Added Minimum Length Setting + + This setting will allow you to specify how many characters a line needs to be considered as a duplicate. + + Default: + "min_line_length": 4