简体   繁体   中英

Getting errors from Monaco editor

I would like to get hold of the errors created by default by Monaco editor.

在此处输入图像描述

看起来您可以调用monaco.editor.getModelMarkers({})来获取文档中所有标记的列表,然后自己过滤以将其限制为您感兴趣的错误。我更喜欢更清楚的-记录的路线,但在我的临时测试中这是有效的。

Given a monaco model, you can have access to the worker. This is similar to a ts.LanguageService but the signatures are async. With that you can then call getSemanticDiagnostics() and all the rest.

monaco.languages.typescript.getTypeScriptWorker()
  .then(_worker=>{_worker(model.uri)
  .then(worker=>{

    worker.getScriptFileNames().then(ff=>{
      ff.forEach(sf=>{
        worker.getSemanticDiagnostics(sf).then(dd=>{          
          console.log('\n\n DIAGNOSTICS FOR '+sf)
          console.log(dd.map(d=>d.messageText))})
        })

This is a simple sample to log errors:

import * as monaco from 'monaco-editor'

// ...

monaco.editor.onDidChangeMarkers(([uri]) => {
  const markers = monaco.editor.getModelMarkers({resource: uri})
  console.log('markers:', markers.map(
    ({ message, startLineNumber, startColumn, endLineNumber, endColumn }) =>
      `${message} [${startLineNumber}:${startColumn}-${endLineNumber}:${endColumn}]`,
  ))
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM