Skip to content

Commit

Permalink
(python) added support for literal brackets in f-strings (#2195)
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-p-reichel authored and joshgoebel committed Oct 13, 2019
1 parent 22afd54 commit 9646eb0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions AUTHORS.en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,5 @@ Contributors:
- Taif Alimov <inzeppelin@gmail.com>
- Yuri Mazursky <mail@colomolome.com>
- Carl Baxter <carl@cbax.tech>
- Thomas Reichel <tom.p.reichel@gmail.com>

1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Improvements:
- fix(cpp): improve string literal matching
- fix(highlight.js): omit empty span-tags in the output (#2182)
- fix(Go): improve function declaration matching
- fix(python): added support for f-string literal curly braces (#2195)

[Carl Baxter]: https://github.com/cdbax

Expand Down
12 changes: 8 additions & 4 deletions src/languages/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ function(hljs) {
keywords: KEYWORDS,
illegal: /#/
};
var LITERAL_BRACKET = {
begin: /\{\{/,
relevance: 0
};
var STRING = {
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE],
Expand All @@ -38,11 +42,11 @@ function(hljs) {
},
{
begin: /(fr|rf|f)'''/, end: /'''/,
contains: [hljs.BACKSLASH_ESCAPE, PROMPT, SUBST]
contains: [hljs.BACKSLASH_ESCAPE, PROMPT, LITERAL_BRACKET, SUBST]
},
{
begin: /(fr|rf|f)"""/, end: /"""/,
contains: [hljs.BACKSLASH_ESCAPE, PROMPT, SUBST]
contains: [hljs.BACKSLASH_ESCAPE, PROMPT, LITERAL_BRACKET, SUBST]
},
{
begin: /(u|r|ur)'/, end: /'/,
Expand All @@ -60,11 +64,11 @@ function(hljs) {
},
{
begin: /(fr|rf|f)'/, end: /'/,
contains: [hljs.BACKSLASH_ESCAPE, SUBST]
contains: [hljs.BACKSLASH_ESCAPE, LITERAL_BRACKET, SUBST]
},
{
begin: /(fr|rf|f)"/, end: /"/,
contains: [hljs.BACKSLASH_ESCAPE, SUBST]
contains: [hljs.BACKSLASH_ESCAPE, LITERAL_BRACKET, SUBST]
},
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE
Expand Down
1 change: 1 addition & 0 deletions test/markup/python/f-strings.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
<span class="hljs-string">rf"<span class="hljs-subst">{name}</span>"</span>
<span class="hljs-string">fr"<span class="hljs-subst">{name}</span>"</span>
<span class="hljs-string">f"<span class="hljs-subst">{name + <span class="hljs-string">f'<span class="hljs-subst">{name}</span>'</span>}</span>"</span>
<span class="hljs-string">f"{{ }}"</span>
1 change: 1 addition & 0 deletions test/markup/python/f-strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ f"{name + 5}"
rf"{name}"
fr"{name}"
f"{name + f'{name}'}"
f"{{ }}"

0 comments on commit 9646eb0

Please sign in to comment.