【问题标题】:Syntax validation of a custom language in Monaco editorMonaco 编辑器中自定义语言的语法验证
【发布时间】:2017-02-18 19:23:42
【问题描述】:
【问题讨论】:
标签:
validation
syntax
monaco-editor
【解决方案1】:
我最近成功地完成了这项工作,我只是使用monaco-css 作为样板,我现在唯一要做的就是为我的语言和我想要的其他功能编写一个解析器。这是我的code。
在项目根目录的 lang_services 文件夹中添加您的解析器和其他语言服务。
我认为这会有所帮助。
【解决方案2】:
这是一个简洁且易于定制的示例,它将在第 1 行的 2-5 位置显示错误,如下所示:
只需将此代码插入到 https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-custom-languages 的游乐场代码的顶部(而不是底部):
monaco.editor.onDidCreateModel(function(model) {
function validate() {
var textToValidate = model.getValue();
// return a list of markers indicating errors to display
// replace the below with your actual validation code which will build
// the proper list of markers
var markers = [{
severity: monaco.MarkerSeverity.Error,
startLineNumber: 1,
startColumn: 2,
endLineNumber: 1,
endColumn: 5,
message: 'hi there'
}];
// change mySpecialLanguage to whatever your language id is
monaco.editor.setModelMarkers(model, 'mySpecialLanguage', markers);
}
var handle = null;
model.onDidChangeContent(() => {
// debounce
clearTimeout(handle);
handle = setTimeout(() => validate(), 500);
});
validate();
});
// -- below this is the original canned example code:
// Register a new language
请注意,为简单起见,此示例忽略了 onDidCreateModel 和 onDidChangeContent 都返回您可能需要跟踪和处置的 IDisposable 对象的考虑。