forked from sal0max/sublime-unifa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indent_traceufa.py
41 lines (33 loc) · 1.38 KB
/
indent_traceufa.py
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
import sublime, sublime_plugin, re
from os.path import basename, splitext
class IndentUnifatraceCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
regions = view.sel()
# if there are more than 1 region or region one and it's not empty
if len(regions) > 1 or not regions[0].empty():
for region in view.sel():
if not region.empty():
s = view.substr(region)
s = self.indent(s)
view.replace(edit, region, s)
# format all text
else:
alltextreg = sublime.Region(0, view.size())
s = view.substr(alltextreg).strip()
s = self.indent(s)
view.replace(edit, alltextreg, s)
def indent(self, s):
return re.sub(r"(?<=.{79}[^}])\n\ {6}", "", s)
def is_enabled(self):
"""
Enables or disables the 'indent' command. Command will be disabled if
there are currently no text selections and current file is not 'traceufa'
or 'Plain Text'. This helps clarify to the user about when the command can
be executed, especially useful for UI controls.
"""
if self.view is None:
return False
syntax = self.view.settings().get('syntax')
language = splitext(basename(syntax))[0].lower() if syntax is not None else "plain text"
return ((language == "traceufa") or (language == "plain text"))