Skip to content

Commit

Permalink
Updated setting functionality
Browse files Browse the repository at this point in the history
Instead of checking the users settings once, the users settings are checked every time the event listeners are triggered, or the toggle command is used.
Using the toggle command will update the users settings.
  • Loading branch information
LordBrom authored Oct 20, 2017
1 parent 1ded0a2 commit 9312e62
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions hightlight_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
DEFAULT_COLOR_SCOPE_NAME = "invalid"
DEFAULT_IS_ENABLED = True

#Set whether the plugin is on or off
settings = sublime.load_settings('highlight_duplicates.sublime-settings')
plugin_enabled = bool(settings.get('highlight_duplicates_enabled', DEFAULT_IS_ENABLED))


def count_lines(lines, view):
'''Counts line occurrences of a view using a hash.
The lines are stripped and tested before count.
Expand Down Expand Up @@ -101,10 +96,20 @@ def select_duplicates(view):
add_lines(duplicates.values(), view)


def downlight_duplicates(window):
def downlight_duplicates(view):
'''Removes any region highlighted by this plugin accross all views.'''
for view in window.views():
view.erase_regions('DuplicatesHighlightListener')
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))


class HighlightDuplicatesCommand(sublime_plugin.WindowCommand):
Expand All @@ -114,7 +119,7 @@ class HighlightDuplicatesCommand(sublime_plugin.WindowCommand):
def run(self):
# If toggling on, go ahead and perform a pass,
# else clear the highlighting in all views
if plugin_enabled:
if isEnabled():
highlight_duplicates(self.window.active_view())
else:
downlight_duplicates(self.window)
Expand All @@ -123,29 +128,35 @@ def run(self):
class DuplicatesHighlightListener(sublime_plugin.EventListener):
'''Handles sone events to automatically run the command.'''
def on_modified(self, view):
if plugin_enabled:
if isEnabled():
highlight_duplicates(view)
else:
downlight_duplicates(view)

def on_activated(self, view):
if plugin_enabled:
if isEnabled():
highlight_duplicates(view)
else:
downlight_duplicates(view)

def on_load(self, view):
if plugin_enabled:
if isEnabled():
highlight_duplicates(view)
else:
downlight_duplicates(view)


class ToggleHighlightDuplicatesCommand(sublime_plugin.WindowCommand):
def run(self):
global plugin_enabled
plugin_enabled = False if plugin_enabled else True

# If toggling on, go ahead and perform a pass,
# else clear the highlighting in all views
if plugin_enabled:
highlight_duplicates(self.window.active_view())
if isEnabled():
update_settings(False)
downlight_duplicates(self.window.active_view())
else:
downlight_duplicates(self.window)
update_settings(True)
highlight_duplicates(self.window.active_view())


class ToggleSelectDuplicatesCommand(sublime_plugin.WindowCommand):
Expand Down

0 comments on commit 9312e62

Please sign in to comment.