unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Simen Heggestøyl" <simenheg@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: [elpa] master ba07fd7: New package: json-mode
Date: Fri, 16 Dec 2016 17:09:42 +0100	[thread overview]
Message-ID: <1481904582.2154.11@smtp.gmail.com> (raw)
In-Reply-To: <jwv4m2axv4l.fsf-monnier+emacsdiffs@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 906 bytes --]

Hi Stefan, thanks for your comments.

On Sun, Dec 11, 2016 at 6:03 PM, Stefan Monnier 
<monnier@iro.umontreal.ca> wrote:
> Would it make sense to inherit some things (such as the syntax-table)
> from js-mode?

JSON's syntax is a subset of the JavaScript syntax, so I think it would
make sense. On the other hand, the JSON syntax is very simple, and
js-mode currently uses cc-mode to produce a syntax table which contains
much more than json-mode needs, so maybe it's OK to just define what we
need in json-mode. What do you think?

> Are these meant to match strings that span several lines?
> If no, the regexps are wrong (because they can match multiple lines).
> If yes, this will fail in various circumstances
> (see (info "(elisp)Font Lock Multiline") for more details).

No. They're removed in the proposed diff.

> Why not do something like [...]

Looks good to me. See the attached diff.

-- Simen

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-WIP.patch --]
[-- Type: text/x-patch, Size: 1944 bytes --]

From 51dc7f55490a957bc4cb289c7f97b0688dd67dfe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <simenheg@gmail.com>
Date: Fri, 16 Dec 2016 17:05:53 +0100
Subject: [PATCH] WIP

---
 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
-- 
2.10.2


  reply	other threads:[~2016-12-16 16:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20161211145355.28952.97223@vcs.savannah.gnu.org>
     [not found] ` <20161211145356.0B6942201B7@vcs.savannah.gnu.org>
     [not found]   ` <=?UTF-8?Q?20161211145356.0B6942201B7@vcs.savannah.gnu.org>
2016-12-11 17:03   ` [elpa] master ba07fd7: New package: json-mode Stefan Monnier
2016-12-16 16:09     ` Simen Heggestøyl [this message]
2016-12-16 18:33       ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1481904582.2154.11@smtp.gmail.com \
    --to=simenheg@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).