Browse files

Merge pull request #78 from yacomink/master

Python options for tab indentation and functions in var lines
  • Loading branch information...
2 parents 2a34726 + 6dfdc9a commit 0b3da0e24f2e4d8fb178acd6fd988d4b99cd7712 Stefano Sanfilippo committed Nov 1, 2011
Showing with 63 additions and 4 deletions.
  1. +1 −0 python/.gitignore
  2. +16 −4 python/jsbeautifier/__init__.py
  3. +46 −0 python/jsbeautifier/tests/testindentation.py
View
1 python/.gitignore
@@ -0,0 +1 @@
+*.pyc
View
20 python/jsbeautifier/__init__.py
@@ -34,11 +34,13 @@ class BeautifierOptions:
def __init__(self):
self.indent_size = 4
self.indent_char = ' '
+ self.tabs = False
self.preserve_newlines = True
self.max_preserve_newlines = 10.
self.jslint_happy = False
self.brace_style = 'collapse'
self.keep_array_indentation = False
+ self.keep_function_indentation = False
self.eval_code = False
@@ -50,6 +52,7 @@ def __repr__(self):
preserve_newlines = %s
max_preserve_newlines = %d
jslint_happy = %s
+tab = %s
brace_style = %s
keep_array_indentation = %s
eval_code = %s
@@ -117,11 +120,13 @@ def usage():
-s, --indent-size=NUMBER indentation size. (default 4).
-c, --indent-char=CHAR character to indent with. (default space).
+ -t, --tabs Indent with tabs, overrides -s and -c
-d, --disable-preserve-newlines do not preserve existing line breaks.
-j, --jslint-happy more jslint-compatible output
-b, --brace-style=collapse brace style (collapse, expand, end-expand)
-k, --keep-array-indentation keep array indentation.
-o, --outfile=FILE specify a file to output to (default stdout)
+ -f, --keep-function-indentaion Do not re-indent function bodies defined in var lines.
Rarely needed options:
@@ -156,8 +161,11 @@ def blank_state(self):
self.just_added_newline = False
self.do_block_just_closed = False
+ if self.opts.tabs:
+ self.indent_string = "\t"
+ else:
+ self.indent_string = self.opts.indent_char * self.opts.indent_size
- self.indent_string = self.opts.indent_char * self.opts.indent_size
self.preindent_string = ''
self.last_word = '' # last TK_WORD seen
self.last_type = 'TK_START_EXPR' # last token type
@@ -749,7 +757,7 @@ def handle_word(self, token_text):
if token_text == 'function':
if self.flags.var_line:
- self.flags.var_line_reindented = True
+ self.flags.var_line_reindented = not self.opts.keep_function_indentation
if (self.just_added_newline or self.last_text == ';') and self.last_text != '{':
# make sure there is a nice clean space of at least one blank line
# before a new function definition
@@ -1066,10 +1074,10 @@ def main():
argv = sys.argv[1:]
try:
- opts, args = getopt.getopt(argv, "s:c:o:djbkil:h", ['indent-size=','indent-char=','outfile=', 'disable-preserve-newlines',
+ opts, args = getopt.getopt(argv, "s:c:o:djbkil:h:t:f", ['indent-size=','indent-char=','outfile=', 'disable-preserve-newlines',
'jslint-happy', 'brace-style=',
'keep-array-indentation', 'indent-level=', 'help',
- 'usage', 'stdin', 'eval-code'])
+ 'usage', 'stdin', 'eval-code', 'tabs', 'keep-function-indentation'])
except getopt.GetoptError:
usage()
sys.exit(2)
@@ -1084,12 +1092,16 @@ def main():
for opt, arg in opts:
if opt in ('--keep-array-indentation', '-k'):
js_options.keep_array_indentation = True
+ if opt in ('--keep-function-indentation','-f'):
+ js_options.keep_function_indentation = True
elif opt in ('--outfile', '-o'):
outfile = arg
elif opt in ('--indent-size', '-s'):
js_options.indent_size = int(arg)
elif opt in ('--indent-char', '-c'):
js_options.indent_char = arg
+ elif opt in ('--tabs', '-t'):
+ js_options.tabs = True
elif opt in ('--disable-preserve_newlines', '-d'):
js_options.preserve_newlines = False
elif opt in ('--jslint-happy', '-j'):
View
46 python/jsbeautifier/tests/testindentation.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import re
+import unittest
+import jsbeautifier
+
+class TestJSBeautifierIndentation(unittest.TestCase):
+ def test_tabs(self):
+ test_fragment = self.decodesto
+
+ self.options.tabs = 1;
+ test_fragment('{tabs()}', "{\n\ttabs()\n}");
+
+ def test_function_indent(self):
+ test_fragment = self.decodesto
+
+ self.options.tabs = 1;
+ self.options.keep_function_indentation = 1;
+ test_fragment('var foo = function(){ bar() }();', "var foo = function() {\n\tbar()\n}();");
+
+ self.options.tabs = 1;
+ self.options.keep_function_indentation = 0;
+ test_fragment('var foo = function(){ baz() }();', "var foo = function() {\n\t\tbaz()\n\t}();");
+
+ def decodesto(self, input, expectation=None):
+ self.assertEqual(
+ jsbeautifier.beautify(input, self.options), expectation or input)
+
+ @classmethod
+ def setUpClass(cls):
+ options = jsbeautifier.default_options()
+ options.indent_size = 4
+ options.indent_char = ' '
+ options.preserve_newlines = True
+ options.jslint_happy = False
+ options.keep_array_indentation = False
+ options.brace_style = 'collapse'
+ options.indent_level = 0
+
+ cls.options = options
+ cls.wrapregex = re.compile('^(.+)$', re.MULTILINE)
+
+
+if __name__ == '__main__':
+ unittest.main()

0 comments on commit 0b3da0e

Please sign in to comment.