Home Download Buy Blog Forum Support

Python raw string breaks syntax highlighter

Python raw string breaks syntax highlighter

Postby jaraco on Thu Jan 26, 2012 7:01 pm

I have Sublime Text 2 build 2165. It seems the Python syntax highlighter doesn't support raw text strings with open square brackets. Consider this Python code:

Code: Select all
val = (r'[', ''),
print val


I enter this code into a blank Python file, and the syntax highlighter considers everything after "r'" to be part of the string, and the remainder of the file is colored purple.

I have one plugin installed (sublimelinter). I don't think the linter would break the syntax highlighting, but please let me know if I should test without it.
jaraco
 
Posts: 8
Joined: Thu Jan 26, 2012 6:53 pm

Re: Python raw string breaks syntax highlighter

Postby facelessuser on Thu Jan 26, 2012 7:07 pm

Code: Select all
val = (r'\[', '')


Problem solved ;)

I should be more specific. The language is coded to recognize the raw strings as regular expressions; that is their most common use. The bracket must be escaped for literal bracket in regex or it requires a closing bracket.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Python raw string breaks syntax highlighter

Postby jaraco on Thu Feb 02, 2012 11:46 pm

Curiously, we encountered this problem in our regular expressions because we're using [] to escape the [. For example,

Code: Select all
open_bracket_expr = re.compile(r'[[]')


Perhaps the regular expression language could be coded to account for that syntax.
jaraco
 
Posts: 8
Joined: Thu Jan 26, 2012 6:53 pm

Re: Python raw string breaks syntax highlighter

Postby facelessuser on Fri Feb 03, 2012 12:13 am

Code: Select all
open_bracket_expr = re.compile(r'[\[]')


Fixed. Actual brackets must have a closing bracket. Bracket characters must be escaped. I always do my regex this way. It also makes clear what is a bracket and what is a bracket char.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: Python raw string breaks syntax highlighter

Postby jaraco on Wed Feb 22, 2012 10:59 am

FYI, not fixed.

Just because it's possible to re-write my code to suit the syntax highlighter doesn't mean the syntax highlighter is functioning properly. I don't always have the luxury of rewriting code to suit the syntax highlighter of a particular editor... and what happens if another editor has a problem with your recommended syntax?

It would be much better if the syntax highlighter could parse the language and recognize the raw string and perhaps highlight it as a regular expression (though I can think of many cases where raw strings are used for purposes other than regular expressions), but not break the state of the highlighter as a result. That is, if the highlighter could always properly detect the end of the string and restore the highlight context from the beginning of the string, that would be best.
jaraco
 
Posts: 8
Joined: Thu Jan 26, 2012 6:53 pm

Re: Python raw string breaks syntax highlighter

Postby VertigoRay on Tue Jul 09, 2013 6:03 pm

jaraco wrote:FYI, not fixed.


I completely agree! I know this is old, but it's still an issue.

`[[]` is proper regex. `[\[]` makes sense, but is redundant. It all goes back to:

jaraco wrote:Just because it's possible to re-write my code to suit the syntax highlighter doesn't mean the syntax highlighter is functioning properly.

Here's a proper fix!

Since the raw string indicator is case-insensitive in python, maybe sublime should only do regex parsing on one case or the other?
Image
VertigoRay
 
Posts: 2
Joined: Tue Jul 09, 2013 5:55 pm
Location: Denton, TX


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot] and 28 guests