I would like to get hold of the errors created by default by Monaco editor.
Getting errors from Monaco editor
Asked Answered
It looks like you can call monaco.editor.getModelMarkers({})
to get the list of all markers in the document, and then filter that yourself to limit it to the errors you're interested in. I would prefer a more clearly-documented route, but in my ad-hoc testing this works.
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}]`,
))
})
As of Oct 2022 this works with @monaco-editor/react, but TS is NOT happy with it. –
Popovich
© 2022 - 2024 — McMap. All rights reserved.