Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
adzenith (
ajpalkovic (
Anomareh (
'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:
col = self.getColumn(view, next.end())
if col > self.getColumn(view, sel.begin()):
last = next
if last is not None:
cursors['col'].append(self.getColumn(view, last.end()))
if sel.empty():
cursors['row'].append(self.getRow(view, last.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 =
if not id in self.views:
data = self.views[id] = {
'guides': [],
'settings': {}
self.updateSettings(view, data)
return data
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:
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']:
if data['settings']['hide_in_selection'] and view.sel().contains(guide):
if self.getColumn(view, guide.a) in cursors['col']:
view.add_regions('IndentGuides', inactiveGuides, data['settings']['scope'], sublime.DRAW_EMPTY)
view.add_regions('ActiveIndentGuides', activeGuides, data['settings']['active_scope'], sublime.DRAW_EMPTY)
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):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment