forked from future-architect/Sublime-uroboroSQL-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurobosql_formatter.py
121 lines (97 loc) · 4.32 KB
/
urobosql_formatter.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
import os
import sys
import sublime
import sublime_plugin
import threading
sys.path.append(os.path.dirname(__file__))
import uroborosqlfmt
from uroborosqlfmt.config import LocalConfig
from uroborosqlfmt.commentsyntax import Doma2CommentSyntax
class UroborosqlFormatCommand(sublime_plugin.TextCommand):
"""SQL format command class"""
def run(self, edit, sync=False):
view = self.view
view.window().status_message('formatting...')
self.settings = view.settings()
self.user_settings = sublime.load_settings(
'sublime-uroborosql-formatter.sublime-settings')
# set syntax
if self.settings.get('syntax') == \
"Packages/Text/Plain text.tmLanguage":
view.set_syntax_file("Packages/SQL/SQL.tmLanguage")
# settings
self.settings.set("tab_size", self.getval("uf_tab_size"))
self.settings.set("translate_tabs_to_spaces",
self.getval("uf_translate_tabs_to_spaces"))
config = LocalConfig()
if self.getval("uf_case") == 'nochange':
config.set_case(None)
else:
config.set_case(self.getval("uf_case"))
if self.getval("uf_reserved_case") == 'nochange':
config.set_reserved_case(None)
else:
config.set_reserved_case(self.getval("uf_reserved_case"))
# ↓for backward compatibility↓
if self.user_settings.get("uf_uppercase") == False \
and self.user_settings.get("uf_case") == None:
config.set_case(None)
if self.user_settings.get("uf_uppercase") == False \
and self.user_settings.get("uf_reserved_case") == None:
config.set_reserved_case(None)
# ↑for backward compatibility ↑
# set reserved words
if self.getval("uf_reserved_words") != None:
input_reserved_words_list = self.getval("uf_reserved_words")
reserved_words = input_reserved_words_list.split(",")
config.set_input_reserved_words(reserved_words)
uroborosqlfmt.config.glb.escape_sequence_u005c = self.getval(
"uf_escapesequence_u005c")
if str(self.getval("uf_comment_syntax")).upper() == "DOMA2":
config.set_commentsyntax(Doma2CommentSyntax())
raw_text = ""
regions = view.sel()
# format selection
if len(regions) > 1 or not regions[0].empty():
for region in view.sel():
if not region.empty():
raw_text = view.substr(region)
else: # format all
region = sublime.Region(0, view.size())
raw_text = view.substr(region)
if sync:
self.run_format(edit, raw_text, config, region.a, region.b)
else:
threading.Thread(target=self.run_format, args=(
edit, raw_text, config, region.a, region.b)).start()
def run_format(self, edit, raw_text, config, region_a, region_b):
formatted_text = uroborosqlfmt.format_sql(raw_text, config)
self.view.run_command("uroborosql_format_replace", {
"region_a": region_a,
"region_b": region_b,
"formatted_text": formatted_text
})
def getval(self, key):
val = self.user_settings.get(key)
return val if val != None else self.settings.get(key)
class UroborosqlFormatReplaceCommand(sublime_plugin.TextCommand):
"""SQL format replace command class"""
def run(self, edit, **args):
region = sublime.Region(args["region_a"], args["region_b"])
self.view.replace(edit, region, args["formatted_text"])
self.view.window().status_message('formatting... complete!!')
class UroborosqlFormatListener(sublime_plugin.EventListener):
"""Event listner"""
def on_pre_save(self, view):
self.settings = view.settings()
self.user_settings = sublime.load_settings(
'sublime-uroborosql-formatter.sublime-settings')
if self.getval("uf_save_on_format") != True:
return
filepath = view.file_name()
if filepath.endswith(tuple(self.getval("uf_save_on_format_extensions"))):
view.run_command("uroborosql_format", {"sync": True})
def getval(self, key):
val = self.user_settings.get(key)
return val if val != None else self.settings.get(key)