From 8e244605825bb80873b34daa3a0c60790446b995 Mon Sep 17 00:00:00 2001 From: salmanul Date: Fri, 28 Jan 2022 17:44:14 +0530 Subject: [PATCH 1/2] Added non-blocking beautify --- lib/ace/ext/beautify.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/ace/ext/beautify.js b/lib/ace/ext/beautify.js index 9fa23399701..41fb1357c7c 100644 --- a/lib/ace/ext/beautify.js +++ b/lib/ace/ext/beautify.js @@ -103,7 +103,7 @@ exports.beautify = function(session) { breakBefore = false; }; - while (token !== null) { + var prettify = function (){ curRow = iterator.getCurrentTokenRow(); rowTokens = iterator.$rowTokens; nextToken = iterator.stepForward(); @@ -409,9 +409,29 @@ exports.beautify = function(session) { token = nextToken; } - - code = code.trim(); - session.doc.setValue(code); + var id; + if(window.requestAnimationFrame){ + id = requestAnimationFrame(function loop (){ + if(token !== null) { + try{ + prettify(); + id = requestAnimationFrame(loop); + }catch(e){ + console.warn('requestAnimationFrame has been interupted',id); + cancelAnimationFrame(id); + } + }else{ + code = code.trim(); + session.doc.setValue(code); + } + }) + }else{ + while(token !== null) { + step(); + } + code = code.trim(); + session.doc.setValue(code); + } }; exports.commands = [{ From b61765f8d9c9db5c4cd71849f19c98451444bbf7 Mon Sep 17 00:00:00 2001 From: salmanul Date: Fri, 13 May 2022 09:45:33 +0530 Subject: [PATCH 2/2] added options for beautify --- lib/ace/ext/beautify.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ace/ext/beautify.js b/lib/ace/ext/beautify.js index 41fb1357c7c..b4134b81a1d 100644 --- a/lib/ace/ext/beautify.js +++ b/lib/ace/ext/beautify.js @@ -44,7 +44,7 @@ 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.beautify = function(session) { +exports.beautify = function(session, options = {useRequestAnimationFrame:false}) { var iterator = new TokenIterator(session, 0, 0); var token = iterator.getCurrentToken(); var tabString = session.getTabString(); @@ -77,6 +77,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') @@ -409,8 +410,8 @@ exports.beautify = function(session) { token = nextToken; } - var id; - if(window.requestAnimationFrame){ + + if(options.useRequestAnimationFrame && window.requestAnimationFrame){ id = requestAnimationFrame(function loop (){ if(token !== null) { try{