Bug 317097 - [PATCH] Python syntax for string.format
Summary: [PATCH] Python syntax for string.format
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: syntax (show other bugs)
Version: unspecified
Platform: Other All
: NOR wishlist
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-03-20 18:18 UTC by xOneca
Modified: 2013-05-15 18:27 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
Patch with respect syntax file v2.16 (1.70 KB, patch)
2013-03-25 15:19 UTC, xOneca
Details

Note You need to log in before you can comment on or make changes to this bug.
Description xOneca 2013-03-20 18:18:50 UTC
It would be great if the syntax highlighted, not only 'old' string format (format % substitutions), but the new style too ("string".format(substitutions)).

The new format has new syntax, and I made a regex to highlight as it was highlighted the olther style.

<RegExpr attribute="String Substitution" String="\{([a-zA-Z0-9_]+|[0-9]+)(\.[a-zA-Z0-9_]+|\[[^ \]]+\])*(![rs])?(:([^}]?[&lt;&gt;=^])?[ +-]?#?0?[0-9]*(\.[0-9]+)?[bcdeEfFgGnosxX%]?)?\}" context="#stay"/>

(It should be placed under the other "String Substitution" is)

Reproducible: Always

Steps to Reproduce:
Write a piece of code:

  print "My var value: {var}".format(var=10)
  print "My var value: {var[name]}".format(var={"name":"Bob"})
Actual Results:  
Strings are fully colored as strings.

Expected Results:  
Variable substitution syntax should be visible in another color/style.

The full syntax is as follows:

( http://docs.python.org/2/library/string.html#format-string-syntax )

replacement_field ::= "{" field_name ["!" conversion] [":" format_spec] "}"
field_name ::= (identifier | integer) ("." attribute_name | "[" element_index "]")*
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s"
format_spec ::= [[fill]align][sign][#][0][width][.precision][type]
fill ::= <a character other than '}'>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Comment 1 xOneca 2013-03-25 15:19:35 UTC
Created attachment 78377 [details]
Patch with respect syntax file v2.16

I have made a patch to the syntax file v2.16.
Comment 2 Gerald Senarclens de Grancy 2013-05-09 19:35:16 UTC
Thanks for the patch. Just had a look and it seems to work. However, in your example:
print "My var value: {var}".format(var=10)
`format` is colored like the matched {var}. This makes it hard to tell where the string ends. Also, other string methods aren't highlighted either, so imho it probably be better (for consistency) not to match the `format` function as a string substitution.
Comment 3 xOneca 2013-05-14 07:18:19 UTC
(In reply to comment #2)
> Thanks for the patch. Just had a look and it seems to work. However, in your
> example:
> print "My var value: {var}".format(var=10)
> `format` is colored like the matched {var}. This makes it hard to tell where
> the string ends. Also, other string methods aren't highlighted either, so
> imho it probably be better (for consistency) not to match the `format`
> function as a string substitution.

That's because format is also a built-in function. (see http://docs.python.org/2/library/functions.html#format -- all functions in that page are colored the same)

In fact, they have different color styles: string substitution is 'dsOthers' and built-in functions are 'dsDataType'.
Comment 4 Gerald Senarclens de Grancy 2013-05-14 11:00:34 UTC
(In reply to comment #3)
> That's because format is also a built-in function. (see
> http://docs.python.org/2/library/functions.html#format -- all functions in
> that page are colored the same)

Thanks; good point. I had tried two built-in functions (like Python 3's print) and they weren't in that list yet (hence not in the same color). Therefore I was confused. Also the color scheme isn't particularly fortunate having the same color for BIFs. Will sort things out by the end of the week.
Comment 5 Gerald Senarclens de Grancy 2013-05-15 18:27:17 UTC
Git commit e60dbd2c68c822e6817f7120ba1c8eca87f05bd9 by Gerald Senarclens de Grancy.
Committed on 15/05/2013 at 20:22.
Pushed by geralds into branch 'master'.

Python syntax for string.format

patch by xOneca

M  +17   -1    part/syntax/data/python.xml

http://commits.kde.org/kate/e60dbd2c68c822e6817f7120ba1c8eca87f05bd9