Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(python) added support for literal brackets in f-strings #2195

Merged
merged 3 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

[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"{{ }}"