mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-17 03:38:08 +00:00
Update python with space and tab adjustment
This commit is contained in:
parent
a2c3d3aa73
commit
0bedb9961c
@ -1,6 +1,5 @@
|
||||
/*jshint node:true */
|
||||
/*
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
|
||||
@ -102,7 +101,7 @@ OutputLine.prototype.pop = function() {
|
||||
OutputLine.prototype.remove_indent = function() {
|
||||
if (this.__indent_count > 0) {
|
||||
this.__indent_count -= 1;
|
||||
this.__character_count -= this.__parent.indent_length;
|
||||
this.__character_count -= this.__parent.indent_size;
|
||||
}
|
||||
};
|
||||
|
||||
@ -122,7 +121,6 @@ OutputLine.prototype.toString = function() {
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
function IndentStringCache(options, baseIndentString) {
|
||||
this.__cache = [''];
|
||||
this.__indent_size = options.indent_size;
|
||||
@ -131,7 +129,7 @@ function IndentStringCache(options, baseIndentString) {
|
||||
this.__indent_string = new Array(options.indent_size + 1).join(options.indent_char);
|
||||
}
|
||||
|
||||
// Set to null to continue support for auto detection of base indent level.
|
||||
// Set to null to continue support for auto detection of base indent
|
||||
baseIndentString = baseIndentString || '';
|
||||
if (options.indent_level > 0) {
|
||||
baseIndentString = new Array(options.indent_level + 1).join(this.__indent_string);
|
||||
@ -188,11 +186,10 @@ IndentStringCache.prototype.__add_column = function() {
|
||||
};
|
||||
|
||||
function Output(options, baseIndentString) {
|
||||
|
||||
this.__indent_cache = new IndentStringCache(options, baseIndentString);
|
||||
this.raw = false;
|
||||
this._end_with_newline = options.end_with_newline;
|
||||
|
||||
this.indent_size = options.indent_size;
|
||||
this.__lines = [];
|
||||
this.previous_line = null;
|
||||
this.current_line = null;
|
||||
@ -249,7 +246,6 @@ Output.prototype.get_code = function(eol) {
|
||||
if (eol !== '\n') {
|
||||
sweet_code = sweet_code.replace(/[\n]/g, eol);
|
||||
}
|
||||
|
||||
return sweet_code;
|
||||
};
|
||||
|
||||
|
@ -66,7 +66,6 @@ class Options:
|
||||
if self.indent_size == 1:
|
||||
self.indent_size = 4
|
||||
|
||||
|
||||
# Backwards compat with 1.3.x
|
||||
self.wrap_line_length = self._get_number(
|
||||
'wrap_line_length', self._get_number('max_char'))
|
||||
|
@ -23,6 +23,7 @@
|
||||
# SOFTWARE.
|
||||
|
||||
import re
|
||||
import math
|
||||
|
||||
# Using object instead of string to allow for later expansion of info
|
||||
# about each line
|
||||
@ -42,18 +43,17 @@ class OutputLine:
|
||||
def item(self, index):
|
||||
return self.__items[index]
|
||||
|
||||
def get_character_count(self):
|
||||
return self.__character_count
|
||||
|
||||
def is_empty(self):
|
||||
return len(self.__items) == 0
|
||||
|
||||
def set_indent(self, indent=0, alignment=0):
|
||||
self.__indent_count = indent
|
||||
self.__alignment_count = alignment
|
||||
self.__character_count = self.__parent.baseIndentLength + \
|
||||
self.__alignment_count + \
|
||||
self.__indent_count * self.__parent.indent_length
|
||||
self.__character_count = self.__parent.get_indent_size(
|
||||
self.__indent_count, self.__alignment_count)
|
||||
|
||||
def get_character_count(self):
|
||||
return self.__character_count
|
||||
|
||||
def last(self):
|
||||
if not self.is_empty():
|
||||
@ -75,7 +75,7 @@ class OutputLine:
|
||||
def remove_indent(self):
|
||||
if self.__indent_count > 0:
|
||||
self.__indent_count -= 1
|
||||
self.__character_count -= self.__parent.indent_length
|
||||
self.__character_count -= self.__parent.indent_size
|
||||
|
||||
def trim(self):
|
||||
while self.last() == ' ':
|
||||
@ -85,52 +85,75 @@ class OutputLine:
|
||||
def toString(self):
|
||||
result = ''
|
||||
if not self.is_empty():
|
||||
if self.__indent_count >= 0:
|
||||
result = self.__parent.get_indent_string(self.__indent_count)
|
||||
if self.__alignment_count >= 0:
|
||||
result += self.__parent.get_alignment_string(
|
||||
self.__alignment_count)
|
||||
result = self.__parent.get_indent_string(
|
||||
self.__indent_count, self.__alignment_count)
|
||||
result += ''.join(self.__items)
|
||||
return result
|
||||
|
||||
|
||||
class IndentCache:
|
||||
def __init__(self, base_string, level_string):
|
||||
self.__cache = [base_string]
|
||||
self.__level_string = level_string
|
||||
class IndentStringCache:
|
||||
def __init__(self, options, base_string):
|
||||
self.__cache = ['']
|
||||
self.__indent_size = options.indent_size
|
||||
self.__indent_string = options.indent_char
|
||||
if not options.indent_with_tabs:
|
||||
self.__indent_string = options.indent_char * options.indent_size
|
||||
|
||||
def __ensure_cache(self, level):
|
||||
while level >= len(self.__cache):
|
||||
self.__cache.append(
|
||||
self.__cache[-1] + self.__level_string)
|
||||
# Set to null to continue support of auto detection of base indent
|
||||
base_string = base_string or ''
|
||||
if options.indent_level > 0:
|
||||
base_string = options.indent_level * self.__indent_string
|
||||
|
||||
def get_level_string(self, level):
|
||||
self.__ensure_cache(level)
|
||||
return self.__cache[level]
|
||||
self.__base_string = base_string
|
||||
self.__base_string_length = len(base_string)
|
||||
|
||||
def get_indent_size(self, indent, column=0):
|
||||
result = self.__base_string_length
|
||||
if indent < 0:
|
||||
result = 0
|
||||
result += indent * self.__indent_size
|
||||
result += column
|
||||
return result
|
||||
|
||||
def get_indent_string(self, indent_level, column=0):
|
||||
result = self.__base_string
|
||||
if indent_level < 0:
|
||||
indent_level = 0
|
||||
result = ''
|
||||
column += indent_level * self.__indent_size
|
||||
self.__ensure_cache(column)
|
||||
result += self.__cache[column]
|
||||
return result
|
||||
|
||||
def __ensure_cache(self, column):
|
||||
while column >= len(self.__cache):
|
||||
self.__add_column()
|
||||
|
||||
def __add_column(self):
|
||||
column = len(self.__cache)
|
||||
indent = 0
|
||||
result = ''
|
||||
if self.__indent_size and column >= self.__indent_size:
|
||||
indent = int(math.floor(column / self.__indent_size))
|
||||
column -= indent * self.__indent_size
|
||||
result = indent * self.__indent_string
|
||||
if column:
|
||||
result += column * ' '
|
||||
self.__cache.append(result)
|
||||
|
||||
|
||||
class Output:
|
||||
def __init__(self, options, baseIndentString=''):
|
||||
|
||||
indent_string = options.indent_char
|
||||
if options.indent_size > 0:
|
||||
indent_string = options.indent_char * options.indent_size
|
||||
|
||||
# Set to null to continue support for auto detection of base levelself.
|
||||
if options.indent_level > 0:
|
||||
baseIndentString = options.indent_level * indent_string
|
||||
|
||||
self.__indent_cache = IndentCache(baseIndentString, indent_string)
|
||||
self.__alignment_cache = IndentCache('', ' ')
|
||||
self.baseIndentLength = len(baseIndentString)
|
||||
self.indent_length = len(indent_string)
|
||||
self.__indent_cache = IndentStringCache(options, baseIndentString)
|
||||
self.raw = False
|
||||
self._end_with_newline = options.end_with_newline
|
||||
self.indent_size = options.indent_size
|
||||
self.__lines = []
|
||||
self.previous_line = None
|
||||
self.current_line = None
|
||||
self.space_before_token = False
|
||||
|
||||
# initialize
|
||||
self.__add_outputline()
|
||||
|
||||
def __add_outputline(self):
|
||||
@ -141,11 +164,11 @@ class Output:
|
||||
def get_line_number(self):
|
||||
return len(self.__lines)
|
||||
|
||||
def get_indent_string(self, level):
|
||||
return self.__indent_cache.get_level_string(level)
|
||||
def get_indent_string(self, indent, column=0):
|
||||
return self.__indent_cache.get_indent_string(indent, column)
|
||||
|
||||
def get_alignment_string(self, level):
|
||||
return self.__alignment_cache.get_level_string(level)
|
||||
def get_indent_size(self, indent, column=0):
|
||||
return self.__indent_cache.get_indent_size(indent, column)
|
||||
|
||||
def is_empty(self):
|
||||
return self.previous_line is None and self.current_line.is_empty()
|
||||
|
@ -326,7 +326,7 @@ class Beautifier:
|
||||
line.push(current_token.whitespace_before)
|
||||
self._output.space_before_token = False
|
||||
elif self._output.set_indent(self._flags.indentation_level,
|
||||
self._flags.alignment):
|
||||
self._flags.alignment):
|
||||
self._flags.line_indent_level = self._flags.indentation_level
|
||||
|
||||
def print_token(self, current_token, s=None):
|
||||
@ -1223,7 +1223,6 @@ class Beautifier:
|
||||
# first line always indented
|
||||
self.print_token(current_token, lines[0])
|
||||
|
||||
|
||||
if len(lines) > 1:
|
||||
lines = lines[1:]
|
||||
javadoc = not any(l for l in lines if (
|
||||
|
Loading…
x
Reference in New Issue
Block a user