Home Download Buy Blog Forum Support

Syntax colour for ST API

Syntax colour for ST API

Postby agibsonsw on Sat Mar 10, 2012 8:45 pm

Hello.

I'm trying to tweak the Python.tmLanguage file so that I can at least show the API methods in a different colour. I tried modifying

Code: Select all
<key>captures</key>
<dict>
   <key>1</key>
   <dict>
      <key>name</key>
      <string>storage.modifier.global.python</string>
   </dict>
</dict>
<key>match</key>
<string>\b(global|window|etc)\b</string>    <<-- add API terms here.

It doesn't colour-code following a dot - any idea how I can modify the expression to accept a preceding dot?

But, more importantly, it must pick up words before mine. That is, a word like 'get' will be found before 'get_clipboard', so my colour never appears.

Perhaps if I create a new scope/category entirely, then it will pick up my words. Can someone offer a few pointers in the right direction for me? I realise that I would also need to modify my chosen theme file as well.

I can't really create a new syntax file, because it's still a standard '.py' file.

An alternative approach I would like advice on: I could use 'on_modified', check it's a Python file, check it contains 'import sublime', find the API methods, use add-regions to re-insert these words with a specified scope. This would give me more control over the scope, and I wouldn't have to modify the language and theme files.

I welcome suggestions and advice :)
"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 9:17 pm

It won't accept a dot because the words are between \b. \b is a word boundary (a zero width representation of where a word [a-zA-Z0-9_]+ begins and ends. This is also why the CSS syntax language will not highlight -moz-border-radius and such. The leading dash cannot be picked up because the word boundary would fall after it. I ended up fixing it for my own personal use.

I am not sure what you are wanting exactly, so I can't really give advise. Ultimately what exactly are you hoping to highlight. I have done things like this before. I actually modified my python to scope all function calls only after the dot like so.

All function calls that are not built in functions are pink

Screen Shot 2012-03-10 at 2.11.39 PM.png
Screen Shot 2012-03-10 at 2.11.39 PM.png (76.12 KiB) Viewed 1240 times


If you give me a idea of what exactly you are trying to do, maybe I can help some. Some times you have to inject rules before other rules to catch things. I think I made a number of changes until I finally got function calls to highlight proper (without highlighting all parent dot object members and keep built in function names to not highlight when they were dot call functions and not get overridden with my new scope. It was a little tricky.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

Postby agibsonsw on Sat Mar 10, 2012 9:28 pm

Hello again @facelessuser

Your screenshot is what I'm looking for (although a little bright for my taste :D ). All my variables, function calls, attributes are plain white/ a single colour, in most themes.

I don't mind if ALL functions calls are at least a different colour, but I was hoping to specifically colour the API methods: window(), sel(), etc..

Are you able to recount the steps to your kaleidoscope? 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: Syntax colour for ST API

Postby facelessuser on Sat Mar 10, 2012 10:14 pm

Even better you can just diff mine with the original and see all of the changes. It is right here on github (I store them here so all of my computers can pull them down):

https://github.com/facelessuser/sublime ... tmLanguage

You can ignore my changes to highlighting python dict objects if there are any (can't remember if it was a color theme change only, or a syntax language change too). The rest of the changes should all be related to function call highlighting. I think to do the same thing in the Javascript it was a three line change, Python I had to tinker a bit.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

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

I'm making some sort of progress with the following code. It manages to find the words and change their formatting, but with a weird background?! I tried a scope of "comment" but a similar effect occurs(?).

Code: Select all
import sublime, sublime_plugin

class ExampleCommand(sublime_plugin.EventListener):
   # def on_query_completions(self, view, prefix, locations):
   #    if not view.match_selector(locations[0],'text.html - source'):
   #       return []

   def on_modified(self, view):
      sel = view.sel()[0]
      pt = sel.begin()
      if not view.match_selector(pt, 'source.python'):
         print 'not Python'
         return
      if view.find("import\s+sublime",0) is None:
         print 'not using Sublime'
         return
      edit = view.begin_edit()
      print 'here'
      api_regions = view.find_all("view|sel|begin")
      print api_regions
      view.add_regions('my_key', api_regions, "storage.type.class.python")
      view.end_edit(edit)
Attachments
sublime_selected1.png
sublime_selected1.png (15.21 KiB) Viewed 1256 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 Sat Mar 10, 2012 10:17 pm

@facelessuser Ta. I'll have a look.

Does ST have a Diff feature? Although, I have WinMerge which is a nice app ;)
"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 10:23 pm

agibsonsw wrote:@facelessuser Ta. I'll have a look.

Does ST have a Diff feature? Although, I have WinMerge which is a nice app ;)


Not really. There are some diff plugins, but they don't really let you copy things from one to another. More of a simple diff view.

When I am on Windows, I usually use WinMerge as well. I use this other free diff thing when I am on Mac, but I am not very happy with it; I need to find a better one.

Oh keep in mind, my changes to highlight function calls was adding a new scope, but you could use an existing one and then you would not need a color theme change.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

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

agibsonsw wrote:I'm making some sort of progress with the following code. It manages to find the words and change their formatting, but with a weird background?! I tried a scope of "comment" but a similar effect occurs(?).


A plugin to highlight the API stuff isn't going to be nearly as nice as it is being built into the tmLanguage file. Highlighted regions are not quite like scoping with a tmLanguage, they are literally a colored region; you can't change the text color, font-style, or anything else. All you can do is create colored blocks, colored outlines, and colored underlines.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Syntax colour for ST API

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

facelessuser wrote:A plugin to highlight the API stuff isn't going to be nearly as nice as it is being built into the tmLanguage file. Highlighted regions are not quite like scoping with a tmLanguage, they are literally a colored region; you can't change the text color, font-style, or anything else. All you can do is create colored blocks, colored outlines, and colored underlines.


Ahh, that's a shame, thought I was almost there! But an interesting exercise non-the less.

Testing your regex skills, how can I make this:

api_regions = view.find_all("view|sel|begin")

only capture if preceded by a dot or other (non-word) character and followed by another non-word character, but NOT capture these additional bits?
"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 rchl on Sat Mar 10, 2012 10:41 pm

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).
rchl
 
Posts: 148
Joined: Fri Mar 04, 2011 11:02 pm

Next

Return to Technical Support

Who is online

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