Commit 085cdc4e authored by Jérome Perrin's avatar Jérome Perrin

monaco_edito: add a symbol provider for python

This enables usage of @ in quick pick to search for symbols and make
sticky scroll show the current context
parent a89c7e9e
import ast
import enum
import json
class SymbolKind(enum.IntEnum):
File = 0
Module = 1
Namespace = 2
Package = 3
Class = 4
Method = 5
Property = 6
Field = 7
Constructor = 8
Enum = 9
Interface = 10
Function = 11
Variable = 12
Constant = 13
String = 14
Number = 15
Boolean = 16
Array = 17
Object = 18
Key = 19
Null = 20
EnumMember = 21
Struct = 22
Event = 23
Operator = 24
TypeParameter = 25
def ERP5Site_getPythonCodeSymbolList(self, data, REQUEST=None):
"""Get symbols from python code
"""
data = json.loads(data)
symbols = []
class Visitor(ast.NodeVisitor):
def addSymbol(self, symbol, current_symbol):
if current_symbol:
current_symbol['children'].append(symbol)
else:
symbols.append(symbol)
def visit_Module(self, node):
self.visitChildren(node, None)
def visitChildren(self, node, current_symbol):
for child in node.body:
if isinstance(child, ast.FunctionDef):
self.visitDef(child, current_symbol)
if isinstance(child, ast.ClassDef):
self.visitClassDef(child, current_symbol)
def visitDef(self, node, current_symbol):
def_symbol = self.getSymbol(
node,
SymbolKind.Method if (
current_symbol is not None
and current_symbol['kind'] == SymbolKind.Class) else
SymbolKind.Function,
)
self.addSymbol(def_symbol, current_symbol)
self.visitChildren(node, def_symbol)
def visitClassDef(self, node, current_symbol):
class_symbol = self.getSymbol(node, SymbolKind.Class)
self.addSymbol(class_symbol, current_symbol)
self.visitChildren(node, class_symbol)
def getSymbol(self, node, kind):
endLineNumber, endColumn = self.getEndPosition(node)
sym = {
"kind": int(kind),
"name": node.name,
"tags": [],
"range": {
"startColumn": node.col_offset,
"startLineNumber": node.lineno,
"endColumn": endColumn,
"endLineNumber": endLineNumber,
},
"children": [],
}
sym['selectionRange'] = sym['range']
return sym
def getEndPosition(self, node):
if not hasattr(node, "body") or len(node.body) == 0:
return (node.lineno, node.col_offset)
return self.getEndPosition(node.body[-1])
try:
tree = ast.parse(data['code'].encode('utf-8'))
except SyntaxError:
pass
else:
visitor = Visitor()
visitor.visit(tree)
if REQUEST:
REQUEST.RESPONSE.setHeader('content-type', 'application/json')
return json.dumps(symbols)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Extension Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>default_reference</string> </key>
<value> <string>MonacoEditorUtils</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>extension.erp5.MonacoEditorUtils</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ExternalMethod" module="Products.ExternalMethod.ExternalMethod"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_function</string> </key>
<value> <string>ERP5Site_getPythonCodeSymbolList</string> </value>
</item>
<item>
<key> <string>_module</string> </key>
<value> <string>MonacoEditorUtils</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_getPythonCodeSymbolList</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -200,6 +200,43 @@ ...@@ -200,6 +200,43 @@
.push(addExtraLibrary('./monaco-renderjs.d.ts', 'renderjs')) .push(addExtraLibrary('./monaco-renderjs.d.ts', 'renderjs'))
.push(addExtraLibrary('./monaco-jio.d.ts', 'jio')); .push(addExtraLibrary('./monaco-jio.d.ts', 'jio'));
} }
if (this.state.model_language === 'python') {
const documentSymbolProvider = {
provideDocumentSymbols: function (model, token) {
const controller = new AbortController();
token.onCancellationRequested(() => {
controller.abort();
});
const data = new FormData();
data.append('data', JSON.stringify({ code: model.getValue() }));
return fetch(
new URL(
'ERP5Site_getPythonCodeSymbolList',
location.href
).toString(),
{
method: 'POST',
body: data,
signal: controller.signal
}
).then(
(response) => response.json(),
(e) => {
if (!(e instanceof DOMException) /* AbortError */) {
throw e;
}
/* ignore aborted requests */
}
);
}
};
monaco.languages.registerDocumentSymbolProvider(
'python',
documentSymbolProvider
);
}
if (modification_dict.hasOwnProperty('editable')) { if (modification_dict.hasOwnProperty('editable')) {
this.editor.updateOptions({ readOnly: !this.state.editable }); this.editor.updateOptions({ readOnly: !this.state.editable });
} }
......
extension.erp5.MonacoEditorUtils
\ No newline at end of file
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ZopePageTemplate" module="Products.PageTemplates.ZopePageTemplate"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/html</string> </value>
</item>
<item>
<key> <string>expand</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>testCodeEditorPythonSymbols</string> </value>
</item>
<item>
<key> <string>output_encoding</string> </key>
<value> <string>utf-8</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <unicode>Code Editor Python Symbols</unicode> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<html xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:metal="http://xml.zope.org/namespaces/metal">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title tal:content="template/title_and_id"></title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr>
<td rowspan="1" colspan="3" tal:content="template/title_and_id"></td>
</tr>
</thead>
<tbody>
<tal:block metal:use-macro="here/Zuite_CommonTemplate/macros/init" />
<tr>
<td>open</td>
<td>${base_url}/foo_module/ListBoxZuite_reset</td>
<td></td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>Reset Successfully.</td>
<td></td>
</tr>
<tr>
<td colspan="3"><b>Set preferred code editor</b></td>
</tr>
<tr>
<td>open</td>
<td>${base_url}/portal_preferences/erp5_ui_test_preference/Preference_viewHtmlStyle</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//input[@name="field_my_preferred_source_code_editor" and @value="monaco"]</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//button[@name='Base_edit:method']</td>
<td></td>
</tr>
<tr>
<td colspan="3"><b>Switch to renderjs UI and edit components</b></td>
</tr>
<tr>
<td>open</td>
<td>${base_url}/web_site_module/renderjs_runner/#/portal_components?editable=true</td>
<td></td>
</tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_app_loaded" />
<tr>
<td>waitForElementPresent</td>
<td>//a[@data-i18n='Add']</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>link=Add</td>
<td></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>//select[@name='field_your_select_action']</td>
<td></td>
</tr>
<tr>
<td>select</td>
<td>//select[@name='field_your_select_action']</td>
<td>label=Document Component</td>
</tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/submit_dialog" />
<tal:block tal:define="notification_configuration python: {'class': 'success',
'text': 'Object created.'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
<tr>
<td colspan="3"><b>Wait for editor to be loaded and edit</b></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>//div[@data-gadget-scope="editor"]//iframe</td>
<td></td>
</tr>
<tr>
<td>selectFrame</td>
<td>//div[@data-gadget-scope="editor"]//iframe</td>
<td></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>css=div.monaco-editor.vs</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>selenium.browserbot.getCurrentWindow().document.querySelector('div.monaco-editor.vs').getAttribute('data-uri')
</td>
<td>model-data-uri</td>
</tr>
<tr>
<td>assertEval</td>
<td>selenium.browserbot.getCurrentWindow().monaco.editor.getModel(storedVars['model-data-uri']).setValue("")
</td>
<td>null</td>
</tr>
<tr>
<td>selectFrame</td>
<td>relative=top</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//button[@data-i18n="Save"]</td>
<td></td>
</tr>
<tal:block tal:define="notification_configuration python: {'class': 'success',
'text': 'Data updated.'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
<tr>
<td>selectFrame</td>
<td>//div[@data-gadget-scope="editor"]//iframe</td>
<td></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>css=div.monaco-editor.vs</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>selenium.browserbot.getCurrentWindow().document.querySelector('div.monaco-editor.vs').getAttribute('data-uri')
</td>
<td>model-data-uri</td>
</tr>
<tr>
<td>assertEval</td>
<td>selenium.browserbot.getCurrentWindow().monaco.editor.getModel(storedVars['model-data-uri']).setValue("def foo():\n pass")
</td>
<td>null</td>
</tr>
<tr>
<td>assertEval</td>
<td>selenium.browserbot.getCurrentWindow().monaco.editor.getEditors()[0].focus()
</td>
<td>null</td>
</tr>
<tr>
<td>assertEval</td>
<td>(function() { selenium.browserbot.getCurrentWindow().monaco.editor.getEditors()[0].getAction('editor.action.quickOutline').run(); return "ok"})()
</td>
<td>ok</td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>//*[@widgetid="editor.contrib.quickInputWidget"]//a//span[contains(@class, "codicon-symbol-function")]</td>
<td></td>
</tr>
<tr>
<td>assertText</td>
<td>//*[@widgetid="editor.contrib.quickInputWidget"]//a//span[contains(@class, "codicon-symbol-function")]/..</td>
<td>foo</td>
</tr>
<tr>
<td>selectFrame</td>
<td>relative=top</td>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
\ No newline at end of file
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