Home Download Buy Blog Forum Support

Syntax colour for ST API

Re: Syntax colour for ST API

Postby facelessuser on Sun Mar 11, 2012 3:19 am

Glad I could help. I just happened to have done this stuff about a week or two ago. Had I not, I would have been pretty clueless.

agibsonsw wrote:Off-topic: What's a straight-forward way (with the API) to position the cursor? I know I could use run_command, but..


What do you mean position the cursor...move the cursor to postion x on line y?

This is assuming you know how to calculate your new position and create a region.
You usually need to account for multiselect so you would clear the current selections and then add each new selection region:

Code: Select all
view.sel().clear()
map(lambda x: view.sel().add(x), new_sel_array)


or for one:

Code: Select all
view.sel().clear()
view.sel().add(new_sel)


Basically you can parse the view.sel keeping the regions you want or just delete them all without checking and add the new one/or ones you want.
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sun Mar 11, 2012 5:26 pm

Thank you @facelessuser

So something like:

sel = view.sel()[0]
view.sel().clear()
view.sel().add(sel)

would clear all selections and re-instate the cursor to the latest position ;)
"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 7:18 pm

Well this works, thank you @facelessuser. Although I'm not entirely happy with my choice of colour (a sort of lilac..) but at list I achieved my aim :)

I'm not sure I want to pursue this any further just at the moment. I can imagine spending hours (days..) on this :o. I would learn a little more about regex, ST themes, and have control over colours I suppose.

Regards, Andy.

Edited: I also modified the scope from '...global.python' to 'storage.type.function.api.python' so that it should behave better, and chucked the word 'global' in with def|lambda.
Attachments
sublime_selected2.png
sublime_selected2.png (13.52 KiB) Viewed 803 times
"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 8:58 pm

@facelessuser. Sorry to bore :) but I've got control now without too much effort, whe'hay! I copied my new category/dictionary entry 'storage.type.function.api.python' to create 'storage.type.function.general.python', and added a few standard, and commonly used library, functions to it - see the yellow text in the screenshot. These two categories are near the front of the tmLanguage file, so conflict seems unlikely.

I was under the impression that I would need to study the whole language file to be able to slip these in :lol:. So now I can add functions as I go along, and create more categories if I so chose, whe'hay! Job done.

