Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use beautify in Ace Editor?

Tags:

ace-editor

I've found the beautify extension in Ace editor but I don't see any examples of how to use it. Here's what I have so far:

var beautiful = ace.require("ace/ext/beautify");
beautiful.beautify();

but I get the error:

Result of expression 'e' [undefined] is not an object.
like image 286
1.21 gigawatts Avatar asked Aug 01 '15 23:08

1.21 gigawatts


2 Answers

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.

like image 116
1.21 gigawatts Avatar answered Sep 22 '22 17:09

1.21 gigawatts


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);
}
like image 30
ErazerBrecht Avatar answered Sep 23 '22 17:09

ErazerBrecht