feeley / codeboot

An in-browser JavaScript REPL which supports single-stepping
32 stars 11 forks source link

Autocompletion of closing braces when pressing return after an opening brace doesn't check if closing brace is already present #60

Closed JPDuchesne closed 11 years ago

JPDuchesne commented 11 years ago

Description: In code blocks, the editor automatically inserts a closing brace when pressing return after an opening brace, even if there already is a closing brace for that code block.

How to reproduce: 1- Type an opening brace, press return and notice that a closing brace is inserted (with an empty new line in between) 2- Go back to the opening brace line and press return to add an extra new line 3- Notice that another closing brace is inserted.

Expected behavior: The editor should detect whether or not there already is a closing brace that matches the opening brace before adding a new closing brace. That way you make sure you don't add unnecessary closing braces.

The culprit: File cm-autoclose.js If block from line 17 to 23

Ideas to fix the issue: There should be a check before line 18 in cm-autoclose.js One possibility for a quick fix is to find the next "{" or "}" character, if it is an opening brace or couldn't find a brace at all, then proceed to add a closing brace. If the next brace you find is a closing brace, then do nothing.

dufour commented 11 years ago

Looking for a matching brace is not sufficient. Consider the following situation :

function f() {
    if (c) {|
}

where | represents the cursor position. After a user pressed Enter, we would look for a matching brace, and find it on the last line. However, a brace should have been inserted in this case.

This is a tricky problem, and I don't know an editor that gets it right every time. A good heuristic could be to only perform the insertion when we find an unbalanced brace starting at the current one and walking backwards. This shouldn't be too long to implement, I can give it a shot in the next few days.

feeley commented 11 years ago

The current behaviour seem OK to me.