How do I use beautify in Ace Editor?

It looks like this works:

var beautify = ace.require("ace/ext/beautify"); // get reference to extension
var editor = ace.edit("editor"); // get reference to editor
    beautify.beautify(editor.session);

It requires that you pass in the Ace Editor session as the first parameter. In my original question, I did not pass in any variables and that was throwing an error.

Note: It did not work well which was mentioned on the extensions release notes. It was not working well enough to use.


I didn't get it working

var beautify = ace.require("ace/ext/beautify"); // get reference to extension

Beautify was always undefined.

After a while I gave up.

And used the external Beautify library (Link)

function beatify() {
  var val = editor.session.getValue();
  //Remove leading spaces
  var array = val.split(/\n/);
  array[0] = array[0].trim();
  val = array.join("\n");
  //Actual beautify (prettify)
  val = js_beautify(val);
  //Change current text to formatted text
  editor.session.setValue(val);
}

Tags:

Ace Editor