using Python in Monaco Editor

47 views Asked by At

I am trying to use the dev version of Monaco, there is directory with basic-languages, can this be used to embed python in monaco? if yes, how can i do it in the DOM.

Thanks

require.config({ paths: { 'vs': 'dev/vs' }});

require(['vs/editor/editor.main', 'vs/basic-languages/python/python.js'], function () {
     monaco.editor.create(document.getElementById('container'), {
         value: [
             'import sys',
             '',
             'def greet(name):',
             '    print(f"Hello, {name}!")',
             '',
             'if __name__ == "__main__":',
             '    name = sys.argv[1]',
             '    greet(name)'
         ].join('\n'),
         language: 'python'
     });
 });
1

There are 1 answers

2
Mike Lischke On

The folders in basic-languages/ contain files to provide basic support for their language in Monaco, which usually means: regex based syntax highlighting and brace mapping. Nothing else.

Supporting a language fully (e.g. Python) requires quite some effort. You have to implement various providers (code completion, syntax/semantic highlighter, definition provider, formatting provider and more). This is not a trivial task and many of these providers require help from a parser or a language server to parse code, do error checks, to provide information about syntactic elements (e.g. what is a string) and semantic structures (e.g. a class) etc. This is a big task and cannot be answered in a single question or discussion.