forked from akalongman/sublimetext-codeformatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyformatter.py
More file actions
302 lines (250 loc) · 9.81 KB
/
pyformatter.py
File metadata and controls
302 lines (250 loc) · 9.81 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# @author Avtandil Kikabidze
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN (akalongman@gmail.com)
# @link http://longman.me
# @license The MIT License (MIT)
import os
import sys
import re
import sublime
st_version = 2
if sublime.version() == '' or int(sublime.version()) > 3000:
st_version = 3
if (st_version == 2):
from pybeautifier import Beautifier
else:
#from .pybeautifier import Beautifier
print('CodeFormatter: formatting python files on ST3 not supported.')
class PyFormatter:
def __init__(self, formatter):
self.formatter = formatter
self.opts = formatter.settings.get('codeformatter_python_options')
def format(self, text):
if (self.formatter.st_version == 3):
stdout = ""
stderr = "formatting python files on ST3 not supported!"
return stdout, stderr
# Options
options = {}
# indent_size
if (self.opts["indent_size"]):
indent_size = self.opts["indent_size"]
else:
indent_size = 1
# indent_with_tabs
if (self.opts["indent_with_tabs"]):
indent_with_tabs = True
else:
indent_with_tabs = False
if indent_with_tabs:
indentation = ' ' * indent_size
else:
indentation = ' ' * indent_size
options['INDENTATION'] = indentation
# max_char
if (self.opts["max_char"]):
col_limit = self.opts["max_char"]
else:
col_limit = 80
options['COL_LIMIT'] = col_limit
# assignment
if (self.opts["assignment"]):
assignment = self.opts["assignment"]
else:
assignment = " = "
options['ASSIGNMENT'] = assignment
# function_param_assignment
if (self.opts["function_param_assignment"]):
function_param_assignment = self.opts["function_param_assignment"]
else:
function_param_assignment = "="
options['FUNCTION_PARAM_ASSIGNMENT'] = function_param_assignment
# function_param_sep
if (self.opts["function_param_sep"]):
function_param_sep = self.opts["function_param_sep"]
else:
function_param_sep = ", "
options['FUNCTION_PARAM_SEP'] = function_param_sep
# list_sep
if (self.opts["list_sep"]):
list_sep = self.opts["list_sep"]
else:
list_sep = ", "
options['LIST_SEP'] = list_sep
# subscript_sep
if (self.opts["subscript_sep"]):
subscript_sep = self.opts["subscript_sep"]
else:
subscript_sep = "="
options['SUBSCRIPT_SEP'] = subscript_sep
# dict_colon
if (self.opts["dict_colon"]):
dict_colon = self.opts["dict_colon"]
else:
dict_colon = ": "
options['DICT_COLON'] = dict_colon
# slice_colon
if (self.opts["slice_colon"]):
slice_colon = self.opts["slice_colon"]
else:
slice_colon = ": "
options['SLICE_COLON'] = slice_colon
# comment_prefix
if (self.opts["comment_prefix"]):
comment_prefix = self.opts["comment_prefix"]
else:
comment_prefix = "# "
options['COMMENT_PREFIX'] = comment_prefix
# shebang
if (self.opts["shebang"]):
shebang = self.opts["shebang"]
else:
shebang = "#!/usr/bin/env python"
options['SHEBANG'] = shebang
# boilerplate
if (self.opts["boilerplate"]):
boilerplate = self.opts["boilerplate"]
else:
boilerplate = ""
options['BOILERPLATE'] = boilerplate
# blank_line
if (self.opts["blank_line"]):
blank_line = self.opts["blank_line"]
else:
blank_line = ""
options['BLANK_LINE'] = blank_line
# keep_blank_lines
if (self.opts["keep_blank_lines"]):
keep_blank_lines = self.opts["keep_blank_lines"]
else:
keep_blank_lines = True
options['KEEP_BLANK_LINES'] = keep_blank_lines
# add_blank_lines_around_comments
if (self.opts["add_blank_lines_around_comments"]):
add_blank_lines_around_comments = self.opts["add_blank_lines_around_comments"]
else:
add_blank_lines_around_comments = True
options['ADD_BLANK_LINES_AROUND_COMMENTS'] = add_blank_lines_around_comments
# add_blank_line_after_doc_string
if (self.opts["add_blank_line_after_doc_string"]):
add_blank_line_after_doc_string = self.opts["add_blank_line_after_doc_string"]
else:
add_blank_line_after_doc_string = True
options['ADD_BLANK_LINE_AFTER_DOC_STRING'] = add_blank_line_after_doc_string
# max_seps_func_def
if (self.opts["max_seps_func_def"]):
max_seps_func_def = self.opts["max_seps_func_def"]
else:
max_seps_func_def = 3
options['MAX_SEPS_FUNC_DEF'] = max_seps_func_def
# max_seps_func_ref
if (self.opts["max_seps_func_ref"]):
max_seps_func_ref = self.opts["max_seps_func_ref"]
else:
max_seps_func_ref = 5
options['MAX_SEPS_FUNC_REF'] = max_seps_func_ref
# max_seps_series
if (self.opts["max_seps_series"]):
max_seps_series = self.opts["max_seps_series"]
else:
max_seps_series = 5
options['MAX_SEPS_SERIES'] = max_seps_series
# max_seps_dict
if (self.opts["max_seps_dict"]):
max_seps_dict = self.opts["max_seps_dict"]
else:
max_seps_dict = 3
options['MAX_SEPS_DICT'] = max_seps_dict
# max_lines_before_split_lit
if (self.opts["max_lines_before_split_lit"]):
max_lines_before_split_lit = self.opts["max_lines_before_split_lit"]
else:
max_lines_before_split_lit = 2
options['MAX_LINES_BEFORE_SPLIT_LIT'] = max_lines_before_split_lit
# left_margin
if (self.opts["left_margin"]):
left_margin = self.opts["left_margin"]
else:
left_margin = ""
options['LEFT_MARGIN'] = left_margin
# normalize_doc_strings
if (self.opts["normalize_doc_strings"]):
normalize_doc_strings = self.opts["normalize_doc_strings"]
else:
normalize_doc_strings = False
options['NORMALIZE_DOC_STRINGS'] = normalize_doc_strings
# leftjust_doc_strings
if (self.opts["leftjust_doc_strings"]):
leftjust_doc_strings = self.opts["leftjust_doc_strings"]
else:
leftjust_doc_strings = False
options['LEFTJUST_DOC_STRINGS'] = leftjust_doc_strings
# wrap_doc_strings
if (self.opts["wrap_doc_strings"]):
wrap_doc_strings = self.opts["wrap_doc_strings"]
else:
wrap_doc_strings = False
options['WRAP_DOC_STRINGS'] = wrap_doc_strings
# leftjust_comments
if (self.opts["leftjust_comments"]):
leftjust_comments = self.opts["leftjust_comments"]
else:
leftjust_comments = False
options['LEFTJUST_COMMENTS'] = leftjust_comments
# wrap_comments
if (self.opts["wrap_comments"]):
wrap_comments = self.opts["wrap_comments"]
else:
wrap_comments = False
options['WRAP_COMMENTS'] = wrap_comments
# double_quoted_strings
if (self.opts["double_quoted_strings"]):
double_quoted_strings = self.opts["double_quoted_strings"]
else:
double_quoted_strings = False
options['DOUBLE_QUOTED_STRINGS'] = double_quoted_strings
# single_quoted_strings
if (self.opts["single_quoted_strings"]):
single_quoted_strings = self.opts["single_quoted_strings"]
else:
single_quoted_strings = False
options['SINGLE_QUOTED_STRINGS'] = single_quoted_strings
# can_split_strings
if (self.opts["can_split_strings"]):
can_split_strings = self.opts["can_split_strings"]
else:
can_split_strings = False
options['CAN_SPLIT_STRINGS'] = can_split_strings
# doc_tab_replacement
if (self.opts["doc_tab_replacement"]):
doc_tab_replacement = self.opts["doc_tab_replacement"]
else:
doc_tab_replacement = "...."
options['DOC_TAB_REPLACEMENT'] = doc_tab_replacement
# keep_unassigned_constants
if (self.opts["keep_unassigned_constants"]):
keep_unassigned_constants = self.opts["keep_unassigned_constants"]
else:
keep_unassigned_constants = False
options['KEEP_UNASSIGNED_CONSTANTS'] = keep_unassigned_constants
# parenthesize_tuple_display
if (self.opts["parenthesize_tuple_display"]):
parenthesize_tuple_display = self.opts["parenthesize_tuple_display"]
else:
parenthesize_tuple_display = True
options['PARENTHESIZE_TUPLE_DISPLAY'] = parenthesize_tuple_display
# java_style_list_dedent
if (self.opts["java_style_list_dedent"]):
java_style_list_dedent = self.opts["java_style_list_dedent"]
else:
java_style_list_dedent = False
options['JAVA_STYLE_LIST_DEDENT'] = java_style_list_dedent
beautifier = Beautifier(self.formatter)
stdout, stderr = beautifier.beautify(text, options)
return stdout, stderr
def formatOnSaveEnabled(self, file_name):
format_on_save = False
if ("format_on_save" in self.opts and self.opts["format_on_save"]):
format_on_save = self.opts["format_on_save"]
if (isinstance(format_on_save, str)):
format_on_save = re.search(format_on_save, file_name) != None
return format_on_save