Home Download Buy Blog Forum Support

Syntax colour for ST API

Re: Syntax colour for ST API

Postby agibsonsw on Sat Mar 10, 2012 10:50 pm

rchl wrote:Sublime has "Show Unsaved Changes..." option in the context menu which can be used for "diffing". Open original file, paste changed file and use this option (without saving file).


Amazing what ST can achieve :D
"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: Syntax colour for ST API

Postby facelessuser on Sat Mar 10, 2012 11:19 pm

Something like this? API calls in green.

Screen Shot 2012-03-10 at 4.16.05 PM.png
Screen Shot 2012-03-10 at 4.16.05 PM.png (21.07 KiB) Viewed 758 times
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sat Mar 10, 2012 11:20 pm

facelessuser wrote:Something like this? API calls in green.

Screen Shot 2012-03-10 at 4.16.05 PM.png


So how did you achieve this - just for the API calls??
"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: Syntax colour for ST API

Postby facelessuser on Sat Mar 10, 2012 11:28 pm

agibsonsw wrote:
facelessuser wrote:Something like this? API calls in green.

Screen Shot 2012-03-10 at 4.16.05 PM.png


So how did you achieve this - just for the API calls??


I will update with the info later when I get some time. Hang tight. Gotta spend time with the kids :) .
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sat Mar 10, 2012 11:59 pm

@facelessuser. Okay. Go play with the kids and their 'gamestations' :D

When you get back.. I'm sooo.. close to what I want with this expression:

Code: Select all
<string>(?:\.)?(size|substr|begin_edit|end_edit|insert|erase|replace|sel|line|full_line|lines|split_by_newlines|word|find|find_all|
rowcol|text_point)\s*(?=\()</string>


It works if I follow the dot with a space '. begin_edit()' but not (currently) without the space?!
"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: Syntax colour for ST API

Postby agibsonsw on Sun Mar 11, 2012 12:11 am

No worries. I needed a more complex expression - following your lead.. That is, I looked through some of your additional regex in .tmLanguage.

Code: Select all
<string>(?:[a-zA-Z_][a-zA-Z0-9_]*\.)?(size|substr|begin_edit|end_edit|insert|erase|replace|sel|line|full_line|lines|split_by_newlines|
         word|find|find_all|rowcol|text_point)\s*(?=\()</string>


This is in place of the keyword 'global' which is very near the top of the tmLanguage file. I should then be able to modify the theme colour (that refers to the scope for 'global'), and if I want to format the word 'global' I can bung it in with something appropriate.

I might go for something yellow-ish or orange :)
"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: Syntax colour for ST API

Postby facelessuser on Sun Mar 11, 2012 12:18 am

Using the my Python modifications that I pointed you to earlier (it needs the other changes I made in the github version to work), I inserted a ST2 API check before checking for normal functions.

Screen Shot 2012-03-10 at 5.10.35 PM.png
Screen Shot 2012-03-10 at 5.10.35 PM.png (51.46 KiB) Viewed 772 times


And I defined the API section by copying the function section and placing it under the Repository part in the XML with the same name you see above. Don't replace the original function section, but make sure to add it before. You still want to make sure non API calls get scoped as functions.

Code: Select all
      <key>st2_api</key>
      <dict>
         <key>begin</key>
         <string>(?:\.)?(size|substr|begin_edit|end_edit|insert|erase|replace|sel|line|full_line|lines|split_by_newlines|word|find|find_all|rowcol|text_point)\s*(?=\()</string>
         <key>beginCaptures</key>
         <dict>
            <key>1</key>
            <dict>
               <key>name</key>
               <string>meta.function-call.st2-api.python</string>
            </dict>
         </dict>
         <key>end</key>
         <string>(\))</string>
         <key>endCaptures</key>
         <dict>
            <key>1</key>
            <dict>
               <key>name</key>
               <string>punctuation.definition.arguments.end.python</string>
            </dict>
         </dict>
         <key>name</key>
         <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>
               <array>
                  <dict>
                     <key>include</key>
                     <string>#dotted_name</string>
                  </dict>
               </array>
            </dict>
            <dict>
               <key>begin</key>
               <string>(\()</string>
               <key>beginCaptures</key>
               <dict>
                  <key>1</key>
                  <dict>
                     <key>name</key>
                     <string>punctuation.definition.arguments.begin.python</string>
                  </dict>
               </dict>
               <key>contentName</key>
               <string>meta.function-call.arguments.python</string>
               <key>end</key>
               <string>(?=\))</string>
               <key>patterns</key>
               <array>
                  <dict>
                     <key>include</key>
                     <string>#keyword_arguments</string>
                  </dict>
                  <dict>
                     <key>include</key>
                     <string>$self</string>
                  </dict>
               </array>
            </dict>
         </array>
      </dict>


I attached the modified python language. Diff it against the version I pointed to on github if you need to.
Attachments
Python.tmLanguage.zip
(6.07 KiB) Downloaded 50 times
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sun Mar 11, 2012 12:28 am

@facelessuser. Thank you, I shall study at length :)

