Sublime Forum

Python raw string breaks syntax highlighter

#1

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:

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.

0 Likes

#2
val = (r'\', '')

Problem solved :wink:

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.

0 Likes

#3

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

open_bracket_expr = re.compile(r']')

Perhaps the regular expression language could be coded to account for that syntax.

0 Likes

#4
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.

0 Likes

#5

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.

0 Likes

#6

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:

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?

0 Likes

#7

This is still a problem.

0 Likes

#8

What about sublime text3

0 Likes

#9
  1. The example string could as well just be non-raw, but I see and know the problem.

  2. Using upper-case R for R'' does not apply regex syntax highlighting to the string, which isn’t terminated properly due to being a hassle with the old tmLanguage format.

  3. The problem is easily fixed with prototyped context pushes in the new sublime-syntax format, but nobody made that yet.

0 Likes