From f7ff76453d78235b0476ed4a7fe992cb2047760c Mon Sep 17 00:00:00 2001 From: Philipp Gloor Date: Mon, 16 Jun 2025 15:21:31 +0200 Subject: [PATCH 1/5] Add better support for Python functions and props --- src/languages/python.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/languages/python.js b/src/languages/python.js index 2604889790..3e3b53785c 100644 --- a/src/languages/python.js +++ b/src/languages/python.js @@ -383,6 +383,39 @@ export default function(hljs) { relevance: 0 }, { match: /\bor\b/, scope: "keyword" }, + // Method calls with parentheses + { + match: [ + /\./, + IDENT_RE, + /(?=\s*\()/ + ], + scope: { + 2: "title.function.method" + } + }, + // Chained method calls + { + match: [ + /\./, + IDENT_RE, + /(?=\s*\.\s*\w)/ + ], + scope: { + 2: "title.function.method" + } + }, + { + match: [ + /(? Date: Tue, 17 Jun 2025 23:04:41 +0200 Subject: [PATCH 2/5] Adjust the tests to account for the changes --- test/markup/python-repl/sample.expect.txt | 4 ++-- test/markup/python/class_self.expect.txt | 2 +- test/markup/python/function-header-comments.expect.txt | 2 +- test/markup/python/keywords.expect.txt | 2 +- test/markup/python/matrix-multiplication.expect.txt | 2 +- test/markup/python/numbers.expect.txt | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/markup/python-repl/sample.expect.txt b/test/markup/python-repl/sample.expect.txt index 66bf554a24..b4ff939350 100644 --- a/test/markup/python-repl/sample.expect.txt +++ b/test/markup/python-repl/sample.expect.txt @@ -3,9 +3,9 @@ "foo = 42" >>> print(v) foo = 42 ->>> print(repr(v).rstrip('"')) +>>> print(repr(v).rstrip('"')) "foo = 42 ->>> print(repr(v).lstrip('"')) +>>> print(repr(v).lstrip('"')) foo = 42" >>> """ diff --git a/test/markup/python/class_self.expect.txt b/test/markup/python/class_self.expect.txt index db39ede4a9..08ceac87f0 100644 --- a/test/markup/python/class_self.expect.txt +++ b/test/markup/python/class_self.expect.txt @@ -1,6 +1,6 @@ class SelfTest: def __init__(self): - self.text = True + self.text = True def method(self): pass diff --git a/test/markup/python/function-header-comments.expect.txt b/test/markup/python/function-header-comments.expect.txt index 35041289d8..9215af8fa2 100644 --- a/test/markup/python/function-header-comments.expect.txt +++ b/test/markup/python/function-header-comments.expect.txt @@ -4,7 +4,7 @@ pass -class Foo(collections.namedtuple('Test'), ( +class Foo(collections.namedtuple('Test'), ( 'name', # comment )): pass diff --git a/test/markup/python/keywords.expect.txt b/test/markup/python/keywords.expect.txt index f2227af2f5..01c309ba03 100644 --- a/test/markup/python/keywords.expect.txt +++ b/test/markup/python/keywords.expect.txt @@ -8,7 +8,7 @@ x = Shorty() if __debug__: sys = __import__('sys') -for _ in sys.path: +for _ in sys.path: print(_) exec(123) diff --git a/test/markup/python/matrix-multiplication.expect.txt b/test/markup/python/matrix-multiplication.expect.txt index 91ad4b9261..6a5f334e4c 100644 --- a/test/markup/python/matrix-multiplication.expect.txt +++ b/test/markup/python/matrix-multiplication.expect.txt @@ -3,5 +3,5 @@ @decorator def f(self, H, V, beta, r): - S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r) + S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r) return S diff --git a/test/markup/python/numbers.expect.txt b/test/markup/python/numbers.expect.txt index 725eb354f7..65530608ea 100644 --- a/test/markup/python/numbers.expect.txt +++ b/test/markup/python/numbers.expect.txt @@ -29,7 +29,7 @@ # expressions containing numeric literals -0..__str__, 1e1.__str__, fn(.5) +0..__str__, 1e1.__str__, fn(.5) 0is 0, 0lis 0 0_0_0is 0, 0_0_0lis 0 0b0is 0, 0b0lis 0 @@ -50,7 +50,7 @@ 0_0_0jis 0, 0_0_9jis 0 # expressions not containing numeric literals -x0.j +x0.j # invalid pseudo-numeric expressions 1__0 From 01488c20d301c2085feb96a2ede810db28103efe Mon Sep 17 00:00:00 2001 From: Philipp Gloor Date: Tue, 17 Jun 2025 23:09:25 +0200 Subject: [PATCH 3/5] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 4b7c3021fd..f84b617f3f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -25,6 +25,7 @@ Core Grammars: - enh(json) add json5 support [Kerry Shetline][] - fix(css) `unicode-range` parsing, issue #4253 [Kerry Shetline][] - fix(csharp) Support digit separators [te-ing][] +- enh(python) add support for methods and properties [fibbo][] Documentation: @@ -53,6 +54,7 @@ CONTRIBUTORS [Thomas Gorissen]: https://github.com/serrynaimo [te-ing]: https://github.com/te-ing [Anthony Martin]: https://github.com/anthony-c-martin +[fibbo]: https://github.com/fibbo ## Version 11.11.1 From 8d779a7c3d79d2adc99fcfd9e6017024f6710d47 Mon Sep 17 00:00:00 2001 From: Philipp Gloor Date: Wed, 18 Jun 2025 08:11:01 +0200 Subject: [PATCH 4/5] Differentiate between built-in and functions --- test/markup/python/keywords.expect.txt | 1 + test/markup/python/keywords.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/test/markup/python/keywords.expect.txt b/test/markup/python/keywords.expect.txt index 01c309ba03..d1d998de37 100644 --- a/test/markup/python/keywords.expect.txt +++ b/test/markup/python/keywords.expect.txt @@ -3,6 +3,7 @@ return NotImplemented x = Shorty() +x.len() len(x) if __debug__: diff --git a/test/markup/python/keywords.txt b/test/markup/python/keywords.txt index 30e3d0a180..9f77309747 100644 --- a/test/markup/python/keywords.txt +++ b/test/markup/python/keywords.txt @@ -3,6 +3,7 @@ class Shorty(dict): return NotImplemented x = Shorty() +x.len() len(x) if __debug__: From ff1cfeebc67c070f5cf7be8fff0d2a5c33611690 Mon Sep 17 00:00:00 2001 From: Philipp Gloor Date: Sun, 22 Jun 2025 00:54:58 +0200 Subject: [PATCH 5/5] Verify that submodules in imports are not highlighted --- test/markup/python/keywords.expect.txt | 3 +++ test/markup/python/keywords.txt | 3 +++ 2 files changed, 6 insertions(+) diff --git a/test/markup/python/keywords.expect.txt b/test/markup/python/keywords.expect.txt index d1d998de37..b5bb7fb80f 100644 --- a/test/markup/python/keywords.expect.txt +++ b/test/markup/python/keywords.expect.txt @@ -1,3 +1,6 @@ +from base.derive import test +import base.test + class Shorty(dict): def len(self): return NotImplemented diff --git a/test/markup/python/keywords.txt b/test/markup/python/keywords.txt index 9f77309747..8b32aec233 100644 --- a/test/markup/python/keywords.txt +++ b/test/markup/python/keywords.txt @@ -1,3 +1,6 @@ +from base.derive import test +import base.test + class Shorty(dict): def len(self): return NotImplemented