public
Created

  • Download Gist
indentGuides.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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
'''
[CREDITS]
authors:
adzenith (https://github.com/adzenith)
ajpalkovic (https://github.com/ajpalkovic)
Anomareh (https://github.com/Anomareh)
url:
https://gist.github.com/1136448
 
[OPTIONS]
'indent_guides_enabled': true
Enables indent guides.
'indent_guides_max_file_characters': 524288
The max file size a file is allowed to be and have indent guides.
'indent_guides_scope': 'comment'
The scope name assigned to guides.
'indent_guides_active_scope': 'indent_guides_scope'
The scope name assigned to active guides.
[NOTE] To enable active guides you have to set this option. Active guides
default to the same scope as inactive guides.
'indent_guides_hide_in_selection': true
Indent guides will be hidden within selections.
'indent_guides_hide_on_line': true
Indent guides will not be drawn on the current line.
[NOTE] This only applies to empty selections.
'indent_guides_active_wrap_to_text': true
If true, the active guide will always be the deepest guide on the cursor's line.
If false, the active guide will be the guide closest to the left of the cursor.
'''
 
import indentation
import sublime
from sublime_plugin import EventListener
 
 
class IndentGuides(EventListener):
views = {}
def getAbsolute(self, view, row, col):
return view.text_point(row, col)
def getColumn(self, view, point):
return view.rowcol(point)[1]
def getCursors(self, view):
data = self.getData(view)
cursors = {
'abs': [],
'col': [],
'row': []
}
for sel in view.sel():
row = self.getRow(view, sel.begin())
ws = view.find(r'^[ \t]*', self.getAbsolute(view, row, 0))
if not data['settings']['active_wrap_to_text'] and sel.begin() < ws.end():
col = 0
last = None
while col < self.getColumn(view, sel.begin()):
next = view.find(r'(?: {{{0}}}|\t|$)'.format(data['settings']['tab_size']), self.getAbsolute(view, row, col))
if next is None:
break
col = self.getColumn(view, next.end())
if col > self.getColumn(view, sel.begin()):
break
else:
last = next
if last is not None:
cursors['abs'].append(last.end())
cursors['col'].append(self.getColumn(view, last.end()))
if sel.empty():
cursors['row'].append(self.getRow(view, last.end()))
else:
cursors['abs'].append(ws.end())
cursors['col'].append(self.getColumn(view, ws.end()))
if sel.empty():
cursors['row'].append(self.getRow(view, ws.end()))
return cursors
def getData(self, view):
id = view.id()
if not id in self.views:
data = self.views[id] = {
'guides': [],
'settings': {}
}
self.updateSettings(view, data)
return data
else:
return self.views[id]
def getRow(self, view, point):
return view.rowcol(point)[0]
def updateGuides(self, view, data):
data['guides'] = []
regions = view.find_all(r'^(?: {{{0}}}|\t)+(?=[ \t]|$)'.format(data['settings']['tab_size']))
for region in regions:
end = self.getColumn(view, region.end())
row = self.getRow(view, region.begin())
col = 0
while col < end:
start = self.getAbsolute(view, row, col)
next = view.find(r'(?: {{{0}}}|\t)'.format(data['settings']['tab_size']), start)
if next is None:
break
else:
data['guides'].append(sublime.Region(next.end(), next.end()))
col = self.getColumn(view, next.end())
def updateSettings(self, view, data):
settings = view.settings()
data['settings']['enabled'] = settings.get('indent_guides_enabled', True)
data['settings']['max_chars'] = settings.get('indent_guides_max_file_characters', 524288)
data['settings']['tab_size'] = indentation.get_tab_size(view)
data['settings']['scope'] = settings.get('indent_guides_scope', 'comment')
data['settings']['active_scope'] = settings.get('indent_guides_active_scope', data['settings']['scope'])
data['settings']['hide_in_selection'] = settings.get('indent_guides_hide_in_selection', True)
data['settings']['hide_on_line'] = settings.get('indent_guides_hide_on_line', True)
data['settings']['active_wrap_to_text'] = settings.get('indent_guides_active_wrap_to_text', True)
if data['settings']['enabled'] and settings.get('draw_indent_guides', False):
settings.set('draw_indent_guides', False)
def refresh(self, view, update = False):
data = self.getData(view)
if data['settings']['enabled'] and view.size() < data['settings']['max_chars']:
cursors = self.getCursors(view)
activeGuides = []
inactiveGuides = []
if update:
self.updateGuides(view, data)
for guide in data['guides']:
if data['settings']['hide_on_line'] and self.getRow(view, guide.a) in cursors['row']:
continue
if data['settings']['hide_in_selection'] and view.sel().contains(guide):
continue
if self.getColumn(view, guide.a) in cursors['col']:
activeGuides.append(guide)
else:
inactiveGuides.append(guide)
view.add_regions('IndentGuides', inactiveGuides, data['settings']['scope'], sublime.DRAW_EMPTY)
view.add_regions('ActiveIndentGuides', activeGuides, data['settings']['active_scope'], sublime.DRAW_EMPTY)
else:
view.erase_regions('IndentGuides')
view.erase_regions('ActiveIndentGuides')
def on_activated(self, view):
self.updateSettings(view, self.getData(view))
def on_load(self, view):
self.refresh(view, True)
def on_modified(self, view):
self.refresh(view, True)
def on_selection_modified(self, view):
self.refresh(view)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.