Home Download Buy Blog Forum Support

Python syntax - import/from

Python syntax - import/from

Postby agibsonsw on Wed Mar 21, 2012 11:02 pm

Hello. Modifying the 'Python.tmLanguage' file I can capture the words import/from/as so as to colour them within a theme-file using:

Code: Select all
<string>\b(?:(import)|(from)|(as))\b</string>

Is it possible to capture the other words (the libraries) that occur on the same line, so that I might colour them differently?

However, I would be happy just to capture the whole line that begins with either import or from :)

Also, is it technically possible to then reference the captured library-names elsewhere in the Python file? I'm thinking that all references to the library could use the same colour that is given to them on the import/from line. [I'm thinking it's either not possible, or v.difficult :o ]

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: Python syntax - import/from

Postby atomi on Wed Mar 21, 2012 11:17 pm

agibsonsw wrote:
Code: Select all
<string>\b(?:(import)|(from)|(as))\b</string>

Is it possible to capture the other words (the libraries) that occur on the same line, so that I might colour them differently?

Also, is it technically possible to then reference the captured library-names elsewhere in the Python file? I'm thinking that all references to the library could use the same colour that is given to them on the import/from line. [I'm thinking it's either not possible, or v.difficult :o ]

Andy.


Yeah, edit the regex to
Code: Select all
<string>\b(?:(import)|(from)|(as))\b([\w]+)</string>

Or you can add a new dict match for each individual package or you can just match the entire line like you said with a greedy asterisk period match (.*).
You have lots of options and all the examples you should need to learn them are in the tmLanguage files.
I find regex to be fun and I hope you like it too because you will need to know it well to edit these files.
Also there are 11 groups you can use to scope your sytanx in Sublime which uses the TextMate scopes which are listed here:
http://manual.macromates.com/en/language_grammars

Good luck.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Python syntax - import/from

Postby agibsonsw on Wed Mar 21, 2012 11:50 pm

Hello @atomi and thank you.

I've been working in the dark a little, as the ST documentation is limited. That link should prove v. useful ;)

Code: Select all
<string>\b(?:(import)|(from)|(as))\b([\w]+)</string>

I believe this would capture a word, but not comma separated items:

import sublime, sublime_plugin

I would like to capture the words if possible (although I might revert to .* in the end)?

I'm assuming it's not possible to reference the captured words later on :cry:. The linked page confirms "it is not possible to use a pattern that matches multiple lines".

Ta, 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: Python syntax - import/from

Postby atomi on Thu Mar 22, 2012 12:34 am

You can just add a comma

Code: Select all
<string>\b(?:(import)|(from)|(as))\b([\w,]+)</string>


These matches will work throughout the entire page.
If you want to match blocks of code use begin and end keys and use patterns key for matching inside those code blocks as long as there is a well defined end match.
You can also use include key to include syntax definitions inside matching code blocks.

Again play with the tmLanguage file. It's easy to check the scope you've defined by pressing ctrl+shift+alt+p
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Python syntax - import/from

Postby agibsonsw on Thu Mar 22, 2012 5:55 pm

@atomi. Just to say thanks again. I shall explore ;)
"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: Python syntax - import/from

Postby atomi on Thu Mar 22, 2012 6:11 pm

agibsonsw wrote:@atomi. Just to say thanks again. I shall explore ;)


No problem. I've been hanging out here kind of bored, recovering from a minor surgery - I'll be fine and back to coding in a couple of weeks. :D
I'm glad I could help.

Also check out these examples I posted for others

viewtopic.php?p=27280#p27280
viewtopic.php?p=27197#p27197

The tmLanguage files seem complicated at first but it's not too bad once you start to separate all the xml elements out.
You can spread the various dict elements out so you can focus on them better. The xml tends to blend together at first.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Python syntax - import/from

Postby agibsonsw on Fri Mar 23, 2012 6:34 pm

Related to this topic ;) I'm sort of botching the 'Python.tmLanguage' file to achieve what I'm after. The following code allows me to colour-code specific methods. However, I cannot persuade it to insist on a leading dot . before the method call. No matter how I modify it - using (?:\.) \.{1} or (?&lt;=\.) the method drops into the 'meta.function-call.python' scope.

Is there a way I can achieve this behaviour? Either by modifying my expression or the 'meta.function-call.python' equivalent expression? I would like to achieve this without having to delve into a substantial editing (begin, end, endCaptures, etc.) of the tmLanguage file.

Andy.

Code: Select all
<dict>
   <key>captures</key>
   <dict>
      <key>1</key>
      <dict>
         <key>name</key>
         <string>support.function.library.python</string>
      </dict>
   </dict>
   <key>match</key>
   <string>(?x)(?:[a-zA-Z_][a-zA-Z0-9_]*\.)*(fromtimestamp|getmtime|itemgetter|split|uuid[1345]{1})\s*(?=\()</string>
</dict>
"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: Python syntax - import/from

Postby atomi on Fri Mar 23, 2012 9:02 pm

This is more of a regex question.

agibsonsw wrote:The following code allows me to colour-code specific methods. However, I cannot persuade it to insist on a leading dot . before the method call.
Code: Select all
   <key>match</key>
   <string>(?x)(?:[a-zA-Z_][a-zA-Z0-9_]*\.)*(fromtimestamp|getmtime|itemgetter|split|uuid[1345]{1})\s*(?=\()</string>


What do you mean persuade it to insist on a leading dot? If you need this rule to absolutely always match a dot you use an escaped dot (\.)
Here is a cool regex tester you can use if you want to make sure your regex is correct (someone posted earlier) http://gskinner.com/RegExr/

The best way to learn regex is to actually read what all the symbols mean: here is a cheatsheet http://www.cheatography.com/davechild/c ... pressions/
Great resource here too http://www.regular-expressions.info/

If you still can't get it to work try posting the code you want to match and it's variations along with your regex attempt.
You'll get more help that way too. GL HTH.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Re: Python syntax - import/from

Postby agibsonsw on Fri Mar 23, 2012 9:30 pm

Hello @atomi and thank you for your response.

I'm quite/reasonably comfortable with regex. Put another way, I want the default Python.tmLanguage to capture only the word 'mycall' (and brackets) - and to require a leading dot - for the scope 'meta.function-call.python' in:

Code: Select all
someobj.mycall()


Currently, it grabs everything before the brackets(), whether or not there is a dot involved:

Code: Select all
<string>meta.function-call.python</string>
         <key>patterns</key>
         <array>
            <dict>
               <key>begin</key>
               <string>(?=[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*\s*\()</string>
               <key>end</key>
               <string>(?=\s*\()</string>
               <key>patterns</key>
"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: Python syntax - import/from

Postby atomi on Sun Mar 25, 2012 4:23 pm

Did you get this working how you wanted?

If you want to scope only the function call and not the objects preceding it use beginCaptures key.
Every key number in the beginCaptures array dict eg
Code: Select all
<key>1</key>
corresponds to a back reference in your "begin" regex string (unless it's a non-capturing group).
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US

Next

Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests