From 8e244605825bb80873b34daa3a0c60790446b995 Mon Sep 17 00:00:00 2001 From: salmanul Date: Fri, 28 Jan 2022 17:44:14 +0530 Subject: [PATCH] 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 = [{