monaco-editorngx-monaco-editor

How to remove Model-Markers in MonacoEditor


Currently, I'm trying to remove model-markers from a Monaco-Model with the following approach.

    monac.editor.getModelMarkers({owner: "owner"}).forEach(marker => {
      console.log(marker)
      marker.dispose()
    })

Markers currently are set through the setModelMarker() method

monaco.editor.setModelMarkers(editor.getModel(), "owner", markers)

Setting an empty array to the Model override the model-markers, so this could be used as a workaround.

monaco.editor.setModelMarkers(editor.getModel(), "owner", [])

Is there any way how I could reliably remove model-markers


Solution

  • As pointed out in the comments, getModelMarkers() returns an array of IMarker, which do not have a dispose() method.
    For any modification of the marker set, setModelMarkers() is the way.
    The main explanation is that IMarkers are just interface object, which must thus have fields like severity, startLineNumber, etc.
    The Monaco editor uses a single setModelMarkers() method to update the marker set, as any modification of these objects after calling the method will have no effect:

    var markers = [{
        severity: monaco.MarkerSeverity.Warning,
        message: "Some warning",
        startLineNumber: 1,
        startColumn: 1,
        endLineNumber: 1,
        endColumn: editor.getModel().getLineLength(1) + 1
    }];
    monaco.editor.setModelMarkers(editor.getModel(), "owner", markers);
    markers[0].severity = monaco.MarkerSeverity.Error; // No effect
    

    With the same idea, you cannot delete a marker (with delete markers[0]; for example), as the delete operator deletes only a reference.