microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
39.26k stars 3.52k forks source link

View Zones move to wrong position #1858

Open davidscherer opened 4 years ago

davidscherer commented 4 years ago

monaco-editor version: 0.20.0 Browser: Firefox, Chromium OS: Linux Playground code that reproduces the issue:

let ed = monaco.editor.create(document.getElementById("container"), {
    value: "1. Insert more lines here by pressing enter:\n\nZone should always be below this line.\n\n\n2. Delete most of this line to trigger ViewZones._recomputeWhitespaceProps()",
    wordWrap: "wordWrapColumn",
    wordWrapColumn: 40
});

ed.changeViewZones((vzChanger)=>{
    let domNode = document.createElement("div")
    domNode.style.background = "#ff0000"
    vzChanger.addZone({
        afterLineNumber:3,
        heightInLines: 1,
        domNode
    })
})

Any time a view zone is moved to a different absolute line number by text flow, and then something happens anywhere in the document that triggers ViewZones._recomputeWhitespaceProps(), the view zone winds up in the wrong place.

You can also get this to happen in VS Code in a somewhat convoluted way:

  1. Open a typescript file
  2. Make an error that gets you a red squiggle
  3. Press F8, or mouse over the red squiggle and click "Peek Problem", to get a view zone/overlay widget combination displaying the error message
  4. Insert or remove one or more lines above the error. The zone flows with the text as expected.
  5. Toggle word wrap with Alt-Z, or fold or unfold something anywhere in the file, or (if word wrap is on) change the height of a word wrapped line anywhere in the document
  6. Now the error is displayed in the wrong place (at its original absolute line number)

The problem seems to be that ViewZones._recomputeWhitespaceProps() calls ViewZones._computeWhitespaceProps() which looks at IViewZone.afterLineNumber and expects it to be up to date. This is a reasonable assumption for the other callers of _computeWhitespaceProps, which are only called when adding or changing the layout of a zone, when the creator of the ViewZone is specifying a point in the file to insert the zone. But it is not reasonable at an arbitrary time later, when text flow may have moved the ViewZone to a different absolute line number but there is no obvious way to update afterLineNumber accordingly.

I assume that _recomputeWhitespaceProps() exists for a reason, but when I stub it out with "return false" it seems to solve this problem. I can't propose a specific fix without better understanding what the function is trying to do.

terpimost commented 4 years ago

@alexdima I can confirm that weird playground bug Screen Cast 2020-05-18 at 6 25 56 PM

In another example https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-listening-to-mouse-events adding new line and folding a block of code causing view zone displacement Screen Cast 2020-05-18 at 6 30 01 PM