Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : non-blocking beautify #4623

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions demo/beautify.html
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ACE Beautify demo</title>
<style type="text/css" media="screen">
body {
overflow: hidden;
}

#editor {
margin: 0;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
</head>
<body>

<pre id="editor"></pre>

<!-- load ace -->
<script src="../build/src-noconflict/ace.js"></script>
<!-- load ace language tools -->
<script src="../build/src-noconflict/ext-language_tools.js"></script>
<!-- load beautify -->
<script src="../build/src-noconflict/ext-beautify.js"></script>
<script>
// trigger extension
ace.require("ace/ext/language_tools");
// trigger beautify extension
var beautify = ace.require("ace/ext/beautify");
var editor = ace.edit("editor");
editor.session.setMode("ace/mode/html");
</script>
<script src="./generate-large-html.js"></script>
<script>
window.onload = function(){
//beautify
beautify.formatOptions.useRequestAnimationFrame = true;
beautify.beautify(editor.session);
}
</script>
</body>
</html>
11 changes: 11 additions & 0 deletions demo/generate-large-html.js
@@ -0,0 +1,11 @@
var generate = function(n){
var output = '';
var sample = "<div><p>lorem ipsum</p></div>";
for(var i=0;i<n;i++){
output += sample;
}
return output;
}
var largeHtmlString = generate(100);
editor.session.setValue(largeHtmlString);

31 changes: 26 additions & 5 deletions lib/ace/ext/beautify.js
Expand Up @@ -45,7 +45,8 @@ exports.singletonTags = ["area", "base", "br", "col", "command", "embed", "hr",
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
lineBreaksAfterCommasInCurlyBlock: true,
useRequestAnimationFrame:false
};

exports.beautify = function(session) {
Expand Down Expand Up @@ -82,6 +83,7 @@ exports.beautify = function(session) {
var levels = {0: 0};
var parents = [];
var caseBody = false;
var id;

var trimNext = function() {
if (nextToken && nextToken.value && nextToken.type !== 'string.regexp')
Expand All @@ -108,7 +110,7 @@ exports.beautify = function(session) {
breakBefore = false;
};

while (token !== null) {
var prettify = function (){
curRow = iterator.getCurrentTokenRow();
rowTokens = iterator.$rowTokens;
nextToken = iterator.stepForward();
Expand Down Expand Up @@ -415,10 +417,29 @@ exports.beautify = function(session) {
}

token = nextToken;
};

if(formatOptions.useRequestAnimationFrame && window.requestAnimationFrame){
id = window.requestAnimationFrame(function loop (){
if(token !== null) {
try{
prettify();
code = code.trim();
session.doc.setValue(code);
id = window.requestAnimationFrame(loop);
}catch(e){
console.warn('requestAnimationFrame has been interupted',id);
window.cancelAnimationFrame(id);
}
}
});
}else{
while(token !== null) {
prettify();
}
code = code.trim();
session.doc.setValue(code);
}

code = code.trim();
session.doc.setValue(code);
};

exports.commands = [{
Expand Down