I want to have in monaco editor a feature to automatically correctly indent the pasted code. E.g. if I paste a function inside another function, all subsequent lines of the pasted function should increase indentation to match the first line.
To some extent I managed to do it by enabling formatOnPaste: true
option, but I have 2 problems with this:
- I only want to auto-indent, I do not want to do other auto-formatting which formatOnPaste does (e.g. adding some spaces).
- format on paste works sometimes, but most of the times when I load the page, it just does not work. (Unless I select some text and run Format Selection command from context menu: after that format on paste magically starts to work. So there is some strange bug in how it works.)
How to reproduce the error:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style>
#container{
height: 200px;
border:1px solid grey;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="monaco-editor/min/vs/loader.js"></script>
<script>
require.config({ paths: { vs: 'monaco-editor/min/vs' } });
require(['vs/editor/editor.main'], function () {
let monacoEditor = monaco.editor.create(container, {
language: 'javascript',
value: `function f(){
}
function g(){
}
`,
insertSpaces: false,
formatOnPaste: true,
});
});
</script>
</body>
</html>
When I try to insert function g inside function f, I want this:
function f(){
function g(){
}
}
But instead, most of the times I get this:
function f(){
function g(){
}
}