I might try and tackle objects like window, view.. This might prove a little trickier though, as I haven't got an opening brace '(' to latch on to :?:

Regards, Andy.
Attachments
sublime_selected3.png
sublime_selected3.png (18.06 KiB) Viewed 792 times
"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 9:49 pm

Window and view should be targetable. In the method I am using, I catch all of the object members (even the ones without (); I just don't scope them with anything; like I said I changed it a lot of things to help me out in the future).

But yeah, there are always more ways than one to do things.
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sun Mar 11, 2012 9:59 pm

I see now what you were indicating with self, cls :( . In a line such as

Code: Select all
self.window.focus_view(OrderedFilesCommand.file_views[index][1])


focus_view is my chosen colour, but self and index are no longer the colour they should be. My expression

Code: Select all
<string>(?:[a-zA-Z_][a-zA-Z0-9_]*\.)*(append|lower|sort|split|itemgetter)\s*(?=\()</string>

is not sufficient to discount everything else on the line.

Can you elaborate on "I just don't scope them with anything" please? This should help me..

[I can see that I'm going to end up with a kaleidoscope like yours :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 Sun Mar 11, 2012 11:25 pm

agibsonsw wrote:I see now what you were indicating with self, cls :( . In a line such as

Code: Select all
self.window.focus_view(OrderedFilesCommand.file_views[index][1])


focus_view is my chosen colour, but self and index are no longer the colour they should be. My expression

Code: Select all
<string>(?:[a-zA-Z_][a-zA-Z0-9_]*\.)*(append|lower|sort|split|itemgetter)\s*(?=\()</string>

is not sufficient to discount everything else on the line.

Can you elaborate on "I just don't scope them with anything" please? This should help me..


This is what I was trying to say earlier. I already accounted for all of the stuff, so I was suggesting that you leverage the work I already did.
Basically in my version I don't do this (?:[a-zA-Z_][a-zA-Z0-9_]*\.)*. This will greedily include any parent object that happens to come before your target. That is the way it was doing things before I touched it. If an object had a function in it, all preceding object members got pulled into the scope which I though overzealous. So what I did is change the default function check to not greedily include those members, and then I catch them with this and give them no additional scope (I should probably check for lists here as well):
Code: Select all
      <key>generic_object_names</key>
      <dict>
         <key>match</key>
         <string>(\.\b([A-Za-z_][A-Za-z0-9_]*)\b(?!\()|\b([A-Za-z_][A-Za-z0-9_]*)\b\.)</string>
      </dict>


So you can see here I look for built in things first, and if I don't find those, I throw out all the dot members that aren't function calls etc, then I check for API and then functions, and then lists etc.:
Code: Select all
      <dict>
         <key>include</key>
         <string>#builtin_functions</string>
      </dict>
      <dict>
         <key>include</key>
         <string>#builtin_types</string>
      </dict>
      <dict>
         <key>include</key>
         <string>#magic_function_names</string>
      </dict>
      <dict>
         <key>include</key>
         <string>#language_variables</string>
      </dict>
      <dict>
         <key>include</key>
         <string>#generic_object_names</string>
      </dict>
      <dict>
         <key>include</key>
         <string>#st2_api</string>
      </dict>
      <dict>
         <key>begin</key>
         <string>(?:\.)?([a-zA-Z_][a-zA-Z_0-9]*)\s*(?=\()</string>
         <key>beginCaptures</key>


I think I also made some minor adjustments to ensure that scope and built in globals get highlighted correctly too, but I would have to diff my changes to remember what I did.

agibsonsw wrote:[I can see that I'm going to end up with a kaleidoscope like yours :D ]

Not so silly now eh? :)
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sun Mar 11, 2012 11:57 pm

@facelessuser. What seems to be relatively straight-forward is getting complicated ;). Part of my confusion was/is that even though I use a non-capturing group (?: ), it seems that the more general term 'function' later on collects this text.

It also seems odd that only the word 'self' gets caught out, although I think 'key' has also lost its colour, and (possibly) index.

So changing the key 'captures' to 'match' is not likely to help me? And you wouldn't recommend that I ditch the name-part 'function' all-together? Edited: Ignore this remark about 'function' - I realise this wouldn't help (it IS a function, regardless).

I apologise ;) . I should be studying all the content you have supplied me with in great depth, but I'm still taking pigeon steps at the mo. I could just "borrow" your language file, but I'm a bit obstinate :twisted:
"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 Mon Mar 12, 2012 1:47 am

agibsonsw wrote:I apologise ;) . I should be studying all the content you have supplied me with in great depth, but I'm still taking pigeon steps at the mo. I could just "borrow" your language file, but I'm a bit obstinate :twisted:


It is what makes us programmers. Early on with javascript, I wrote my own simple framework (not because I thought I could do better than Jquery; I think Jquery is great and way better) but I just wanted to learn as much as I could about javascript...it was both fun and painful, but I ended up with a pretty usable framework. In the end, I think I learned more about web browsers and javascript than I ever cared to learn.
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Mon Mar 12, 2012 2:27 am

@facelessuser. Ditto. I wrote my own JavaScript library :lol:. Admittedly, some methods such as $() and getElementsByClassName() I pretty much copied letter for letter, but most methods I adapted (improved upon..), including creating my own sort procedure and date parser. I wanted to look at jQuery, but I didn't fancy reading another book. So I went on to the jQuery forum and answered tons of questions (before I'd even typed a line of jQuery code :o ).

Anyway, I think I've short-circuited my labours. I realised that the function greediness is how the language file behaves. I wasn't looking to correct this behaviour, merely to add a number of specific methods/terms to the language. And it's mainly the term 'self' that was an issue (the word 'key' isn't even in the file!).

For the moment I've added the terms self and cls in with global, very near the top of the file. The screenshot shows that self, and my api calls, are colour-coded :). If the function scope gobbles up other terms then that's SEP! [Actually, placing 'self' near the top of the file may prevent 'function' from gobbling up text anyway..]

All I need to do now (hopefully) is decide what words/methods/additional categories I want and flatten out the colours a bit.

Regards, and thanks again, Andy.
Attachments
sublime_selected4.png
sublime_selected4.png (25.74 KiB) Viewed 754 times
"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 25 guests