Skip to content

Commit

Permalink
Add alignment to output
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Aug 12, 2018
1 parent 6b4ab06 commit 6e8afc8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
35 changes: 30 additions & 5 deletions js/src/core/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ function OutputLine(parent) {
this._character_count = 0;
// use indent_count as a marker for this._lines that have preserved indentation
this._indent_count = -1;
this._alignment_count = 0;

this._items = [];
}

OutputLine.prototype.set_indent = function(level) {
this._character_count = this._parent.baseIndentLength + level * this._parent.indent_length;
this._indent_count = level;
this._character_count = this._parent.baseIndentLength + this._alignment_count + this._indent_count * this._parent.indent_length;
};

OutputLine.prototype.set_alignement = function(level) {
this._alignment_count = level;
this._character_count = this._parent.baseIndentLength + this._alignment_count + this._indent_count * this._parent.indent_length;
};

OutputLine.prototype.get_character_count = function() {
Expand Down Expand Up @@ -88,7 +94,10 @@ OutputLine.prototype.toString = function() {
var result = '';
if (!this.is_empty()) {
if (this._indent_count >= 0) {
result = this._parent.indent_cache[this._indent_count];
result = this._parent._indent_cache[this._indent_count];
}
if (this._alignment_count >= 0) {
result += this._parent._alignment_cache[this._alignment_count];
}
result += this._items.join('');
}
Expand All @@ -98,7 +107,8 @@ OutputLine.prototype.toString = function() {

function Output(indent_string, baseIndentString) {
baseIndentString = baseIndentString || '';
this.indent_cache = [baseIndentString];
this._indent_cache = [baseIndentString];
this._alignment_cache = [''];
this.baseIndentLength = baseIndentString.length;
this.indent_length = indent_string.length;
this.raw = false;
Expand Down Expand Up @@ -156,8 +166,8 @@ Output.prototype.get_code = function(end_with_newline, eol) {
Output.prototype.set_indent = function(level) {
// Never indent your first output indent at the start of the file
if (this._lines.length > 1) {
while (level >= this.indent_cache.length) {
this.indent_cache.push(this.indent_cache[this.indent_cache.length - 1] + this.indent_string);
while (level >= this._indent_cache.length) {
this._indent_cache.push(this._indent_cache[this._indent_cache.length - 1] + this.indent_string);
}

this.current_line.set_indent(level);
Expand All @@ -167,6 +177,21 @@ Output.prototype.set_indent = function(level) {
return false;
};

Output.prototype.set_alignment = function(level) {
// Never indent your first output indent at the start of the file
if (this._lines.length > 1) {
while (level >= this._alignment_cache.length) {
this._alignment_cache.push(this._alignment_cache[this._alignment_cache.length - 1] + ' ');
}

this.current_line.set_alignment(level);
return true;
}
this.current_line.set_alignment(0);
return false;
};


Output.prototype.add_raw_token = function(token) {
for (var x = 0; x < token.newlines; x++) {
this.add_outputline();
Expand Down
3 changes: 3 additions & 0 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
var mergeOpts = require('../core/options').mergeOpts;
var acorn = require('../core/acorn');
var InputScanner = require('../core/inputscanner').InputScanner;
var Output = require('../core/output').Output;
var Tokenizer = require('../html/tokenizer').Tokenizer;
var TOKEN = require('../html/tokenizer').TOKEN;

Expand Down Expand Up @@ -595,6 +596,8 @@ function Beautifier(html_source, options, js_beautify, css_beautify) {
this.indent_string += this.indent_character;
}

this._output = new Output('', this.indent_string); // jshint unused:false

this.add_text_item = function(text) {
if (text) {
this.output.push(text);
Expand Down
35 changes: 28 additions & 7 deletions python/jsbeautifier/core/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, parent):
self.__parent = parent
self.__character_count = 0
self.__indent_count = -1
self.__alignment_count = 0

self.__items = []

Expand All @@ -44,9 +45,14 @@ def is_empty(self):
return len(self.__items) == 0

def set_indent(self, level):
self.__character_count = self.__parent.baseIndentLength + \
level * self.__parent.indent_length
self.__indent_count = level
self.__character_count = self.__parent.baseIndentLength + \
+ self.__alignment_count + self.__indent_count * self.__parent.indent_length

def set_alignment(self, level):
self.__alignment_count = level
self.__character_count = self.__parent.baseIndentLength + \
+ self.__alignment_count + self.__indent_count * self.__parent.indent_length

def last(self):
if not self.is_empty():
Expand Down Expand Up @@ -79,7 +85,9 @@ def toString(self):
result = ''
if not self.is_empty():
if self.__indent_count >= 0:
result = self.__parent.indent_cache[self.__indent_count]
result = self.__parent._indent_cache[self.__indent_count]
if self.__alignment_count >= 0:
result += self.__parent._alignment_cache[self.__alignment_count]
result += ''.join(self.__items)
return result

Expand All @@ -89,7 +97,8 @@ def __init__(self, indent_string, baseIndentString=''):

self.indent_string = indent_string
self.baseIndentString = baseIndentString
self.indent_cache = [baseIndentString]
self._indent_cache = [baseIndentString]
self._alignment_cache = ['']
self.baseIndentLength = len(baseIndentString)
self.indent_length = len(indent_string)
self.raw = False
Expand Down Expand Up @@ -133,15 +142,27 @@ def get_code(self, end_with_newline, eol):
def set_indent(self, level):
# Never indent your first output indent at the start of the file
if len(self.lines) > 1:
while level >= len(self.indent_cache):
self.indent_cache.append(
self.indent_cache[-1] + self.indent_string)
while level >= len(self._indent_cache):
self._indent_cache.append(
self._indent_cache[-1] + self.indent_string)

self.current_line.set_indent(level)
return True
self.current_line.set_indent(0)
return False

def set_alignment(self, level):
# Never indent your first output indent at the start of the file
if len(self.lines) > 1:
while level >= len(self._alignment_cache):
self._alignment_cache.append(
self._alignment_cache[-1] + ' ')

self.current_line.set_alignment(level)
return True
self.current_line.set_alignment(0)
return False

def add_raw_token(self, token):
for _ in range(token.newlines):
self.add_outputline()
Expand Down

0 comments on commit 6e8afc8

Please sign in to comment.