Codemirror: Auto-close brackets: Inserting ')' ignores

Created on 31 Mar 2018  路  4Comments  路  Source: codemirror/CodeMirror

Auto-close brackets: inserting ) is ignored if next character is also )

Example editor session repro (you can play along at https://codemirror.net/demo/closebrackets.html ):

Foo(this);

_Oops, on this line I forgot to call Fix(this). So I move the cursor |:_

Foo(|this);

_I type Fix(, and then move the cursor to after this:_

Foo(Fix(this|);

_and type the missing )_

EXPECTED: Foo(this)|);

OBSERVED: the cursor moves right, ignoring the "redundant" ):
Foo(Fix(this)|;

But it's not really redundant; the parens were unbalanced and I was trying to balance them.

WORKAROUND: Move cursor to right-end of block of ), and then insert a new )

I guess the design choice is based on an assumption that people who want to auto-close brackets are expected to also use ) to move outside of the matched parens they just filled in (because they are in the habit of _not_ having the auto-paired brackets already inserted?)
That rationale (which I am only speculating) seems strange to me, as expecting users to type ) undermines the value-add of auto-inserting it when the ( was created.

This behavior is especially frustrating when I am inserting a paren in a block of several parents, since it's easy to not notice that the paren didn't insert, and it's a pain to go back and recount all the parens to figure out (after a compile error) where the missing paren should go.

FIX?: (I think?) the code for this behavior is here:

https://github.com/codemirror/CodeMirror/blob/master/addon/edit/closebrackets.js#L130

where the handler decides to "skip" (--> "goCharRight") instead of (I guess) CodeMirror.Pass which I think would allow the ) to be inserted as a regular character.

Repro notes:
I used Chrome 65 on Mac High Sierra, but I don't think that matters.

All 4 comments

Does bracket-closing work differently in any other software? Sublime Text does the same, and I think it's very reasonable behavior鈥攜ou can type another ')' to create the final balancing one.

Indeed, SublimeText matches CodeMirror in this regard.

Some people find this SublimeText behavior annoying, and there is a way to disable it with user config:
https://stackoverflow.com/questions/14032041/how-to-prevent-sublime-text-2-from-swallowing-closing-brackets-quotes-and-paren

I tested IntelliJ (CLion) and Eclipse 4.7 for comparison:

IntelliJ is very clever: It will check to see if parens are balanced, and will treat ) as "move right" if and only if parens are already balanced. IMO this is the ideal behavior, but takes a lot more static syntax analysis than CodeMirror currently performs (right?).

Eclipse is moderately clever: It will will treat ) as "move right" only while in a special "auto-close fill-in" mode while filling in content inside of autoclosed brackets. There's some threshold (I'm not sure what, something like moving the cursor outside of the brackets, or inserting more brackets inside the auto-brackets) after which it it stops skipping over ")". This is a better and worse than SublimeText: it won't ignore necessary brackets, but it also adds extra brackets sometimes.
I think Eclipse's behavior is better than SublimeText's behavior, since I'd rather the tool bias toward respecting user input over being more magically helpful, if it can't be perfectly magical. But I can see how user opinions would vary based on how often they need one behavior vs the other.

Since getting this behavior right for all the edge cases (InelliJ-style) takes a bunch of work:
Until/Unless CodeMirror can count parens to determine when a paren should be skipped, I think that a good cheap compromise would be providing an option to disable the paren-skipping would be a small amount of work that would make many users happy. I think that would require adding a preference setting, and checking it in the one if block that decides to paren-skip.

I'd accept such a pull request, but I'm closing this issue since I don't consider this a bug.

If you ever use the classic Microsoft Visual Studio or the new VS Code. They can properly detect the ")" you inserted is not matched and you want to add an ending ")".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KnightYoshi picture KnightYoshi  路  6Comments

niftylettuce picture niftylettuce  路  4Comments

liviuignat picture liviuignat  路  3Comments

benrbray picture benrbray  路  6Comments

Pomax picture Pomax  路  3Comments