diff --git a/CHANGELOG.md b/CHANGELOG.md index 83555f5b..4aca2c99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ ### Changed - Changed snippet (autocomplete) content and behavior [issue#471]. +### Fixed +- Fixed auto-indent not working [issue#470]. + ## [1.1.0-beta.2] - 2021-07-06 ## Changed @@ -38,6 +41,7 @@ Prerelease changes are documented at [support#48]. +[issue#470]: https://github.com/pybricks/pybricks-code/issues/470 [issue#471]: https://github.com/pybricks/pybricks-code/issues/471 [support#48]: https://github.com/pybricks/support/issues/48 [support#378]: https://github.com/pybricks/support/issues/378 diff --git a/src/editor/Editor.tsx b/src/editor/Editor.tsx index acb0794c..ef54576a 100644 --- a/src/editor/Editor.tsx +++ b/src/editor/Editor.tsx @@ -42,6 +42,10 @@ monaco.languages.register({ id: pybricksMicroPythonId }); const toDispose = new Array(); toDispose.push( + monaco.languages.setLanguageConfiguration( + pybricksMicroPythonId, + pybricksMicroPython.conf, + ), monaco.languages.setMonarchTokensProvider( pybricksMicroPythonId, pybricksMicroPython.language, diff --git a/src/editor/pybricksMicroPython.ts b/src/editor/pybricksMicroPython.ts index 83bce69b..f1e6c085 100644 --- a/src/editor/pybricksMicroPython.ts +++ b/src/editor/pybricksMicroPython.ts @@ -1,4 +1,4 @@ -// Copied from https://github.com/microsoft/monaco-languages/blob/main/src/python/python.ts +// Copied from https://github.com/microsoft/monaco-languages/blob/d7cc098c481059f63d51ce3753975c8ca8ab6030/src/python/python.ts /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. @@ -7,6 +7,47 @@ import { monaco } from 'react-monaco-editor'; +export const conf: monaco.languages.LanguageConfiguration = { + comments: { + lineComment: '#', + blockComment: ["'''", "'''"], + }, + brackets: [ + ['{', '}'], + ['[', ']'], + ['(', ')'], + ], + autoClosingPairs: [ + { open: '{', close: '}' }, + { open: '[', close: ']' }, + { open: '(', close: ')' }, + { open: '"', close: '"', notIn: ['string'] }, + { open: "'", close: "'", notIn: ['string', 'comment'] }, + ], + surroundingPairs: [ + { open: '{', close: '}' }, + { open: '[', close: ']' }, + { open: '(', close: ')' }, + { open: '"', close: '"' }, + { open: "'", close: "'" }, + ], + onEnterRules: [ + { + beforeText: new RegExp( + '^\\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async).*?:\\s*$', + ), + action: { indentAction: monaco.languages.IndentAction.Indent }, + }, + ], + folding: { + offSide: true, + markers: { + start: new RegExp('^\\s*#region\\b'), + end: new RegExp('^\\s*#endregion\\b'), + }, + }, +}; + export const language = { defaultToken: '', tokenPostfix: '.python',