codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.44k stars 343 forks source link

After setting the error diagnostics by using setDiagnostics, when copying & pasting the complete text, the entire document prompts error. #1394

Closed 819947516 closed 2 weeks ago

819947516 commented 2 weeks ago

Describe the issue

  1. Use setDiagnostics set error diagnostics
  2. When the position of diagnostics is at the end of the document, like this: image
  3. Copy and paste the all document, All the contents in the editor prompt errors. 20240614163418_rec_
import {EditorView, basicSetup} from "codemirror"
import {javascript} from "@codemirror/lang-javascript"
import {tooltips} from "@codemirror/view";
import {setDiagnostics} from "@codemirror/lint";

(window as any).view = new EditorView({
  doc: `# no error

error('')`,
  extensions: [
    basicSetup,
    javascript(),
    tooltips({
      parent: document.body
    })
  ],
  parent: document.body
})

const addDiagnostics = () => {
  const view = (window as any).view
  view.dispatch(setDiagnostics(view.state, [
    {
      from: 13,
      to: 21,
      severity: 'error',
      message: "error message",
    },
  ]))
}

const div = document.createElement('div')
div.textContent = 'addDiagnostics'
div.addEventListener('click', () => {
  addDiagnostics()
})

document.body.appendChild(div)

Browser and platform

No response

Reproduction link

No response

marijnh commented 2 weeks ago

This was caused by the fact that these markers were set as inclusive, meaning that they'd apply to anything inserted at their start or end (context). That doesn't seem a good idea, so attached patch makes them non-inclusive.

819947516 commented 2 weeks ago

Your speed of modifying the problem is too fast.