Commit 19b61a09 authored by Jérome Perrin's avatar Jérome Perrin

monaco_editor: enable prettier as a js code formatter

This is triggered by using "Format document" from context menu.

Include prettier 1.14.0 Copyright 2017-2018 James Long
https://github.com/prettier/prettier/
parent bf385582
......@@ -8,6 +8,8 @@
<script src="rsvp.js"></script>
<script src="renderjs.js"></script>
<script src="monaco-editor/prettier@1.14.0/standalone.js"></script>
<script src="monaco-editor/prettier@1.14.0/parser-babylon.js"></script>
<script>
/*
......
......@@ -88,8 +88,30 @@
monaco.languages.html.htmlDefaults.options.format.tabSize = 2;
monaco.languages.html.htmlDefaults.options.format.insertSpaces = true;
}
if (this.state.model_language === 'javascript') {
// prettier as a formatting provider
monaco.languages.registerDocumentFormattingEditProvider(
'javascript',
{
provideDocumentFormattingEdits(model, options, token) {
const text = prettier.format(model.getValue(), {
parser: 'babylon',
plugins: [prettierPlugins.babylon],
// see http://json.schemastore.org/prettierrc for supported options.
singleQuote: true,
tabWidth: 2
});
return [
{
range: model.getFullModelRange(),
text
}
];
}
}
);
// Type mapping for Nexedi libraries
function addExtraLibrary(script_name, lib_name) {
return fetch(script_name)
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Folder" module="OFS.Folder"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>prettier@1.14.0</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>parser-babylon.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>parser-babylon.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>standalone.js</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/javascript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>standalone.js</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -46,6 +46,9 @@
<script tal:content='python: "window.monacoEditorWebPackResourceBaseUrl = " + modules["json"].dumps(options["portal_url"]) + " + \"/monaco-editor/\""'></script>
<script tal:attributes="src string:${options/portal_url}/monaco-editor/prettier@1.14.0/standalone.js"></script>
<script tal:attributes="src string:${options/portal_url}/monaco-editor/prettier@1.14.0/parser-babylon.js"></script>
<script>
/* we need to defer import for the monacoEditorWebPackResourceBaseUrl trick to work as expected in ZMI */
var $script = document.createElement("script");
......@@ -105,6 +108,24 @@ $script.onload = function() {
}
});
}
monaco.languages.registerDocumentFormattingEditProvider('javascript', {
provideDocumentFormattingEdits(model, options, token) {
const text = prettier.format(model.getValue(), {
parser: 'babylon',
plugins: [prettierPlugins.babylon],
// see http://json.schemastore.org/prettierrc for supported options.
singleQuote: true,
tabWidth: 2
});
return [
{
range: model.getFullModelRange(),
text,
},
];
},
});
var timeout = null;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment