Skip to content

Commit

Permalink
Fixed highlight lost after view reload
Browse files Browse the repository at this point in the history
The colors are not reapplied when the file is reloaded
seanliang#32
  • Loading branch information
evandrocoan committed Jul 23, 2019
1 parent d12002d commit a4c2b5c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions HighlightWords.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import functools

import pushdown
import threading

from debug_tools import getLogger

Expand Down Expand Up @@ -117,11 +118,16 @@ def on_change(self, text):
sublime.set_timeout(lambda: self.highlight(text, stamp), 500)

def highlight(self, text, stamp):
# print('highlight text', text)
# print('highlight stamp', stamp)
if self.stamp != stamp:
return

self.window.run_command('unhighlight_words')
view = self.window.active_view()
words = self.get_words(text)
# print('highlight words', words)

regions = []
size = 0
flag = 0
Expand All @@ -137,8 +143,10 @@ def highlight(self, text, stamp):
regions = view.find_all(word, flag)
view.add_regions('highlight_word_%d' % size, regions, SCOPES[size % len(SCOPES)] , '', sublime.HIDE_ON_MINIMAP)
size += 1

view.settings().set('highlight_size', size)
view.settings().set('highlight_text', text)
# print('highlight end')

def on_cancel(self):
self.window.run_command('unhighlight_words')
Expand Down Expand Up @@ -177,8 +185,34 @@ def on_done(self, selected):
settings.set('colors_by_scope', SCOPES)
sublime.save_settings('HighlightWords.sublime-settings')


class HighlightKeywordsCommand(sublime_plugin.EventListener):

def on_post_window_command(self, window, command_name, args):
# print('HighlightKeywordsCommand', command_name)

if command_name in ("reload_current_view_refresh", "revert", "fix_revert"):

def delayedFix():
time.sleep(1)
# print('delayedFix running...')

window = sublime.active_window()
view = window.active_view()

highlighter = HighlightWordsCommand( window )
highlighter.view = view
highlighter.view_text = view.substr( sublime.Region( 0, view.size() ) )

highlight_text = view.settings().get('highlight_text', '')
# print('highlight_text', highlight_text)

highlighter.stamp = 1
highlighter.highlight( highlight_text, 1 )

# print('Fixing word highlight', command_name, "...")
threading.Thread( target=delayedFix ).start()

def handleTimeout(self, view, stamp):
if self.stamp != stamp:
return
Expand Down

0 comments on commit a4c2b5c

Please sign in to comment.