Skip to content

Commit

Permalink
Merge pull request #17 from titoBouzout/timernothreading
Browse files Browse the repository at this point in the history
Use timer instead of thread
  • Loading branch information
adzenith committed Dec 22, 2011
2 parents 92a3711 + f6c98eb commit 0dff965
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions word_highlight.py
@@ -1,7 +1,7 @@
import sublime
import sublime_plugin
import re
from threading import Timer
import time

settings = sublime.load_settings('Word Highlight.sublime-settings')

Expand All @@ -13,24 +13,14 @@ def load(self):
Pref.highlight_when_selection_is_empty = bool(settings.get('highlight_when_selection_is_empty', False))
Pref.highlight_word_under_cursor_when_selection_is_empty = bool(settings.get('highlight_word_under_cursor_when_selection_is_empty', False))
Pref.word_separators = []
Pref.timing = time.time()

Pref().load()

settings.add_on_change('color_scope_name', lambda:Pref().load())
settings.add_on_change('draw_outlined', lambda:Pref().load())
settings.add_on_change('highlight_when_selection_is_empty', lambda:Pref().load())

def delayed(seconds):
def decorator(f):
def wrapper(*args, **kargs):
if wrapper.timer:
wrapper.timer.cancel()
wrapper.timer = None
wrapper.timer = Timer(seconds, f, args, kargs)
wrapper.timer.start()
wrapper.timer = None
return wrapper
return decorator

class SelectHighlightedWordsCommand(sublime_plugin.TextCommand):
def run(self, edit):
Expand All @@ -46,11 +36,12 @@ def on_activate(self, view):

def on_selection_modified(self, view):
if not view.settings().get('is_widget'):
self.pend_highlight_occurences(view)

@delayed(Pref.selection_delay)
def pend_highlight_occurences(self, view):
sublime.set_timeout(lambda: self.highlight_occurences(view), 0)
now = time.time()
if now - Pref.timing > Pref.selection_delay:
self.highlight_occurences(view)
Pref.timing = now
else:
Pref.timing = now

def highlight_occurences(self, view):
regions = []
Expand Down

0 comments on commit 0dff965

Please sign in to comment.