Skip to content

Commit

Permalink
Enable highlight on_modified
Browse files Browse the repository at this point in the history
  • Loading branch information
kdnk committed Mar 19, 2019
1 parent 1690416 commit b96d84a
Showing 1 changed file with 53 additions and 39 deletions.
92 changes: 53 additions & 39 deletions highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
import re
from collections import OrderedDict

COLORS_BY_SCOPE = OrderedDict() # TODO: Make color configurable
COLORS_BY_SCOPE['markup.changed.git_gutter'] = None # vivid purple
COLORS_BY_SCOPE['support.class'] = None # yellow
COLORS_BY_SCOPE['markup.deleted.git_gutter'] = None # vivid pink
COLORS_BY_SCOPE['markup.inserted.git_gutter'] = None # vivid green
COLORS_BY_SCOPE['constant.numeric'] = None # orange
COLORS_BY_SCOPE['constant.character.escape'] = None # light blue
COLORS_BY_SCOPE['variable'] = None # red
COLORS_BY_SCOPE['string'] = None # light green
COLORS_BY_SCOPE['comment'] = None # glay

class TextHighlighterToggleCommand(sublime_plugin.WindowCommand):
def __init__(self, window):
super().__init__(window)
if not window.project_data():
window.set_project_data(init_color())

# TODO: run this command when new tab is opened
def run(self):
active_view = self.window.active_view()
Expand All @@ -28,46 +22,45 @@ def run(self):
sel_string = active_view.substr(word_region)

views = self.window.views()
if is_highlighted(sel_string):
color = find_used_color(sel_string)
if is_highlighted(self.window, sel_string):
color = find_used_color(self.window, sel_string)
for view in views:
eraser(view, sel_string, color)
else:
color = find_usable_color(sel_string)
color = find_usable_color(self.window, sel_string)
for view in views:
highlighter(view, sel_string, color)
print('COLORS_BY_SCOPE1: ', COLORS_BY_SCOPE)



class TextHighlighterClearAllCommand(sublime_plugin.WindowCommand):
def run(self):
window =self.window
views = self.window.views()
for sel_string in COLORS_BY_SCOPE.values():
colors_by_scope = window.project_data()
for sel_string in colors_by_scope.values():
if sel_string:
color = find_used_color(sel_string)
color = find_used_color(window, sel_string)
if color:
for view in views:
eraser(view, sel_string, color)

class HighlighterCommand(sublime_plugin.EventListener):
def on_pre_save(self, view):
print('------------------ START ------------------')
print('COLORS_BY_SCOPE2: ', COLORS_BY_SCOPE)
for color, sel_string in COLORS_BY_SCOPE.items():
if color and sel_string:
highlighter(view, sel_string, color)
print('COLORS_BY_SCOPE3: ', COLORS_BY_SCOPE)
print('------------------ END ------------------')

def on_modified(self, view):
window = view.window()
colors_by_scope = window.project_data()
views = window.views()
for view in views:
for color, sel_string in colors_by_scope.items():
if color and sel_string:
highlighter(view, sel_string, color)

def highlighter(view, sel_string, color):
print('COLORS_BY_SCOPE4: ', COLORS_BY_SCOPE)
window = view.window()
colors_by_scope = window.project_data()
regions = find_all(view, sel_string)
print('regions3: ', regions)
if color and regions:
if not COLORS_BY_SCOPE[color]:
COLORS_BY_SCOPE[color] = sel_string
if not colors_by_scope[color]:
colors_by_scope[color] = sel_string
window.set_project_data(colors_by_scope)
view.add_regions(
sel_string,
regions,
Expand All @@ -77,33 +70,54 @@ def highlighter(view, sel_string, color):
)

def eraser(view, sel_string, color):
window = view.window()
colors_by_scope = window.project_data()

regions = find_all(view, sel_string)
COLORS_BY_SCOPE[color] = None
colors_by_scope[color] = None
window.set_project_data(colors_by_scope)
view.erase_regions(sel_string)

def find_all(view, sel_string):
return view.find_all(re.escape(sel_string))

def is_highlighted(sel_string):
def is_highlighted(window, sel_string):
colors_by_scope = window.project_data()

highlighted = False
for key, value in COLORS_BY_SCOPE.items():
for key, value in colors_by_scope.items():
if value == sel_string:
highlighted = True
break
return highlighted

def find_used_color(sel_string):
def find_used_color(window, sel_string):
colors_by_scope = window.project_data()
color = None
for key, value in COLORS_BY_SCOPE.items():
for key, value in colors_by_scope.items():
if value == sel_string:
color = key
return color

def find_usable_color(sel_string):
def find_usable_color(window, sel_string):
colors_by_scope = window.project_data()
color = None
for key, value in COLORS_BY_SCOPE.items():
for key, value in colors_by_scope.items():
if not value:
color = key
break
return color

def init_color():
colors_by_scope = OrderedDict() # TODO: Make color configurable
colors_by_scope['markup.changed.git_gutter'] = None # vivid purple
colors_by_scope['support.class'] = None # yellow
colors_by_scope['markup.deleted.git_gutter'] = None # vivid pink
colors_by_scope['markup.inserted.git_gutter'] = None # vivid green
colors_by_scope['constant.numeric'] = None # orange
colors_by_scope['constant.character.escape'] = None # light blue
colors_by_scope['variable'] = None # red
colors_by_scope['string'] = None # light green
colors_by_scope['comment'] = None # glay
return colors_by_scope

0 comments on commit b96d84a

Please sign in to comment.