I'm guessing that your (?:\.)? worked because it was registered within a beginCapture.

I thought my task was complete, but not of the theme files contain 'storage.modifier.global.python', or even the word 'global' at all. Am I able to just create a new dict entry, following an existing structure:

Code: Select all
<dict>
   <key>name</key>
   <string>Keyword</string>
   <key>scope</key>
   <string>keyword</string>
   <key>settings</key>
   <dict>
      <key>fontStyle</key>
      <string></string>
      <key>foreground</key>
      <string>#FF3854</string>
   </dict>
</dict>

and making up a name (Global?)? I'm optimistic that I can.

I'm not so concerned about other function calls at the moment - at least everything will not be the same colour ;)
"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: Syntax colour for ST API

Postby facelessuser on Sun Mar 11, 2012 12:41 am

There are a couple of advantages to what my version does over yours. There were a number of changes to make sure self and cls still get scoped proper in lines like this:

Code: Select all
self.object.api_call()


self and cls don't get colored in the theme, but they are getting scoped, and my requirement was not to break any other scoping. The one thing I did change was that if you had this:

Code: Select all
self.object.another_object.function()


object.another_object.function() would all get scoped as meta.function-call. I found this to be overzealous, so I reduced it to only scope function() as meta.function-call. After that, all I did was add generic to the function call so that all was pretty much the same meta.function-call.generic, but you add an extra specifier you could target it in your theme, but you don't have to and it would then be unhighlighted and benign.

By inserting the api check right before the normal function check, you get two kinds of function calls to target (keep in mind, built in functions are not touched by these, so they still get scoped like normal; I think I might have made some adjustments to ensure this).

meta.function-call.generic.python and meta.function-call.st2-api.python

I highlight like this if all I want to highlight is the api:
Code: Select all
      <dict>
         <key>name</key>
         <string>Function Call</string>
         <key>scope</key>
         <string>meta.function-call.st2-api</string>
         <key>settings</key>
         <dict>
            <key>fontStyle</key>
            <string></string>
            <key>foreground</key>
            <string>#A6E22E</string>
         </dict>
      </dict>
facelessuser
 
Posts: 1570
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sun Mar 11, 2012 1:45 am

Cool, so I can copy a dictionary entry, use my scope and make up a name - and colour.

I'm not concerned about any other functions, variables, etc., at the moment - I just want(ed) the code a little less monochrome. I'm still learning the API at the moment, so I was particularly interested in these methods.

The ST API documentation is quite limited, and I'm sure it's only mentioning a fraction of what's available. It doesn't mention on_query_completions, extract_completions, match_selector, or describe arguments in enough detail. So it's been mainly a question of trawling through, and deciphering, any code samples I can find.

I'm quite keen to pursue 'on_query_completions' and might build on this, together with a completions (or other source) file. Python and/or, more specifically, the API would be a sensible choice for this (as I can learn the language at the same time :) ). Besides, CodeIntel seems to have fallen by the wayside. [I might even look into that, seeing as it's ported from Komodo.]

I'll modify the theme file tomorrow.. and possibly come back if I can't get a nice orange effect :lol:. Andy.

Off-topic: What's a straight-forward way (with the API) to position the cursor? I know I could use run_command, but..
"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

PreviousNext

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 27 guests