Home Download Buy Blog Forum Support

regex in user key bindings

regex in user key bindings

Postby cwolves on Sun Oct 07, 2012 10:23 pm

I'm trying to get a regex to work with my key binding. Specifically, I'm trying to match any block comments. I've gotten the following to work matching the full scope, but I can't seem to get it to work using `regex_contains` so that I can just match "comment.block". Help?

Code: Select all
  , {
      "keys"    : ["enter"]
    , "command" : "insert_snippet"
    , "args"    : {"contents": "\n* "}
    , "context" : [
       { "key" : "selector", "operator": "equal", "operand": "source.js comment.block.documentation.js" }
    ]
  }
cwolves
 
Posts: 12
Joined: Thu Jan 12, 2012 9:01 pm

Re: regex in user key bindings

Postby agibsonsw on Sun Oct 07, 2012 10:48 pm

I know nothing about this so I apologise for mis-information in advance. But I would take a guess that if there is an operator "equal" then there might also be an operator "contains" - even though I can't see one in the default key-bindings. Worth a stab, perhaps :)

You should also post your regex version..

The asterisk might also need escaping \*, but you've stated that it works..
"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: regex in user key bindings

Postby cwolves on Sun Oct 07, 2012 11:00 pm

Yeah, I don't see a contains (and the parser chokes on it anyway). There is a `regex_contains`, which I'm trying to use as such:

Code: Select all
{ "key" : "selector", "operator": "regex_contains", "operand": ".*comment.block.*" }


I've tried variations of that with or without the `.*`, using `^` and `$`, etc... Can't seem to get anything to work. I can't even get `not_regex_contains` to work (which should match everything!)
cwolves
 
Posts: 12
Joined: Thu Jan 12, 2012 9:01 pm

Re: regex in user key bindings

Postby agibsonsw on Sun Oct 07, 2012 11:29 pm

Code: Select all
    { "key" : "selector", "operator": "regex_contains", "operand": "comment\.block" }
"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: regex in user key bindings

Postby jps on Mon Oct 08, 2012 1:07 am

regex_contains only works with the preceding_text, text, and following_text keys.

To match vs all block comments, you just want to use a selector:

Code: Select all
    { "key" : "selector", "operator": "equal", "operand": "comment.block" }


You can test selectors via positioning the caret at the desired location, and then using the console:

Code: Select all
view.match_selector(view.sel()[0].a, "comment.block")
jps
Site Admin
 
Posts: 3058
Joined: Wed Mar 19, 2008 12:33 pm

Re: regex in user key bindings

Postby cwolves on Mon Oct 08, 2012 8:42 pm

Thanks jps :)
cwolves
 
Posts: 12
Joined: Thu Jan 12, 2012 9:01 pm

Re: regex in user key bindings

Postby highend on Tue Oct 09, 2012 12:40 am

jps wrote:regex_contains only works with the preceding_text, text, and following_text keys.

To match vs all block comments, you just want to use a selector:

Code: Select all
    { "key" : "selector", "operator": "equal", "operand": "comment.block" }


You can test selectors via positioning the caret at the desired location, and then using the console:

Code: Select all
view.match_selector(view.sel()[0].a, "comment.block")


Very helpful, thanks a lot.
highend
 
Posts: 93
Joined: Fri Jan 20, 2012 2:47 pm

Re: regex in user key bindings

Postby gekushner on Wed Oct 31, 2012 11:25 pm

I was looking to do this also. Does this mean that there is no way to set a key binding for more than one scope. For example I have the scope source.python and source.python.django. I tried to use the regex_contains to set a key binding for every python scope but couldn't get it to work.

thanks.
gekushner
 
Posts: 7
Joined: Wed Jul 20, 2011 11:23 pm


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 16 guests