From 7a02e8c2b951b2381a719497acad889b90afedc2 Mon Sep 17 00:00:00 2001 From: Nate Mills Date: Tue, 25 Feb 2020 21:33:19 -0800 Subject: [PATCH 1/5] Adding minimum length setting --- Main.sublime-menu | 56 +++++++++++++-------------- highlight_duplicates.sublime-settings | 4 +- hightlight_duplicates.py | 19 +++++---- messages.json | 5 ++- messages/1.4.0.txt | 8 ++++ 5 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 messages/1.4.0.txt diff --git a/Main.sublime-menu b/Main.sublime-menu index 9fb5180..3ffe359 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -1,28 +1,28 @@ -[ - { - "caption": "Preferences", - "mnemonic": "n", - "id": "preferences", - "children": - [{ - "caption": "Package Settings", - "mnemonic": "P", - "id": "package-settings", - "children": - [{ - "caption": "Highlight Duplicates", - "mnemonic": "H", - "children": - [{ - "caption": "-" - },{ - "command": "open_file", "args": {"file": "${packages}/Highlight Duplicates/highlight_duplicates.sublime-settings"},"caption": "Settings – Default" - },{ - "command": "open_file", "args": {"file": "${packages}/User/highlight_duplicates.sublime-settings"},"caption": "Settings – User" - },{ - "caption": "-" - }] - }] - }] - } -] +[ + { + "caption": "Preferences", + "mnemonic": "n", + "id": "preferences", + "children": + [{ + "caption": "Package Settings", + "mnemonic": "P", + "id": "package-settings", + "children": + [{ + "caption": "Highlight Duplicates", + "mnemonic": "H", + "children": + [{ + "caption": "-" + },{ + "command": "open_file", "args": {"file": "${packages}/Highlight Duplicates/highlight_duplicates.sublime-settings"},"caption": "Settings – Default" + },{ + "command": "open_file", "args": {"file": "${packages}/User/highlight_duplicates.sublime-settings"},"caption": "Settings – User" + },{ + "caption": "-" + }] + }] + }] + } +] diff --git a/highlight_duplicates.sublime-settings b/highlight_duplicates.sublime-settings index 6f0cdda..01ac559 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" : 3 } diff --git a/hightlight_duplicates.py b/hightlight_duplicates.py index 3987109..e9475e0 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 = 3 -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, 0))) # select duplicated lines merged = merge_results(duplicates.values()) merged.sort(key=lambda elm: elm.begin()) @@ -165,6 +166,10 @@ 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') + return settings.get('min_line_length', 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..bc32aaf --- /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": 3 From 5dabb66dbd43c98c064a09e76b336bb89ff0473b Mon Sep 17 00:00:00 2001 From: Nate Mills Date: Tue, 25 Feb 2020 21:55:18 -0800 Subject: [PATCH 2/5] Changing default to 4, as to not change current functionality. --- highlight_duplicates.sublime-settings | 2 +- messages/1.4.0.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/highlight_duplicates.sublime-settings b/highlight_duplicates.sublime-settings index 01ac559..91d755f 100644 --- a/highlight_duplicates.sublime-settings +++ b/highlight_duplicates.sublime-settings @@ -10,5 +10,5 @@ "ignore_case" : false, - "min_line_length" : 3 + "min_line_length" : 4 } diff --git a/messages/1.4.0.txt b/messages/1.4.0.txt index bc32aaf..6f8a1b8 100644 --- a/messages/1.4.0.txt +++ b/messages/1.4.0.txt @@ -5,4 +5,4 @@ Highlight Duplicates v1.4.0 This setting will allow you to specify how many characters a line needs to be considered as a duplicate. Default: - "min_line_length": 3 + "min_line_length": 4 From 3406e8c0e4b6576c350236a5664b3ddcbc17d641 Mon Sep 17 00:00:00 2001 From: Nate Mills Date: Tue, 25 Feb 2020 22:01:07 -0800 Subject: [PATCH 3/5] Adding validation for new setting. --- hightlight_duplicates.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hightlight_duplicates.py b/hightlight_duplicates.py index e9475e0..711c941 100644 --- a/hightlight_duplicates.py +++ b/hightlight_duplicates.py @@ -24,7 +24,7 @@ DEFAULT_COLOR_SCOPE_NAME = "invalid" DEFAULT_IS_ENABLED = True DEFAULT_IS_DISABLED = False -DEFAULT_MIN_LINE_LENGTH = 3 +DEFAULT_MIN_LINE_LENGTH = 4 def count_lines(lines, view, minLineLength): '''Counts line occurrences of a view using a hash. @@ -168,7 +168,12 @@ def getHighlightColor(): def getMinLineLength(): settings = sublime.load_settings('highlight_duplicates.sublime-settings') - return settings.get('min_line_length', DEFAULT_MIN_LINE_LENGTH) + 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): From e86c90ed8fc08a8b8ae69fb68b84c94a9aa344fa Mon Sep 17 00:00:00 2001 From: Nate Mills Date: Wed, 26 Feb 2020 07:40:47 -0800 Subject: [PATCH 4/5] Reverting line endings. --- Main.sublime-menu | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Main.sublime-menu b/Main.sublime-menu index 3ffe359..9fb5180 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -1,28 +1,28 @@ -[ - { - "caption": "Preferences", - "mnemonic": "n", - "id": "preferences", - "children": - [{ - "caption": "Package Settings", - "mnemonic": "P", - "id": "package-settings", - "children": - [{ - "caption": "Highlight Duplicates", - "mnemonic": "H", - "children": - [{ - "caption": "-" - },{ - "command": "open_file", "args": {"file": "${packages}/Highlight Duplicates/highlight_duplicates.sublime-settings"},"caption": "Settings – Default" - },{ - "command": "open_file", "args": {"file": "${packages}/User/highlight_duplicates.sublime-settings"},"caption": "Settings – User" - },{ - "caption": "-" - }] - }] - }] - } -] +[ + { + "caption": "Preferences", + "mnemonic": "n", + "id": "preferences", + "children": + [{ + "caption": "Package Settings", + "mnemonic": "P", + "id": "package-settings", + "children": + [{ + "caption": "Highlight Duplicates", + "mnemonic": "H", + "children": + [{ + "caption": "-" + },{ + "command": "open_file", "args": {"file": "${packages}/Highlight Duplicates/highlight_duplicates.sublime-settings"},"caption": "Settings – Default" + },{ + "command": "open_file", "args": {"file": "${packages}/User/highlight_duplicates.sublime-settings"},"caption": "Settings – User" + },{ + "caption": "-" + }] + }] + }] + } +] From 2886de064da824404ad109e4455fadf18a29e98b Mon Sep 17 00:00:00 2001 From: Nate Mills Date: Wed, 26 Feb 2020 07:42:12 -0800 Subject: [PATCH 5/5] Updating remove duplicate function to use same min line length as other functions. --- hightlight_duplicates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hightlight_duplicates.py b/hightlight_duplicates.py index 711c941..746aa77 100644 --- a/hightlight_duplicates.py +++ b/hightlight_duplicates.py @@ -134,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, 0))) + 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())