Skip to content

Commit

Permalink
Fixed beautifier#2219 - formatting of new Angular control flow syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Gergely Gyorgy Both committed Nov 11, 2023
1 parent 1df08a0 commit bd2dfe0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
15 changes: 15 additions & 0 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ Printer.prototype.indent = function() {
this.indent_level++;
};

Printer.prototype.deindent = function() {
if (this.indent_level > 0 ) {
this.indent_level--;
this._output.set_indent(this.indent_level, this.alignment_size);
}
};

Printer.prototype.get_full_indent = function(level) {
level = this.indent_level + (level || 0);
if (level < 1) {
Expand Down Expand Up @@ -305,6 +312,14 @@ Beautifier.prototype.beautify = function() {
parser_token = this._handle_tag_close(printer, raw_token, last_tag_token);
} else if (raw_token.type === TOKEN.TEXT) {
parser_token = this._handle_text(printer, raw_token, last_tag_token);
} else if(raw_token.type === TOKEN.BRACE_OPEN) { // TODO: handle indentation based on brace_style (and preserve-inline)
printer.set_space_before_token(true);
printer.print_token(raw_token);
printer.indent();
} else if(raw_token.type === TOKEN.BRACE_CLOSE) {
printer.deindent();
printer.print_newline(true);
printer.print_token(raw_token);
} else {
// This should never happen, but if it does. Print the raw token
printer.add_raw_token(raw_token);
Expand Down
21 changes: 20 additions & 1 deletion js/src/html/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var Pattern = require('../core/pattern').Pattern;
var TOKEN = {
TAG_OPEN: 'TK_TAG_OPEN',
TAG_CLOSE: 'TK_TAG_CLOSE',
BRACE_OPEN: 'TK_BRACE_OPEN',
BRACE_CLOSE: 'TK_BRACE_CLOSE',
ATTRIBUTE: 'TK_ATTRIBUTE',
EQUALS: 'TK_EQUALS',
VALUE: 'TK_VALUE',
Expand All @@ -60,7 +62,7 @@ var Tokenizer = function(input_string, options) {
var pattern_reader = new Pattern(this._input);

this.__patterns = {
word: templatable_reader.until(/[\n\r\t <]/),
word: templatable_reader.until(/[\n\r\t <{}]/),
single_quote: templatable_reader.until_after(/'/),
double_quote: templatable_reader.until_after(/"/),
attribute: templatable_reader.until(/[\n\r\t =>]|\/>/),
Expand Down Expand Up @@ -123,6 +125,7 @@ Tokenizer.prototype._get_next_token = function(previous_token, open_token) { //
token = token || this._read_open_handlebars(c, open_token);
token = token || this._read_attribute(c, previous_token, open_token);
token = token || this._read_close(c, open_token);
token = token || this._read_control_flows(c, previous_token, open_token);
token = token || this._read_raw_content(c, previous_token, open_token);
token = token || this._read_content_word(c);
token = token || this._read_comment_or_cdata(c);
Expand Down Expand Up @@ -221,6 +224,22 @@ Tokenizer.prototype._read_open_handlebars = function(c, open_token) {
return token;
};

Tokenizer.prototype._read_control_flows = function (c, previous_token, open_token) {
console.log(c, open_token);
var resulting_string = null;
var token = null;
if (!open_token) {
if (previous_token.text[0] === '@' && c === '{' && this._input.peek(1) !== '{' && this._input.peek(-1) !== '{') {
resulting_string = this._input.next();
token = this._create_token(TOKEN.BRACE_OPEN, resulting_string);
} else if (c === '}' && this._input.peek(1) !== '}' && this._input.peek(-1) !== '}') {
resulting_string = this._input.next();
token = this._create_token(TOKEN.BRACE_CLOSE, resulting_string);
}
}
return token;
};


Tokenizer.prototype._read_close = function(c, open_token) {
var resulting_string = null;
Expand Down

0 comments on commit bd2dfe0

Please sign in to comment.