Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set indent size in ACE editor

I created an ACE editor instance using the following code:

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
#editor {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/ace.js"></script>

<div id="editor">function foo (x) {
    return 2 * x;
}

function bar (y) {
    return foo(y) / 2;
}

console.log(bar(2) + foo(3));</div>

I want to control the indent size (especially when pressing the tab key). How can I do that?

I searched in the API reference but I couldn't find the solution...

like image 704
Ionică Bizău Avatar asked Apr 14 '15 06:04

Ionică Bizău


People also ask

What is indent size?

The indent size - is how many whitespaces will be put in the indented line start. Sometimes it is done with tabs, sometimes with spaces, depending on configuration.

What is ACE code editor?

Ace is an embeddable code editor written in JavaScript. It matches the features and performance of native editors such as Sublime, Vim and TextMate. It can be easily embedded in any web page and JavaScript application.


1 Answers

you can use setOption("tabSize", 8) or similar setOptions function shown bellow

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setOptions({
    mode: "ace/mode/javascript",
    tabSize: 8,
    useSoftTabs: true
});
#editor {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/ace.js">
</script>

<div id="editor">function foo (x) {
    return 2 * x;
}</div>
like image 70
a user Avatar answered Sep 23 '22 15:09

a user