monaco-editor version: 0.10.1
Browser: chrome
OS: Win 10
Steps or JS usage snippet reproducing the issue:
Im not sure if im doing this correctly or if this is an actual bug. if you go to playground and paste the following code:
var model1 = monaco.editor.create(document.getElementById("container"), {
value: "function hello() {\n\talert('Hello world!');\n}",
language: "javascript"
});
model1.updateOptions({
"autoIndent": true
});
After you run it, try pasting some code into the editor, and you will notice that it doesn't get formatted like it does in VSC.
I think you have to enable the formatOnPaste option to make it work...
var model1 = monaco.editor.create(document.getElementById("container"), {
value: "function hello() {\n\talert('Hello world!');\n}",
language: "javascript",
formatOnPaste: true
});
@mofux
Your fix works for pasting, but i noticed that when you move a line up or down (alt+[arrow up/down]) in VSC the line automatically gets indented, however in monaco nothing happens to the indent level for the line. (autoIndent = true in both VSC and ME)
Hmmm... there is another option formatOnType, maybe that is also required?
formatOnType works ok for me on javascript but not on Python - any help on getting Pyton auto indentation working in Monaco? :)
@mofux
I tried the following in the Playground, but it didn't work as expected, i think it maybe a bug, im not sure:
var model1 = monaco.editor.create(document.getElementById("container"), {
value: "function hello() {\n\talert('Hello world!');\n}",
language: "javascript"
});
model1.updateOptions({
"autoIndent": true,
"formatOnPaste": true,
"formatOnType": true
});
Expect:
A selected line should autoindent when moving up or down via (alt+[arrow up/down])
Just tested it, I can confirm that alt+up/down doesn't do the auto indentation, even with formatOnType and autoIndent set to true.
There are quite a lot of features in vscode that don't work in monaco right now (for example the code highlighting for javascript seems quite outdated when compared with vscode).
Unfortunately it is sometimes hard to figure out which features have been put on top by the vscode team, and which features are actual bugs 馃槥
The feature is there, but the JavaScript language in the standalone editor is configured differently than the JavaScript language in VS Code.
var ed1 = monaco.editor.create(document.getElementById("container"), {
value: "function hello() {\n\talert('Hello world!');\n}",
language: "javascript"
});
ed1.updateOptions({
"autoIndent": true
});
monaco.languages.setLanguageConfiguration('javascript', {
indentationRules: {
// ^(.*\*/)?\s*\}.*$
decreaseIndentPattern: /^((?!.*?\/\*).*\*\/)?\s*[\}\]\)].*$/,
// ^.*\{[^}"']*$
increaseIndentPattern: /^((?!\/\/).)*(\{[^}"'`]*|\([^)"'`]*|\[[^\]"'`]*)$/
}
});
I took those indentation rules from VS Code.

@alexandrudima brilliant
auto indent in Python doesn't work for me too in Monaco..
any ideas how to fix it?
@Ilanak for python you need to set the increaseIndentPattern and decreaseIndentPattern rules. But i dont think they have it for python https://github.com/Microsoft/vscode/search?utf8=%E2%9C%93&q=decreaseIndentPattern&type=
The way the Monaco indentation scheme designed, autoindent is structured in a way where it cannot be implemented for Python. The designers basically screwed up -- increasing indent can happen after a line with a colon, but decreasing indent is impossible to determine by merely analyzing a single line -- you would need to look at two adjacent lines and see that the user manually had un-indented.
So the designers are going to need to fix the entire scheme if they want it to be possible for Python. In the meanwhile, I would suggest using event capturing to look at when the user hits the return carriage key and unindenting at that time.
Most helpful comment
The feature is there, but the JavaScript language in the standalone editor is configured differently than the JavaScript language in VS Code.
I took those indentation rules from VS Code.