                     if previous_char of @matching_opening_bracket
                         next_char = @get_next_char()
                         if next_char is @matching_opening_bracket[previous_char]
-                            @remove_next()
-                            return true
+                            num_not_closed_bracket = @count_not_closed_brackets previous_char
+                            if num_not_closed_bracket <= 0
+                                @remove_next()
+                                return true
                     # If the user remove the first quote of an empty string, we remove both quotes
                     else if previous_char is '"' or previous_char is "'"
                         next_char = @get_next_char()
                         if next_char is previous_char and @get_previous_char(2) isnt '\\'
-                            @remove_next()
-                            return true
+                            num_quote = @count_char char_to_insert
+                            if num_quote%2 is 0
+                                @remove_next()
+                                return true
                     return true
 
                 char_to_insert = String.fromCharCode event.which

                                    @move_cursor 1
                                    event.preventDefault()
                                return true
            return true

        get_next_char: =>
            cursor_end = @codemirror.getCursor()

                        query_after_cursor += query_lines[i] + '\n'
                    else
                        query_after_cursor += query_lines[i]

            # Initialize @current_element, which tracks what the user typed before they hit TAB (to auto-complete).
            # Tracking this helps us let the user loop over all the suggestions available for the fragment they typed (and go back to the fragment).
            # We now compute this stack earlier, because we need it for @pair_char
            # BUT we don't want to save data too early
            previous_element = @current_element
            @current_element = ''

            stack = @extract_data_from_query
