Skip to content

Commit

Permalink
feat: Added ability to configure certain format options for beautify …
Browse files Browse the repository at this point in the history
…extension
  • Loading branch information
andrewnester committed May 11, 2022
1 parent 4d2ecf0 commit 20275de
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/ace/ext/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ exports.singletonTags = ["area", "base", "br", "col", "command", "embed", "hr",
// insert a line break after block level tags
exports.blockTags = ["article", "aside", "blockquote", "body", "div", "dl", "fieldset", "footer", "form", "head", "header", "html", "nav", "ol", "p", "script", "section", "style", "table", "tbody", "tfoot", "thead", "ul"];

exports.formatOptions = {
lineBreaksAfterCommasInCurlyBlock: true
};

exports.beautify = function(session) {
var iterator = new TokenIterator(session, 0, 0);
var token = iterator.getCurrentToken();
var tabString = session.getTabString();
var singletonTags = exports.singletonTags;
var blockTags = exports.blockTags;
var formatOptions = exports.formatOptions || {};
var nextToken;
var breakBefore = false;
var spaceBefore = false;
Expand Down Expand Up @@ -269,7 +274,7 @@ exports.beautify = function(session) {
trimNext();

// line break after commas in curly block
if (value.match(/^(,)$/) && curlyDepth>0 && roundDepth===0) {
if (value.match(/^(,)$/) && curlyDepth>0 && roundDepth===0 && formatOptions.lineBreaksAfterCommasInCurlyBlock) {
rowsToAdd++;
} else {
spaceAfter = true;
Expand Down
34 changes: 34 additions & 0 deletions lib/ace/ext/beautify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,40 @@ module.exports = {
+ "\t\t\"b\": \"2\"\n"
+ "\t}\n"
+ "</script>");
},

"test beautify php default behaviour with line breaks after comma": function() {
var s = new EditSession([
"<?php\n",
"class Test {",
"public int $id, $num;",
"}"
], new PHPMode());
s.setUseSoftTabs(false);

beautify.beautify(s);
assert.equal(s.getValue(), "<?php\n"
+ "class Test {\n"
+ "\tpublic int $id,\n"
+ "\t$num;\n"
+ "}");
},

"test beautify php with no line breaks after comma": function() {
var s = new EditSession([
"<?php\n",
"class Test {",
"public int $id, $num;",
"}"
], new PHPMode());
s.setUseSoftTabs(false);

beautify.formatOptions.lineBreaksAfterCommasInCurlyBlock = false;
beautify.beautify(s);
assert.equal(s.getValue(), "<?php\n"
+ "class Test {\n"
+ "\tpublic int $id, $num;\n"
+ "}");
}
};

Expand Down

0 comments on commit 20275de

Please sign in to comment.