Packages: python: last % element in string incorrectly colored

Created on 21 Jun 2016  Â·  12Comments  Â·  Source: sublimehq/Packages

This has bothered me for a while now.

1

Most helpful comment

For now I've fixed the immediate issue with 5e43afc543fb392735350aca398437a6d23f7d8a, and another issue with commas in {} format placeholders.

Feel free to open another issue about improving the placeholder definition further.

All 12 comments

It's because the % before the M is captured with the %H I think:

datetime.datetime.utcnow().strftime('%Y%m%d%H%M') # "bug"
datetime.datetime.utcnow().strftime('%Y%m%d%H %M') # works
'%H %M' # works
'%H%M' # "bug"

I don't know enough about what _should_ match and what _shouldn't_ match to suggest a fix for it, it's quite a complicated/long regex: https://github.com/sublimehq/Packages/blob/dba0eaed9285d36590013ea5c5135fd2b90f9642/Python/Python.sublime-syntax#L634

This should be added as a test case ofc, currently the test file only contains %s :)

Interesting. I was tinkering with the string interpolation rules not too long ago. With my test code, only the %d is matched since I broke out the .format() and string mini-format syntaxes without considering date strings. I tailored the "modes" available to match the doc.

Here's what I had WIP:

  constant_placeholder:
    - match: '%(?i:\([a-z_]+\))?#?0?\-? ?\+?([0-9]*|\*)(\.([0-9]*|\*))?[hlL]?[acdeEfFgGiorsuxX%]'
      scope: constant.other.placeholder.python
    - match: '\{([\w.\[\]]*)(.?:[<>=^]?)?[ +-]?\d*(\.\d+)?[bcdeEfFgGnosxX%]?\}'
      scope: constant.other.placeholder.python

I could add date formatting strings as an additional set.


Side note: I originally set out to deal with slightly more complex .format() placeholders, but it turns out that they can be nested in ways that aren't exactly simple to parse:

'{0[{1}]}'.format(foo, bar)
'{0:{1}}'.format(foo, bar)
'{0.foo[{0d}]:={1[{2:0>3}]}}'.format(bar, baz, bif)

maybe the string passed into strftime should be handled separately by the syntax definition? https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
from what I can see, str.format is detailed in PEP3101 and the default % style formatting is covered here: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

Imo datestring format shouldn't be covered by the syntax definition at all. Only the normal string formats.

Unless we add a special context that matches datestring format strings within a strftime function call only.

Edit: @michaelblyons The format string mini-language is complex enough to require its own context (with nested contexts even).

For now I've fixed the immediate issue with 5e43afc543fb392735350aca398437a6d23f7d8a, and another issue with commas in {} format placeholders.

Feel free to open another issue about improving the placeholder definition further.

​I have a question. Can Python.sublime-syntax be updated standalone?
I downloaded the file, unzipped Python.sublime-package, updated, and
re-zipped it. The bug is fixed, but I've noticed a new unrelated bug. I'm
not sure if it's actually a bug or because the files are not synced in the
package.

The best course of action is to follow the directions in the readme.

​Hmm it doesn't work. It's not getting picked up. Maybe because I'm on

  1. I'll wait till the next release.​

It should not matter if you are on 3114. What platform are you on? Can you provide the output of performing a directory listing on the Packages/ folder?

$ ls -l /opt/sublime_text/Packages/
ActionScript.sublime-package
AppleScript.sublime-package
ASP.sublime-package
Batch File.sublime-package
Clojure.sublime-package
Color Scheme - Default.sublime-package
CSS.sublime-package
C#.sublime-package
C++.sublime-package
Default.sublime-package
Diff.sublime-package
D.sublime-package
Erlang.sublime-package
Go.sublime-package
Graphviz.sublime-package
Groovy.sublime-package
Haskell.sublime-package
HTML.sublime-package
JavaScript.sublime-package
Java.sublime-package
Language - English.sublime-package
LaTeX.sublime-package
Lisp.sublime-package
Lua.sublime-package
Makefile.sublime-package
Markdown.sublime-package
Matlab.sublime-package
Objective-C.sublime-package
OCaml.sublime-package
Pascal.sublime-package
Perl.sublime-package
PHP.sublime-package
Python -> /home/user/Packages/Python
Python.sublime-package
Rails.sublime-package
Regular Expressions.sublime-package
RestructuredText.sublime-package
R.sublime-package
Ruby.sublime-package
Rust.sublime-package
Scala.sublime-package
ShellScript.sublime-package
SQL.sublime-package
TCL.sublime-package
Textile.sublime-package
Text.sublime-package
Theme - Default.sublime-package
Vintage.sublime-package
XML.sublime-package
YAML.sublime-package
$ ls -1 /home/user/Packages/Python
Completion Rules.tmPreferences
Miscellaneous.tmPreferences
Python.sublime-build
Python.sublime-syntax
Regular Expressions (Python).sublime-syntax
Snippets
Symbol Index.tmPreferences
Symbol List.tmPreferences
syntax_test_python.py
ln -s /home/user/Packages/Python ~/.config/sublime-text-3/Packages

​Works! (The README doesn't say.) And the new bug is still there. I'll file
a report shortly.​

Was this page helpful?
0 / 5 - 0 ratings