Unexpected behavior of closeBrackets

Hi @marijn, I have been working on a Markdown editor and recently discovered this issue, while it might be design, I would like to hear your thoughts.

Let’s say I am setting up the editor like this:

import {basicSetup, EditorView} from "codemirror"
import {markdown, markdownLanguage} from "@codemirror/lang-markdown"
import {languages} from "@codemirror/language-data"

let view = new EditorView({
  doc: "Hello\n\n```javascript\nlet x = 'y'\n```",
  extensions: [
    basicSetup,
    markdown({codeLanguages: languages}),
    markdownLanguage.data.of({
    closeBrackets: {
      brackets: [
        // Default
        '(', '[', '{', '\'', '"',
        // Custom
        '`', '*', '_',
      ],
    }}),
  ],
  parent: document.body
})

I added “`” to closeBrackets, so that when I insert a backtick, it automatically closes as two backticks.

It works great and closes only before non-whitespace characters, for most cases. However, I found some exceptions.

For example (| is the caret position):

`|+

If the caret is before a plus sign and I insert a backtick, it closes as two backticks.

``|+

However, the behavior is not quite consistent, if I insert a ( instead of backtick, it won’t close it.

I am quite confused by what I’ve observed and would like to understand if it’s desired.

The code snippet can be used to reproduce the issue on Try CodeMirror.

Could you please take a look?

Thanks!

That seems entirely consistent with the behavior you describe for backtick, where it also inserts a single backtick. By default, brackets are only closed when the cursor is before whitespace or a closing punctuation token (see this option).

Thank you for the quick reply, Marijn.

So, does this mean brackets are less likely to be closed (compared to backticks)? I did notice the option you mentioned but couldn’t quite understand its implications.

Would you mind pointing me to the relevant source code? That would help me get a clearer understanding.

Thanks!

I think my original post might not be very clear, here are some examples.

I hit backtick in both cases. For comparison:

image

image

image

image

Yes, quotes and brackets where the opening and closing are different go through different code paths. What is the concrete problem you’re having with the current behavior?

Thanks. Not a problem—just curious why I set these characters (backtick and bracket, etc) using the same method, i.e., closeBrackets.brackets, and the code treats them differently.