[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master f584f66: Use syntactic fontification for JSON object names
From: |
Simen Heggestøyl |
Subject: |
[elpa] master f584f66: Use syntactic fontification for JSON object names |
Date: |
Sun, 18 Dec 2016 13:00:43 +0000 (UTC) |
branch: master
commit f584f667717321aaf3bc020a38918523003bd90a
Author: Simen Heggestøyl <address@hidden>
Commit: Simen Heggestøyl <address@hidden>
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.
---
packages/json-mode/json-mode.el | 32 ++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/packages/json-mode/json-mode.el b/packages/json-mode/json-mode.el
index defaae0..4c87a81 100644
--- a/packages/json-mode/json-mode.el
+++ b/packages/json-mode/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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [elpa] master f584f66: Use syntactic fontification for JSON object names,
Simen Heggestøyl <=