Skip to content

Commit 20275de

Browse files
committed
feat: Added ability to configure certain format options for beautify extension
1 parent 4d2ecf0 commit 20275de

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/ace/ext/beautify.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ exports.singletonTags = ["area", "base", "br", "col", "command", "embed", "hr",
4444
// insert a line break after block level tags
4545
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"];
4646

47+
exports.formatOptions = {
48+
lineBreaksAfterCommasInCurlyBlock: true
49+
};
50+
4751
exports.beautify = function(session) {
4852
var iterator = new TokenIterator(session, 0, 0);
4953
var token = iterator.getCurrentToken();
5054
var tabString = session.getTabString();
5155
var singletonTags = exports.singletonTags;
5256
var blockTags = exports.blockTags;
57+
var formatOptions = exports.formatOptions || {};
5358
var nextToken;
5459
var breakBefore = false;
5560
var spaceBefore = false;
@@ -269,7 +274,7 @@ exports.beautify = function(session) {
269274
trimNext();
270275

271276
// line break after commas in curly block
272-
if (value.match(/^(,)$/) && curlyDepth>0 && roundDepth===0) {
277+
if (value.match(/^(,)$/) && curlyDepth>0 && roundDepth===0 && formatOptions.lineBreaksAfterCommasInCurlyBlock) {
273278
rowsToAdd++;
274279
} else {
275280
spaceAfter = true;

lib/ace/ext/beautify_test.js

+34
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,40 @@ module.exports = {
383383
+ "\t\t\"b\": \"2\"\n"
384384
+ "\t}\n"
385385
+ "</script>");
386+
},
387+
388+
"test beautify php default behaviour with line breaks after comma": function() {
389+
var s = new EditSession([
390+
"<?php\n",
391+
"class Test {",
392+
"public int $id, $num;",
393+
"}"
394+
], new PHPMode());
395+
s.setUseSoftTabs(false);
396+
397+
beautify.beautify(s);
398+
assert.equal(s.getValue(), "<?php\n"
399+
+ "class Test {\n"
400+
+ "\tpublic int $id,\n"
401+
+ "\t$num;\n"
402+
+ "}");
403+
},
404+
405+
"test beautify php with no line breaks after comma": function() {
406+
var s = new EditSession([
407+
"<?php\n",
408+
"class Test {",
409+
"public int $id, $num;",
410+
"}"
411+
], new PHPMode());
412+
s.setUseSoftTabs(false);
413+
414+
beautify.formatOptions.lineBreaksAfterCommasInCurlyBlock = false;
415+
beautify.beautify(s);
416+
assert.equal(s.getValue(), "<?php\n"
417+
+ "class Test {\n"
418+
+ "\tpublic int $id, $num;\n"
419+
+ "}");
386420
}
387421
};
388422

0 commit comments

Comments
 (0)