Skip to content

Commit

Permalink
Merge pull request #8 from LordBrom/min_length_setting
Browse files Browse the repository at this point in the history
Min length setting
  • Loading branch information
LordBrom authored Feb 26, 2020
2 parents 24bb89b + 2886de0 commit ce3c64c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 3 additions & 1 deletion highlight_duplicates.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
24 changes: 17 additions & 7 deletions hightlight_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
'''
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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())
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions messages.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"1.3.0": "messages/1.3.0.txt"
}
"1.3.0": "messages/1.3.0.txt",
"1.4.0": "messages/1.4.0.txt"
}
8 changes: 8 additions & 0 deletions messages/1.4.0.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ce3c64c

Please sign in to comment.