- Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFormatSQL.py
More file actions
Latest commit
36 lines (32 loc) · 1.27 KB
/
Copy pathFormatSQL.py
File metadata and controls
36 lines (32 loc) · 1.27 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
31
32
33
34
35
36
from __future__ importabsolute_import
importsublime
importsublime_plugin
try:
from .sqlparseimportformat
exceptValueError:
fromsqlparseimportformat
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")
returnformat(
s, keyword_case="upper", reindent=True, indent_width=indent_size
)