Sublime Forum

add_regions swaps background and foreground?

#1

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:

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:

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: bitbucket.org/hibbelig/tablist

0 Likes

Syntax highlighting programmatically
Syntax highlighting with Pygments
#2

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:

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.

0 Likes

#3

I notice that Command+Option+P doesn’t list the scope name from the add_regions call.

0 Likes

#4

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).

0 Likes

#5

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.

4 Likes