Home Download Buy Blog Forum Support

Parenthesis don't close correctly with nested functions

Parenthesis don't close correctly with nested functions

Postby craig on Thu Mar 01, 2012 3:20 am

I'm on Mac OS 10.6.8.

Reproduce:
Start with the following code:

Code: Select all
functionCall(some_variable)

Wrap the inner variable in another function by adding the function call and opening parenthesis:

Code: Select all
functionCall(otherFunctionCall(some_variable)

Move the cursor to directly after "some_variable" and type ")" to close the parenthesis for the function you just added.

Expected:
A closing parenthesis is added.

Actual
You have to move to the end of the line or press the closing parenthesis again.

This is true for as many levels deep as you go so if you have 3 or 4 functions nested you have to press the ")" 3 or 4 times in order to close the one you just added.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Re: Parenthesis don't close correctly with nested functions

Postby atomi on Thu Mar 01, 2012 7:20 am

What language?
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Parenthesis don't close correctly with nested functions

Postby craig on Thu Mar 01, 2012 3:11 pm

This is not language specific. It happens for every language I have tried including plain text.

The simplest reproduce code is to select plain text and paste this into sublime:

Code: Select all
function1(function2(var)


Then move the cursor to directly after the "r" in var and type ")". The ) is not inserted.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Re: Parenthesis don't close correctly with nested functions

Postby atomi on Fri Mar 02, 2012 11:42 am

craig wrote:This is not language specific. It happens for every language I have tried including plain text.

The simplest reproduce code is to select plain text and paste this into sublime:

Code: Select all
function1(function2(var)


Then move the cursor to directly after the "r" in var and type ")". The ) is not inserted.


Oh. Yeah, what you can do is edit the keybinding regex that does a look behind and checks for multiple open parenthesis.
The keybinding to edit would be the close parenthesis keybinding with the move_to forward true regex.

Thats my best suggestion.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Parenthesis don't close correctly with nested functions

Postby jfountain2012 on Fri Mar 09, 2012 4:45 pm

I can confirm this is also an issue in Ubuntu.
jfountain2012
 
Posts: 1
Joined: Fri Mar 09, 2012 4:42 pm

Re: Parenthesis don't close correctly with nested functions

Postby alexrussell on Sun Mar 11, 2012 12:13 am

FYI I don't think this is a bug (per se). I think the point of the keybinding is that when ST adds in your closing brackets for free it needs to allow for people who accidentally try to close them. Thus if you're about to type a closing bracket, brace or quote and you're next to one, it'll overwrite/move the cursor forward rather than insert a new one. Consider the following use case:

Someone types
Code: Select all
var a = '

ST2 shows (| denoting insertion point cursor):
Code: Select all
var a = '|'


Developer then completes the string and types the ending quote (by mistake - ST2 already gave them one for free). Typed:
Code: Select all
some string'


ST2 realises they ended the string it already tried to end for them, and skips over the quote (rather than doubling it up):
Code: Select all
var a = 'a string'|

Rather than:
Code: Select all
var a = 'a string'|'


So that's a use case where it makes sense to skip over a bracket/brace/quote. But I understand in your situation this behaviour is unexpected and unwanted. Unfortunately there's probably not a lot you can do about that. ST2 really tried to help by always automatically closing these things whenever it sees fit (presumably based on scope), but can't always do so, thus leaving the developer having to add the ending bracket/brace/quote, and in those circumstances the behaviour I just described shouldn't be used. But presumably it's difficult for ST2 to know when and when not to use this heuristic, so it always does it, hoping that it doesn't happen much.

So I can't offer a fix, and nor am I defending the behaviour per se, just saying that I don't think it's a bug: it's there to help you out in other situations and unfortunately gets in the way in this one. The bug in my eye is more that when you type your inside open bracket, the editor doesn't automatically add a closing bracket for you. Having said that, I've noticed less of this happening lately (maybe some definitions were updated at some point).

The only time this is an issue for me now is when typing an opening bracket next to a word character, and then I understand completely that the editor can't automatically close it for me. Consider the following example:

Code: Select all
if (a == 5 && |b == 8) {
    console.log('great');
}


If I go to add an opening bracket at the insertion point just to the left of the variable 'b' for a bit more specificity, then the editor doesn't automatically close it for me (it's unlikely I'll want a closing one right next to the opening one, and the editor can't read my mind as to where it should go (should it be '(b) == 8' or '(b == 8)'?)). Then when I go to add the closing one to the right of the number '8', it doesn't appear to do anything because it thinks I'm inadvertently closing the bracket that (it assumes) it created automatically for me. This is the exact problem you're having as described in your first post. It'd be interesting to see if you agree with me that the benefits of the system outweigh this one drawback.

If you really want a fix, I think there's a setting to not dot he automatic completion of brackets and the like, which will presumably stop this closing-skip behaviour too, but then you lose the automatic closing which is so useful.
alexrussell
 
Posts: 74
Joined: Fri Jul 15, 2011 9:06 am

Re: Parenthesis don't close correctly with nested functions

Postby agibsonsw on Sun Mar 11, 2012 2:00 am

I've learned to accept this behaviour - as it's much better than the alternative of disabling bracket-completion. It's caught me out on a few occasions, when I assumed I was closing a second bracket, but was only closing the same one twice :( . On occasions I've had to type a space, then go back and delete it.

I think it's helpful to try and make use of the fact that we can highlight text and type a bracket, and ST will (bracket the text) for us. But, for anyone struggling, there are bracket-highlighting plug-ins that, presumably, are helpful? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Parenthesis don't close correctly with nested functions

Postby craig on Wed Mar 14, 2012 12:43 am

I just wanted to point out a temporary work around is to overwrite the default key bindings:

Code: Select all
{ "keys": [")"], "command": "insert", "args": {"characters": ")"} },
{ "keys": ["]"], "command": "insert", "args": {"characters": "]"} },
{ "keys": ["}"], "command": "insert", "args": {"characters": "}"} }

This still allows the matching parenthesis to be created when you open a parenthesis.

This is not ideal when you try to type function() cause you end up with function()) when you explicitly type the closing parenthesis.

What is interesting is the way TextMate behaves. Basically if a closing parenthesis is created automatically in the context of adding code then typing ) again will be ignored. I actually think this is really smart because a lot of the time I find myself typing the closing parenthesis anyway without thinking about it.

Maybe I will try to make a sublime package to deal with this.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Re: Parenthesis don't close correctly with nested functions

Postby C0D312 on Wed Mar 14, 2012 12:59 am

FIX
===

ADD TO USER PREFERENCES:
Code: Select all
"auto_match_enabled": false


ADD TO USER KEYBINDINGS:
Code: Select all
{ "keys": ["tab"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
    [
        { "key": "following_text", "operator": "regex_contains", "operand": "^[)\"'\\]\\};]", "match_all": true },
        { "key": "auto_complete_visible", "operator": "equal", "operand": false },
        { "key": "has_next_field", "operator": "equal", "operand": false }
    ]   
  }


The second thing is option but I like to use tab to move out of parenthesis.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Parenthesis don't close correctly with nested functions

Postby craig on Wed Mar 14, 2012 3:28 pm

Setting auto_match_enabled to false doesn't exactly fix the original issue it just makes it so that you have to explicitly type every character.

Having the auto match is a nice thing it's just that when you try to close one of the automatch tags it should not always ignore it.
craig
 
Posts: 13
Joined: Thu Mar 01, 2012 3:03 am
Location: New York, NY

Next

Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot] and 43 guests