public
Created

Stripe highlighting plugin for Sublime Text

  • Download Gist
stripe_highlight.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
"""
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.