Monaco Editor custom formatters

故事扮演 提交于 2020-02-24 05:16:09

问题


I am trying to use the Monaco Editor by Microsoft in a project I am currently developing. I have looked through the documentation and see that you can setup a custom language with custom code completion and syntax highlighting, but I cannot find any information on how we can add custom formatting to the custom language as well.

Is this a possibility?


回答1:


Read the docs: registerDocumentFormattingEditProvider

You must create a new DocumentFormattingEditProvider and then pass it to monaco.languages.registerDocumentFormattingEditProvider. For example:

const cssFormatProvider = {
    provideDocumentFormattingEdits(model, options, token) {
        return [{
            text: YourFormatter(model.getValue()) // put formatted text here
            range: model.getFullModelRange()
        }];
    }
};
const languageId = 'css';

monaco.languages.registerDocumentFormattingEditProvider(languageId, cssFormatProvider);


来源:https://stackoverflow.com/questions/45340967/monaco-editor-custom-formatters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!