Home Download Buy Blog Forum Support

add_regions swaps background and foreground?

add_regions swaps background and foreground?

Postby hibbelig on Tue Dec 27, 2011 1:33 pm

I am trying to programmatically add syntax highlighting a view, using view.add_regions. It seems that sometimes swaps foreground and background colors, though! Am I doing something wrong?

I have the following:
Code: Select all
view.add_regions('some_key', regions, 'comment')

The expected result is that the text looks like comments do in Python files. But comments in Python files are grey on the default background color, but the regions I added have grey background with default foreground.

Interestingly, I also have the following, rather similar code:
Code: Select all
view.add_regions('another_key', other_regions, 'string')

It works as expected: the regions look just like strings do in Python files.

Full code here: https://bitbucket.org/hibbelig/tablist
hibbelig
 
Posts: 77
Joined: Wed Dec 21, 2011 11:24 pm

Re: add_regions swaps background and foreground?

Postby hibbelig on Tue Dec 27, 2011 3:40 pm

I have now done some more testing. First of all, here is code that illustrates the issue, to be put in Packages/User/foo.py:
Code: Select all
import sublime, sublime_plugin

class FooCommand(sublime_plugin.WindowCommand):
   def run(self):
      v = self.window.new_file()
      edit = v.begin_edit()
      pt1 = 0
      # Example comment to see how it looks like
      pt2 = pt1 + v.insert(edit, pt1, "This is a comment\n")
      pt3 = pt2 + v.insert(edit, pt2, "This is a string\n")
      v.add_regions('foo_comment', [sublime.Region(pt1, pt2)], 'comment')
      v.add_regions('foo_string', [sublime.Region(pt2, pt3)], 'string')
      v.end_edit(edit)



Now look at the colors used in the line "Example comment to see how it looks like" and also look at the colors of "This is a string".

Open the console and type "window.run_command('foo', {})" into it.

The result will be a new view with two lines in it. I expect the first line to have the same colors as the example comment, and the second line to have the same colors as "This is a string" in the foo.py file.

But with the default theme, I get two lines with background and foreground colors swapped: In foo.py, the comment has a grayish foreground color, in the new view the comment has a grayish background color. And in foo.py, the strings have a yellowish foreground color, but in the new view the string has a grayish background color.

Using the Soda Light theme along with the Espresso Soda syntax highlighting, the comment line has swapped background/foreground colors, but the string line looks correct.

Using build 2154.
hibbelig
 
Posts: 77
Joined: Wed Dec 21, 2011 11:24 pm

Re: add_regions swaps background and foreground?

Postby hibbelig on Wed Dec 28, 2011 7:47 pm

I notice that Command+Option+P doesn't list the scope name from the add_regions call.
hibbelig
 
Posts: 77
Joined: Wed Dec 21, 2011 11:24 pm

Re: add_regions swaps background and foreground?

Postby davraamides on Tue Jan 15, 2013 2:15 pm

Did anyone ever find a solution to this? I'm having the same issue: the scope I pass to add_regions is being applied in reverse (the foreground color is being applied to the background).
davraamides
 
Posts: 2
Joined: Sat Mar 31, 2012 10:36 pm

Re: add_regions swaps background and foreground?

Postby FichteFoll on Sat Jan 19, 2013 4:42 pm

hibbelig wrote:I notice that Command+Option+P doesn't list the scope name from the add_regions call.

Yes, this is true; mainly because this is not how these "regions" work.

Apparently, regions added by `view.add_regions()` can not set the foreground color. By default they only color the background and with options they can also color the outline only oder underline the region. Setting the foreground color is not supported.

Because I think this feature is essential to create IDE-like features like dynamic code highlighting (e.g. variables that were passed from a function call) it is a missing feature that really could make sublime far better. But since Jon does not check userecho "frequently" and most users do not really appreciate changes to the API it is likely to be forgotten there. Let's hope Jon will make up/use a better issue-tracking system some time.
FichteFoll
 
Posts: 388
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to Plugin Development

Who is online

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