0

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

Mb175
  • 55
  • 9
  • Doesn't this last solution work? Setting markers as an empty array is the way to remove model markers in monaco editor. – Astor Bizard Feb 09 '22 at 09:03
  • yes it does, but shouldn't the dispose function also somewhat work ? Otherwise I compute the array of markers I want to keep externally and override all of them which seems to be kind of redundant to me – Mb175 Feb 10 '22 at 10:57
  • Actually, no, it shouldn't even execute. [According to API specification](https://microsoft.github.io/monaco-editor/api/modules/monaco.editor.html#getModelMarkers), `getModelMarkers()` returns an array of [IMarker](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IMarker.html), which do not have a `dispose()` method. `setModelMarkers` really is the preferred way for any markers set modification, including deletion. – Astor Bizard Feb 10 '22 at 13:03
  • @AstorBizard Great thanks fo the insides, im going to write an summary answer on the question. – Mb175 Feb 10 '22 at 16:57

1 Answers1

4

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.

Astor Bizard
  • 231
  • 1
  • 3
  • 11