From 45694a25948146e860738cb4e01de7e7e9aa91b0 Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 22 May 2022 18:53:00 +0200 Subject: [PATCH] Don't font-lock invalid invalid class/function names * lisp/progmodes/python.el (python-font-lock-keywords-level-1): Don't font-lock invalid invalid class/function names (bug#55573). --- lisp/progmodes/python.el | 4 ++-- test/lisp/progmodes/python-tests.el | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index c1368364a97..9adbb82abf4 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -540,9 +540,9 @@ the {...} holes that appear within f-strings." (setq ppss (syntax-ppss)))))) (defvar python-font-lock-keywords-level-1 - `((,(rx symbol-start "def" (1+ space) (group (1+ (or word ?_)))) + `((,(python-rx symbol-start "def" (1+ space) (group symbol-name)) (1 font-lock-function-name-face)) - (,(rx symbol-start "class" (1+ space) (group (1+ (or word ?_)))) + (,(python-rx symbol-start "class" (1+ space) (group symbol-name)) (1 font-lock-type-face))) "Font lock keywords to use in `python-mode' for level 1 decoration. diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index a3f778bbbe9..479d68a0623 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -214,6 +214,18 @@ aliqua." (should (string= (buffer-string) "\"\"")) (should (null (nth 3 (syntax-ppss)))))) +(ert-deftest python-font-lock-keywords-level-1-1 () + (python-tests-assert-faces + "def func():" + '((1 . font-lock-keyword-face) (4) + (5 . font-lock-function-name-face) (9)))) + +(ert-deftest python-font-lock-keywords-level-1-2 () + "Invalid function name should not be font-locked." + (python-tests-assert-faces + "def 1func():" + '((1 . font-lock-keyword-face) (4)))) + (ert-deftest python-font-lock-assignment-statement-1 () (python-tests-assert-faces "a, b, c = 1, 2, 3" -- 2.39.2