Permalink
Browse files

Redo optimization for big files; track typing and count but do not co…

…unt again if only the cursor is moving; count if selection is not empty and changes
  • Loading branch information...
titoBouzout committed May 13, 2014
1 parent 5e39d8f commit cea768bca910abc0b7be8ed1deb56b60dbdf8689
Showing with 13 additions and 7 deletions.
  1. +13 −7 WordCount.py
View
@@ -20,6 +20,7 @@ class Pref:
def load(self):
Pref.view = False
Pref.modified = False
+ Pref.selection_is_not_empty = False
Pref.elapsed_time = 0.4
Pref.running = False
Pref.wrdRx = re.compile(s.get('word_regexp', "^[^\w]?\w+[^\w]*$"), re.U)
@@ -68,9 +69,15 @@ def on_activated_async(self, view):
def on_post_save_async(self, view):
self.asap(view)
- def on_selection_modified_async(self, view):
+ def on_modified_async(self, view):
Pref.modified = True
+ def on_selection_modified_async(self, view):
+ selection_is_not_empty = view.has_non_empty_selection_region()
+ if selection_is_not_empty or Pref.selection_is_not_empty:
+ Pref.modified = True
+ Pref.selection_is_not_empty = selection_is_not_empty
+
def on_close(self, view):
Pref.view = False
Pref.modified = True
@@ -144,7 +151,7 @@ def display(self, view, on_selection, word_count, char_count, word_count_line, c
read_time = ""
view.set_status('WordCount', "%s%s%s%s%s%s" % (
- self.makePlural('Word', word_count),
+ '' if not word_count else self.makePlural('Word', word_count),
char_count,
word_count_line,
chars_count_line,
@@ -168,7 +175,7 @@ def __init__(self, view, content, content_line, on_selection):
self.chars_in_line = 0
def run(self):
- #print 'running:'+str(time.time())
+ # print ('running:'+str(time.time()))
Pref.running = True
self.word_count = sum([self.count(region) for region in self.content])
@@ -193,7 +200,7 @@ def on_done(self):
def count(self, content):
- #begin = time.time()
+ begin = time.time()
#=====1
# wrdRx = Pref.wrdRx
@@ -219,7 +226,7 @@ def count(self, content):
else:
words = len([x for x in content.replace("'", '').split() if False == x.isdigit() and wrdRx(x)])
- #Pref.elapsed_time = end = time.time() - begin;
+ Pref.elapsed_time = end = time.time() - begin;
#print 'Benchmark: '+str(end)
return words
@@ -231,5 +238,4 @@ def word_count_loop():
# sleep_time becomes elapsed_time*3
if Pref.running == False:
sublime.set_timeout(lambda:word_count(), 0)
- time.sleep((Pref.elapsed_time*3 if Pref.elapsed_time > 0.4 else 0.4))
-
+ time.sleep((Pref.elapsed_time*3 if Pref.elapsed_time > 0.4 else 0.4))

0 comments on commit cea768b

Please sign in to comment.