Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Redo optimization for big files; track typing and count but do not co…
Browse files Browse the repository at this point in the history
…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 cea768b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions WordCount.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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])
Expand All @@ -193,7 +200,7 @@ def on_done(self):

def count(self, content):

#begin = time.time()
begin = time.time()

#=====1
# wrdRx = Pref.wrdRx
Expand All @@ -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
Expand All @@ -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.