Skip to content

Instantly share code, notes, and snippets.

@HelloZeroNet
Last active August 29, 2015 14:23
Show Gist options
  • Save HelloZeroNet/e6343d9ee8e9968ac54e to your computer and use it in GitHub Desktop.
Save HelloZeroNet/e6343d9ee8e9968ac54e to your computer and use it in GitHub Desktop.
Detect tab or space indent for Sublime text
import sublime, sublime_plugin, re
class ExampleCommand(sublime_plugin.EventListener):
def on_activated(self, view):
self.on_load(view)
def on_load(self, view):
settings = view.settings()
if settings.get("indent_detected"):
return
settings.set("indent_detected", True)
start = 0
for i in range(10): # Probably not a code if no indent in first 10k
buff = (view.substr(sublime.Region(start, start+1024)))
match = re.search("^( |\t).*", buff, re.M)
if match:
tab_indented = match.group(1) == "\t"
print("Tab indeneted:", tab_indented, repr(match.group(1)))
if tab_indented:
settings.set('translate_tabs_to_spaces', False)
else:
settings.set('translate_tabs_to_spaces', True)
break
if not buff:
break
start += 1024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment