Skip to content

Commit 8931f6c

Browse files
committed
Added Sublime Text 3 support.
1 parent 09126ff commit 8931f6c

12 files changed

Lines changed: 94 additions & 72 deletions

File tree

FormatSQL.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1+
from __future__ import absolute_import
12
import sublime
23
import sublime_plugin
3-
import re
4-
from os.path import basename
5-
import sqlparse
4+
5+
try:
6+
from .sqlparse import format
7+
except ValueError:
8+
from sqlparse import format
9+
610

711
class FormatSqlCommand(sublime_plugin.TextCommand):
812
def run(self, edit):
9-
view = self.view
10-
regions = view.sel()
11-
# if there are more than 1 region or region one and it's not empty
12-
if len(regions) > 1 or not regions[0].empty():
13-
for region in view.sel():
14-
if not region.empty():
15-
s = view.substr(region)
16-
s = self._run(s)
17-
view.replace(edit, region, s)
18-
else: #format all text
19-
alltextreg = sublime.Region(0, view.size())
20-
s = view.substr(alltextreg)
21-
s = self._run(s)
22-
view.replace(edit, alltextreg, s)
13+
view = self.view
14+
regions = view.sel()
15+
# if there are more than 1 region or region one and it's not empty
16+
if len(regions) > 1 or not regions[0].empty():
17+
for region in view.sel():
18+
if not region.empty():
19+
s = view.substr(region)
20+
s = self._run(s)
21+
view.replace(edit, region, s)
22+
else: # format all text
23+
alltextreg = sublime.Region(0, view.size())
24+
s = view.substr(alltextreg)
25+
s = self._run(s)
26+
view.replace(edit, alltextreg, s)
2327

24-
def _run(self, s):
25-
settings = self.view.settings()
26-
#indent_char = " " if settings.get("translate_tabs_to_spaces") else "\t"
27-
indent_char = " " #TODO indent by TAB (currently not supported in python-sqlparse)
28-
indent_size = int(settings.get("tab_size")) if indent_char == " " else 1
29-
s = s.encode("utf-8")
30-
return sqlparse.format(s, keyword_case="upper", reindent=True, indent_width=indent_size)
28+
def _run(self, s):
29+
settings = self.view.settings()
30+
#indent_char = " " if settings.get("translate_tabs_to_spaces") else "\t"
31+
indent_char = " " #TODO indent by TAB (currently not supported in python-sqlparse)
32+
indent_size = int(settings.get("tab_size")) if indent_char == " " else 1
33+
s = s.encode("utf-8")
34+
return format(
35+
s, keyword_case="upper", reindent=True, indent_width=indent_size
36+
)

sqlparse/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# the BSD License: http://www.opensource.org/licenses/bsd-license.php.
55

66
"""Parse SQL statements."""
7-
7+
from __future__ import absolute_import
88

99
__version__ = '0.1.3'
1010

@@ -14,9 +14,9 @@ class SQLParseError(Exception):
1414

1515

1616
# Setup namespace
17-
from sqlparse import engine
18-
from sqlparse import filters
19-
from sqlparse import formatter
17+
from . import engine
18+
from . import filters
19+
from . import formatter
2020

2121

2222
def parse(sql):
@@ -55,7 +55,7 @@ def split(sql):
5555
return [unicode(stmt) for stmt in stack.run(sql)]
5656

5757

58-
from sqlparse.engine.filter import StatementFilter
58+
from .engine.filter import StatementFilter
5959
def split2(stream):
6060
splitter = StatementFilter()
6161
return list(splitter.process(None, stream))

sqlparse/engine/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
# the BSD License: http://www.opensource.org/licenses/bsd-license.php.
55

66
"""filter"""
7-
8-
from sqlparse import lexer
9-
from sqlparse.engine import grouping
10-
from sqlparse.engine.filter import StatementFilter
7+
from __future__ import absolute_import
8+
from .. import lexer
9+
from . import grouping
10+
from .filter import StatementFilter
1111

1212
# XXX remove this when cleanup is complete
1313
Filter = object

sqlparse/engine/filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
3-
from sqlparse.sql import Statement, Token
4-
from sqlparse import tokens as T
2+
from __future__ import absolute_import
3+
from ..sql import Statement, Token
4+
from .. import tokens as T
55

66

77
class TokenFilter(object):

sqlparse/engine/grouping.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
2-
2+
from __future__ import absolute_import
33
import itertools
44

5-
from sqlparse import sql
6-
from sqlparse import tokens as T
5+
from .. import sql
6+
from .. import tokens as T
77

88
try:
99
next

sqlparse/filters.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
from __future__ import unicode_literals
24

35
import re
46

57
from os.path import abspath, join
68

7-
from sqlparse import sql
8-
from sqlparse import tokens as T
9-
from sqlparse.engine import FilterStack
10-
from sqlparse.tokens import (
9+
from . import sql
10+
from . import tokens as T
11+
from .engine import FilterStack
12+
from .tokens import (
1113
Comment, Keyword, Name,
1214
Punctuation, String, Whitespace,
1315
)
@@ -36,7 +38,7 @@ def __init__(self, case=None):
3638
if case is None:
3739
case = 'upper'
3840
assert case in ['lower', 'upper', 'capitalize']
39-
self.convert = getattr(unicode, case)
41+
self.convert = getattr(str, case)
4042

4143
def process(self, stack, stream):
4244
for ttype, value in stream:
@@ -111,7 +113,7 @@ def process(self, stack, stream):
111113
f = open(path)
112114
raw_sql = f.read()
113115
f.close()
114-
except IOError, err:
116+
except IOError as err:
115117
yield Comment, u'-- IOError: %s\n' % err
116118

117119
else:
@@ -213,7 +215,7 @@ def __init__(self, width=2, char=' ', line_width=None):
213215
def _get_offset(self, token):
214216
all_ = list(self._curr_stmt.flatten())
215217
idx = all_.index(token)
216-
raw = ''.join(unicode(x) for x in all_[:idx + 1])
218+
raw = ''.join(str(x) for x in all_[:idx + 1])
217219
line = raw.splitlines()[-1]
218220
# Now take current offset into account and return relative offset.
219221
full_offset = len(line) - len(self.char * (self.width * self.indent))
@@ -467,7 +469,7 @@ def Tokens2Unicode(stream):
467469
result = ""
468470

469471
for _, value in stream:
470-
result += unicode(value)
472+
result += str(value)
471473

472474
return result
473475

sqlparse/formatter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# the BSD License: http://www.opensource.org/licenses/bsd-license.php.
55

66
"""SQL formatter"""
7-
8-
from sqlparse import SQLParseError
9-
from sqlparse import filters
7+
from __future__ import absolute_import
8+
from . import SQLParseError
9+
from . import filters
1010

1111

1212
def validate_options(options):

sqlparse/keywords.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from sqlparse import tokens
1+
from __future__ import absolute_import
2+
from . import tokens
23

34
KEYWORDS = {
45
'ABORT': tokens.Keyword,

sqlparse/lexer.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
# http://pygments.org/
1212
# It's separated from the rest of pygments to increase performance
1313
# and to allow some customizations.
14-
14+
from __future__ import absolute_import
15+
from __future__ import unicode_literals
1516
import re
1617

17-
from sqlparse import tokens
18-
from sqlparse.keywords import KEYWORDS, KEYWORDS_COMMON
18+
from . import tokens
19+
from .keywords import KEYWORDS, KEYWORDS_COMMON
1920

2021

2122
class include(str):
@@ -79,7 +80,7 @@ def _process_state(cls, unprocessed, processed, state):
7980

8081
try:
8182
rex = re.compile(tdef[0], rflags).match
82-
except Exception, err:
83+
except Exception as err:
8384
raise ValueError(("uncompilable regex %r in state"
8485
" %r of %r: %s"
8586
% (tdef[0], state, cls, err)))
@@ -150,9 +151,7 @@ def __call__(cls, *args, **kwds):
150151
return type.__call__(cls, *args, **kwds)
151152

152153

153-
class Lexer(object):
154-
155-
__metaclass__ = LexerMeta
154+
class Lexer(object, metaclass=LexerMeta):
156155

157156
encoding = 'utf-8'
158157
stripall = False
@@ -209,7 +208,7 @@ def __init__(self):
209208
self.filters = []
210209

211210
def add_filter(self, filter_, **options):
212-
from sqlparse.filters import Filter
211+
from .filters import Filter
213212
if not isinstance(filter_, Filter):
214213
filter_ = filter_(**options)
215214
self.filters.append(filter_)
@@ -223,7 +222,7 @@ def get_tokens(self, text, unfiltered=False):
223222
Also preprocess the text, i.e. expand tabs and strip it if
224223
wanted and applies registered filters.
225224
"""
226-
if not isinstance(text, unicode):
225+
if not isinstance(text, str):
227226
if self.encoding == 'guess':
228227
try:
229228
text = text.decode('utf-8')

sqlparse/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# This module is part of python-sqlparse and is released under
44
# the BSD License: http://www.opensource.org/licenses/bsd-license.php.
5-
5+
from __future__ import absolute_import
66
from types import GeneratorType
77

88

0 commit comments

Comments
 (0)