Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Stripe highlighting plugin for Sublime Text
"""
This simple and "dirty" plugin highlights specified columns visually by
splitting the buffer into columns which can be colored according to `sections`.
To enable this "stripe highlighting" create a keymapping for the
"toggle_stripe_highlight" command.
http://www.sublimetext.com/forum/viewtopic.php?f=2&t=12111
"""
import sublime
import sublime_plugin
# Modify these values!
splits = [4, 10, 20, 22]
# splits = [76, 112, 556, 562]
sections = ["", "string"]
#############################################################
# DON'T EDIT BELOW HERE (if you don't know what you're doing)
#############################################################
SETTINGS_KEY = "stripe_highlight"
REGIONS_NAME = SETTINGS_KEY + '_%s'
# validate splits (as in, this is optional but needed for the code)
splits.sort()
if splits[0] != 0:
splits.insert(0, 0)
if splits[-1] != -1:
splits.append(-1)
######### Parts from sublime_lib.view ###########
def rowcount(view):
return view.rowcol(view.size())[0] + 1
def rowwidth(view, row):
return view.rowcol(view.line(view.text_point(row, 0)).end())[1] + 1
def relative_point(view, row=0, col=0, p=None):
if p is not None:
if len(p) != 2:
raise TypeError("Coordinates have 2 dimensions, not %d" % len(p))
(row, col) = p
# shortcut
if row == -1 and col == -1:
return view.size()
# calc absolute coords and check if coords are in the bounds
rowc = rowcount(view)
if row < 0:
row = max(rowc + row, 0)
else:
row = min(row, rowc - 1)
roww = rowwidth(view, row)
if col < 0:
col = max(roww + col, 0)
else:
col = min(col, roww - 1)
return view.text_point(row, col)
def coorded_region(view, reg1=None, reg2=None, rel=None):
reg1 = reg1 or (0, 0)
if rel:
reg2 = (reg1[0] + rel[0], reg1[1] + rel[1])
else:
reg2 = reg2 or (-1, -1)
p1 = relative_point(view, p=reg1)
p2 = relative_point(view, p=reg2)
return sublime.Region(p1, p2)
######### Actual stuff ################
class SplitSectionsListener(sublime_plugin.EventListener):
def on_modified_async(self, view):
enabled = view.settings().get(SETTINGS_KEY, False)
if not enabled:
if view.get_regions(REGIONS_NAME % sections[0]):
for sel in sections:
view.erase_regions(REGIONS_NAME % sel)
return
regions = dict()
for sel in sections:
regions[sel] = []
for i in range(rowcount(view)):
l = rowwidth(view, i)
for j, p in enumerate(splits):
# Don't cycle the first
if not j:
continue
sel = sections[(j - 1) % len(sections)]
# print(i, j, sel, p)
regions[sel].append(coorded_region(view, (i, splits[j - 1]), (i, p)))
# Do not iterate another time if this one already exceeded the line length
if (p + 1) > l:
break
for k, v in regions.items():
view.add_regions(REGIONS_NAME % k, v, k)
def on_modified(self, view):
# Run async manually with ST2
if int(sublime.version()) < 3000:
sublime.set_timeout(lambda: self.on_modified_async(view), 0)
class ToggleStripeHighlightCommand(sublime_plugin.TextCommand):
def run(self, edit):
print("toggled")
s = self.view.settings()
s.set(SETTINGS_KEY, not s.get(SETTINGS_KEY, False))
# TODO: run command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.