forked from du-song/SublimeFormatSQL
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatSQL.py
More file actions
Latest commit
30 lines (26 loc) · 1.17 KB
/
Copy pathFormatSQL.py
File metadata and controls
30 lines (26 loc) · 1.17 KB
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
importsublime
importsublime_plugin
importsqlparse
classFormatSqlCommand(sublime_plugin.TextCommand):
defrun(self, edit):
view=self.view
regions=view.sel()
# if there are more than 1 region or region one and it's not empty
iflen(regions) >1ornotregions[0].empty():
forregioninview.sel():
ifnotregion.empty():
s=view.substr(region)
s=self._run(s)
view.replace(edit, region, s)
else: # format all text
alltextreg=sublime.Region(0, view.size())
s=view.substr(alltextreg)
s=self._run(s)
view.replace(edit, alltextreg, s)
def_run(self, s):
settings=self.view.settings()
#indent_char = " " if settings.get("translate_tabs_to_spaces") else "\t"
indent_char=" "# TODO indent by TAB (currently not supported in python-sqlparse)
indent_size=int(settings.get("tab_size")) ifindent_char==" "else1
s=s.encode("utf-8")
returnsqlparse.format(s, keyword_case="upper", identifier_case="lower", reindent=True, indent_width=indent_size)