Skip to content

Commit 2577b6e

Browse files
RunDevelopmentmAAdhaTTah
authored andcommitted
Add decorator support to Python (#1639)
Add support for Python decorators, implementing #1635 using the solution proposed by @spectras. Also add 2 aliases: `annotation` to be like Java and `punctuation` to change the actual color (like Java). This resolves #1635.
1 parent d33d259 commit 2577b6e

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

components/prism-python.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@ Prism.languages.python = {
2020
pattern: /(\bclass\s+)\w+/i,
2121
lookbehind: true
2222
},
23+
'decorator': {
24+
pattern: /(^\s*)@\w+(?:\.\w+)*/i,
25+
lookbehind: true,
26+
alias: ['annotation', 'punctuation'],
27+
inside: {
28+
'punctuation': /\./
29+
}
30+
},
2331
'keyword': /\b(?:and|as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|print|raise|return|try|while|with|yield)\b/,
24-
'builtin':/\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,
32+
'builtin': /\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,
2533
'boolean': /\b(?:True|False|None)\b/,
2634
'number': /(?:\b(?=\d)|\B(?=\.))(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,
2735
'operator': /[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,

components/prism-python.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@some.decorator(foobar)
2+
def foo(bar):
3+
pass
4+
5+
----------------------------------------------------
6+
7+
[
8+
["decorator", [
9+
"@some",
10+
["punctuation", "."],
11+
"decorator"]
12+
],
13+
["punctuation", "("],
14+
"foobar",
15+
["punctuation", ")"],
16+
17+
["keyword", "def"],
18+
["function", "foo"],
19+
["punctuation", "("],
20+
"bar",
21+
["punctuation", ")"],
22+
["punctuation", ":"],
23+
["keyword", "pass"]
24+
]
25+
26+
----------------------------------------------------
27+
28+
Checks for decorators.

0 commit comments

Comments
 (0)