Home Download Buy Blog Forum Support

Tiny "bug" with automatically inserted characters

Tiny "bug" with automatically inserted characters

Postby edanm on Thu Apr 15, 2010 8:48 am

When you insert a character like a quote ' or a bracket [, Sublime will helpfully insert the closing character.
This is great, but the implementation has one tiny problem:
If I insert a bracket [, then delete it, I'm left with the automatically inserted closing bracket.

This hurts a lot when you're trying to type without looking at the screen: suddenly, if you make a mistake and delete it, which I often do when coding, you're left with lots of garbage characters filling your line.

The solution is just to delete the extra added character when deleting the original.

Edan
P.S. I'm pretty sure this behavior isn't in a user package and is part of Sublime proper.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Tiny "bug" with automatically inserted characters

Postby edanm on Mon Apr 26, 2010 9:11 am

Another small idea in the same spirit:

Sublime will automatically overwrite "special" characters for you. For example, if my cursor is next to a "(" and I try to insert a "(", it will be overwritten.
This is for the same reason I wrote about earlier, to help people when Sublime automagically completes all sorts of characters.

The problem is, this happens all the time. Even if my cursor is next to a special character that *wasn't* automagically written for me, it will be overwritten. This is very weird, and oftentimes annoying, behavior.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Tiny "bug" with automatically inserted characters

Postby edanm on Fri May 07, 2010 12:07 pm

I keep running into this and it is very annoying.

Am I the only one having this issue? Cause I see that no one else has responded. Is this even happening to anyone else?
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Tiny "bug" with automatically inserted characters

Postby guillermooo on Fri May 07, 2010 12:50 pm

Yes, it's a feature. Sometimes I think about switching it off, but on the whole I think it does help. It spares me some gymnastics in typing curly brackets and square brackets with my keyboard layout. I get tripped up by it a lot too, though.

An alternative is to define snippets for (), {}, [], etc. But this approach forces you to think in advance too.

Brackets should appear where you need them just by thinking about it, shouldn't they? :P Maybe Jon can implement this in a future release.
guillermooo
 
Posts: 723
Joined: Thu Jul 23, 2009 9:06 am

Re: Tiny "bug" with automatically inserted characters

Postby edanm on Fri May 07, 2010 1:19 pm

No, the autocompleting is definitely a good thing.
The only difference should be that autocompleted chars should be deleted when the first one is deleted.

This just keeps getting in my way all the time...
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Tiny "bug" with automatically inserted characters

Postby guillermooo on Fri May 07, 2010 2:40 pm

This should do the trick too (if we're talking about the same thing):
Code: Select all
   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="}" />
      <context name="allPrecedingCharacter" value="{" />
   </binding>
   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="]" />
      <context name="allPrecedingCharacter" value="[" />
   </binding>
   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value=")" />
      <context name="allPrecedingCharacter" value="(" />
   </binding>
guillermooo
 
Posts: 723
Joined: Thu Jul 23, 2009 9:06 am

Re: Tiny "bug" with automatically inserted characters

Postby Anomareh on Fri May 07, 2010 3:50 pm

Oh man that is so awesome! Thank you for posting that. That has been driving me INSANE forever. Should be part of the default keybindings IMO.

e: Don't know why I never bothered trying to disable these glitches, as they've been driving me nuts for a while. I also added bindings like above for quotes. The last thing that was mentioned that I'd like to turn off is the eating of the key press when you try to insert 2 closing braces next to each other. How exactly do you go about unbinding something like that from the user keybindings? I've just commented out the bindings in the default bindings file but that's just going to get overwritten come updates.
Anomareh
 
Posts: 221
Joined: Thu Oct 01, 2009 7:32 pm

Re: Tiny "bug" with automatically inserted characters

Postby edanm on Fri May 07, 2010 4:10 pm

@guillermooo: Awesome! Thanks so much. This has definitely been at the top of my "annoying" lists for a while.

@Anomareh: Yeah trying to insert two closing brace characters next to each other is annoying. The way it should work, imo, is that it only overrides a quote character if it was previously auto-inserted, and you haven't moved to a different line. I have no idea how to bind that though.

If anyone is interested, here's @guillermooo's code, but also including a fix for quotes:
Code: Select all
<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="}" />
      <context name="allPrecedingCharacter" value="{" />
   </binding>

   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="]" />
      <context name="allPrecedingCharacter" value="[" />
   </binding>

   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value=")" />
      <context name="allPrecedingCharacter" value="(" />
   </binding>

   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="'" />
      <context name="allPrecedingCharacter" value="'" />
   </binding>

   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="&quot;" />
      <context name="allPrecedingCharacter" value="&quot;" />
   </binding>
   <binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
      <context name="allFollowingCharacter" value="<" />
      <context name="allPrecedingCharacter" value=">" />
   </binding>




EDIT: Added a fix for html brackets (<) as well.
Last edited by edanm on Fri May 14, 2010 12:23 pm, edited 1 time in total.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm

Re: Tiny "bug" with automatically inserted characters

Postby guillermooo on Fri May 07, 2010 6:38 pm

This should work reasonably well for the second annoyance regarding brackets:
Code: Select all
import sublime, sublimeplugin
import re

# FIXME: doesn't work with multiple selections.
class InsertUnbalancedBracketCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        matchingPairs = { "]":"\[", ")":"\(", "}":"\{" }
        openChar = matchingPairs[args[0]]

        openCharsRegex = re.compile(openChar)
        closeCharsRegex = re.compile("\%s" % args[0])

        currLine = view.substr(view.line(view.sel()[0]))
        closeCharProbablyNeeded = (len(openCharsRegex.findall(currLine)) !=
                                    len(closeCharsRegex.findall(currLine)))

        if closeCharProbablyNeeded:
            view.runCommand("insertAndDecodeCharacters %s" % args[0])
        else:
            view.runCommand("move characters 1")


Code: Select all
<binding key="/([\}\)\]])/" command="insertUnbalancedBracket $1">
    <context name="allFollowingText" value="[\}\)\]]" />
</binding>


EDIT: This sample plugin has a bug that won't let you insert closing brackets in certain conditions.
Last edited by guillermooo on Wed May 12, 2010 8:52 am, edited 1 time in total.
guillermooo
 
Posts: 723
Joined: Thu Jul 23, 2009 9:06 am

Re: Tiny "bug" with automatically inserted characters

Postby edanm on Fri May 07, 2010 9:05 pm

That's an interesting approach, I'll have to look into it tomorrow morning.

I was actually thinking of the using the new api to define a new context, something only available in the latest beta.
edanm
 
Posts: 131
Joined: Thu Mar 04, 2010 11:05 pm


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 10 guests

cron