Skip to content

Commit

Permalink
enh(python) Improve handling of decorators (#2811)
Browse files Browse the repository at this point in the history
Modifies the meta matching for Python code to handle decorators more accurately. This allows e.g. comments to be correctly formatted on lines starting with `@`.
  • Loading branch information
textbook committed Oct 31, 2020
1 parent 259b7c9 commit cf63d2f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Expand Up @@ -295,3 +295,4 @@ Contributors:
- Richard Gibson <gibson042@github>
- Fredrik Ekre <ekrefredrik@gmail.com>
- Jan Pilzer <Hirse@github>
- Jonathan Sharpe <mail@jonrshar.pe>
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -8,6 +8,7 @@ Language Improvements:
- enh(javascript) Match numeric literals per ECMA-262 spec [Richard Gibson][]
- enh(java) Match numeric literals per Java Language Specification [Richard Gibson][]
- enh(php) highlight variables (#2785) [Taufik Nurrohman][]
- fix(python) Handle comments on decorators (#2804) [Jonathan Sharpe][]

Dev Improvements:

Expand All @@ -23,6 +24,7 @@ New themes:
[Josh Goebel]: https://github.com/joshgoebel
[Taufik Nurrohman]: https://github.com/taufik-nurrohman
[Jan Pilzer]: https://github.com/Hirse
[Jonathan Sharpe]: https://github.com/textbook


## Version 10.3.1
Expand Down
3 changes: 2 additions & 1 deletion src/languages/python.js
Expand Up @@ -275,7 +275,8 @@ export default function(hljs) {
},
{
className: 'meta',
begin: /^[\t ]*@/, end: /$/
begin: /^[\t ]*@/, end: /(?=#)|$/,
contains: [NUMBER, PARAMS, STRING]
},
{
begin: /\b(print|exec)\(/ // don’t highlight keywords-turned-functions in Python 3
Expand Down
2 changes: 1 addition & 1 deletion test/detect/python/default.txt
@@ -1,4 +1,4 @@
@requires_authorization
@requires_authorization(roles=["ADMIN"])
def somefunc(param1='', param2=0):
r'''A docstring'''
if param1 > param2: # interesting
Expand Down
31 changes: 31 additions & 0 deletions test/markup/python/decorators.expect.txt
@@ -0,0 +1,31 @@
<span class="hljs-meta">@foo</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bar</span>():</span>
<span class="hljs-keyword">pass</span>

<span class="hljs-meta">@foo </span><span class="hljs-comment"># bar</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">baz</span>():</span>
<span class="hljs-keyword">pass</span>

<span class="hljs-meta">@foo.bar.baz</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">qux</span>():</span>
<span class="hljs-keyword">pass</span>

<span class="hljs-meta">@surround_with(<span class="hljs-params"><span class="hljs-string">&quot;#&quot;</span>, repeat=<span class="hljs-number">3</span></span>)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">text</span>():</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hi!&quot;</span>

<span class="hljs-meta">@py38.style</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span>():</span>
<span class="hljs-keyword">pass</span>

<span class="hljs-meta">@py[<span class="hljs-string">&quot;3.9&quot;</span>].style</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span>():</span>
<span class="hljs-keyword">pass</span>

<span class="hljs-meta">@py[<span class="hljs-number">3.9</span>].style</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span>():</span>
<span class="hljs-keyword">pass</span>

<span class="hljs-meta">@<span class="hljs-number">2</span> + <span class="hljs-number">2</span> == <span class="hljs-number">5</span></span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span>():</span>
<span class="hljs-keyword">pass</span>
31 changes: 31 additions & 0 deletions test/markup/python/decorators.txt
@@ -0,0 +1,31 @@
@foo
def bar():
pass

@foo # bar
def baz():
pass

@foo.bar.baz
def qux():
pass

@surround_with("#", repeat=3)
def text():
return "hi!"

@py38.style
def func():
pass

@py["3.9"].style
def func():
pass

@py[3.9].style
def func():
pass

@2 + 2 == 5
def func():
pass

0 comments on commit cf63d2f

Please sign in to comment.