emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/json-mode 093e848 2/6: Use syntactic fontification for


From: Stefan Monnier
Subject: [elpa] externals/json-mode 093e848 2/6: Use syntactic fontification for JSON object names
Date: Sat, 28 Nov 2020 23:28:27 -0500 (EST)

branch: externals/json-mode
commit 093e848d4555fd535b04ab4b6dbd6df47bd66f6e
Author: Simen Heggestøyl <simenheg@gmail.com>
Commit: Simen Heggestøyl <simenheg@gmail.com>

    Use syntactic fontification for JSON object names
    
    * packages/json-mode/json-mode.el (json-mode-font-lock-keywords): Don't
    match object names and strings.
    (json-mode--string-is-object-name-p): New function determining whether a
    position is at the beginning of an object name.
    (json-font-lock-syntactic-face-function): Fontify object names.
---
 json-mode.el | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/json-mode.el b/json-mode.el
index defaae0..4c87a81 100644
--- a/json-mode.el
+++ b/json-mode.el
@@ -81,20 +81,28 @@
 (defvar json-mode-font-lock-keywords
   `(;; Constants
     (,(concat "\\<" (regexp-opt json-keywords) "\\>")
-     (0 font-lock-constant-face))
-    ;; Object names
-    ("\\(\"[^\"]*\"\\)[[:blank:]]*:"
-     (1 'json-mode-object-name-face))
-    ;; Strings
-    ("\"\\(\\\\.\\|[^\"]\\)*\""
-     (0 font-lock-string-face))))
+     (0 font-lock-constant-face))))
+
+(defun json-mode--string-is-object-name-p (startpos)
+  "Return t if STARTPOS is at the beginning of an object name."
+  (save-excursion
+    (goto-char startpos)
+    (and (eq (char-after) ?\")
+         (condition-case nil
+             (progn (forward-sexp 1) t)
+           (scan-error nil))
+         (looking-at "[[:blank:]]*:"))))
 
 (defun json-font-lock-syntactic-face-function (state)
-  "Highlight comments only.
-Strings are handled by `json-mode-font-lock-keywords', since we
-want to highlight object name strings differently from ordinary
-strings."
-  (when (nth 4 state) font-lock-comment-face))
+  "Determine which face to use for strings and comments.
+Object names receive the face `json-mode-object-name-face' to
+distinguish them from other strings."
+  (cond
+   ((nth 4 state) font-lock-comment-face)
+   ((and (nth 3 state)
+         (json-mode--string-is-object-name-p (nth 8 state)))
+    'json-mode-object-name-face)
+   (t font-lock-string-face)))
 
 (defconst json-mode--smie-grammar
   (smie-prec2->grammar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]