[vim/vim] added option to allow json comments 'g:vim_json_allow_comments=1' (#6400)

320 views
Skip to first unread message

bugrasan

unread,
Jul 5, 2020, 5:49:33 PM7/5/20
to vim/vim, Subscribed

even comments in JSON aren't allowed, there are more and more cases where comments in JSON are accepted, e.g. TypeScript, Visual Studio Code, etc.
This patch adds the config parameter g:vim_json_allow_comments=1 to highlight comments as comments instead of error.

Note: highlighting of /* */ comments don't work properly; i hope somebody with more knowledge on syntax highlighting might help out...


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/6400

Commit Summary

  • added option to allow json comments 'g:vim_json_allow_comments=1'

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

codecov[bot]

unread,
Jul 5, 2020, 6:16:11 PM7/5/20
to vim/vim, Subscribed

Codecov Report

Merging #6400 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@

##           master    #6400      +/-   ##

==========================================

- Coverage   88.02%   88.00%   -0.02%     

==========================================

  Files         144      144              

  Lines      159143   159143              

==========================================

- Hits       140084   140054      -30     

- Misses      19059    19089      +30     
Impacted Files Coverage Δ
src/ui.c 79.83% <0.00%> (-3.82%) ⬇️
src/gui.c 62.92% <0.00%> (-0.71%) ⬇️
src/os_unix.c 70.66% <0.00%> (-0.23%) ⬇️
src/if_xcmdsrv.c 88.73% <0.00%> (-0.18%) ⬇️
src/sign.c 94.77% <0.00%> (-0.18%) ⬇️
src/gui_gtk_x11.c 58.57% <0.00%> (-0.15%) ⬇️
src/ex_getln.c 91.84% <0.00%> (ø)
src/search.c 91.46% <0.00%> (+0.10%) ⬆️
src/netbeans.c 76.37% <0.00%> (+0.29%) ⬆️
src/testing.c 93.64% <0.00%> (+0.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0ad3e89...6162da6. Read the comment docs.

K.Takata

unread,
Jul 5, 2020, 7:43:03 PM7/5/20
to vim/vim, Subscribed

Have you contacted the maintainer?

bfrg

unread,
Jul 5, 2020, 8:03:52 PM7/5/20
to vim/vim, Subscribed

Comments are not allowed in json. This is non-standard. There is really no need to add yet another option. I can't even think of a program that supports them OTOH.

If you really need the comments, just add the following to your after/syntax/json.vim:

hi link jsonCommentError Comment

The whole point of highlight-default is to use the default value only if it hasn't been linked yet. This is from :help :highlight-default:

Using [default] is especially useful to overrule the highlighting of a
specific syntax file. For example, the C syntax file contains: >
	:highlight default link cComment Comment
If you like Question highlighting for C comments, put this in your vimrc file: >
	:highlight link cComment Question

For the /* */ comments just take a look at the runtime/syntax/java.vim, javascript.vim, or c.vim files. They all use the same type of comments.

bugrasan

unread,
Jul 6, 2020, 12:49:47 AM7/6/20
to vim/vim, Subscribed

thank you for the hints, i will look at them later tonight.

typescript compiler generates tsconfig.json with comments, and the questions on how to make it work has been asked here: https://stackoverflow.com/questions/55669954/why-does-vim-highlight-all-my-json-comments-in-red

Bram Moolenaar

unread,
Jul 6, 2020, 3:33:40 PM7/6/20
to vim/vim, Subscribed

"json with comments is not json".
You can find plenty of references to this.
Perhaps we can use a different filetype for this? It's Javascript without the script.

Ben Jackson

unread,
Jul 29, 2020, 1:12:08 PM7/29/20
to vim/vim, Subscribed

Throwing in my 2p, as I has this debate with myself recently.

Increasingly, json is being used (for better or worse) as a human writable configuration format. The author of JSON famously said[see citation] that JSON doesn't contain comments (after all, it's an interchange format), but that if it were used as a configuration file format, then it makes perfect sense to use comments, and that implementations should run it through some sort of "minifier" before parsing.

All in all, JSON (the interchange format) certainly does not contain comments, but files-containing-a-single-JSON-object (let's call them .json files) legitimately can (and do).

citation:
My reference for this was this: http://web.archive.org/web/20100629021329/http://blog.getify.com/2010/06/json-comments/ (sadly the link to the source is dead, so it may not be reliable). Indeed I use json.minify for this purpose.

Summary of the quote:

A JSON encoder MUST NOT output comments. A JSON decoder MAY accept and ignore comments.

Kevin Locke

unread,
Jun 25, 2021, 3:00:01 PM6/25/21
to vim/vim, Subscribed

To add a bit of context, elzr/vim-json#61 is a long-standing open PR to add support for g:vim_json_comments. I forked it as kevinoid/vim-jsonc for the jsonc filetype and have incorporated a few fixes. Unfortunately, I've also found some significant implementation issues which I haven't had time to fix (e.g. kevinoid/vim-jsonc#1).

I'd be willing to fix the issues and help upstream the changes into elzr/vim-json or vim/vim if it is likely to be accepted.

I can't even think of a program that supports them OTOH.

It's not uncommon in the Node.js world. See https://github.com/kevinoid/vim-jsonc/blob/master/ftdetect/jsonc.vim for a list of recognizable filenames I've come across. (Along with manifest.json for WebExtensions which supports // but not /* */ comments.)

Perhaps we can use a different filetype for this?

I agree. Using a different filetype has the advantage of being easy to set via modelines and easy for users to understand and use. I'd suggest jsonc (obviously) but I've seen cjson used, and am not opposed to something more explicit like jsonwithcomments.

Bram Moolenaar

unread,
Jun 25, 2021, 3:48:05 PM6/25/21
to vim/vim, Subscribed

I'm not against jsonc, unless it's ambiguous. It seems to be known: https://filext.com/file-extension/JSONC
cjson has much fewer matches, and the first one refers to a Vim plugin :-)

Christian Brabandt

unread,
Jun 27, 2021, 1:54:14 PM6/27/21
to vim/vim, Subscribed

closing this for #8450 (which just has been merged)

Christian Brabandt

unread,
Jun 27, 2021, 1:54:16 PM6/27/21
to vim/vim, Subscribed

Closed #6400.

Kevin Locke

unread,
Jun 27, 2021, 1:57:49 PM6/27/21
to vim/vim, Subscribed

@chrisbra Thanks for tidying up the issues, but I'm not sure how #8450 (setting ft=json for JSON Patch) is related to this (supporting comments in JSON)?

(Other than that me commenting on both back-to-back, since I came across this issue when searching for JSON issues before opening that one. 😉 Sorry for the confusion.)

Christian Brabandt

unread,
Jun 27, 2021, 1:58:50 PM6/27/21
to vim/vim, Subscribed

ah, okay. Sorry for the confusion. Will re-open then.

Christian Brabandt

unread,
Jun 27, 2021, 1:58:53 PM6/27/21
to vim/vim, Subscribed

Reopened #6400.

bfrg

unread,
Mar 20, 2022, 2:19:47 PM3/20/22
to vim/vim, Subscribed

I think this can be closed. Users who need comments in their json, can set the filetype to jsonc. There's already a syntax and filetype file in runtime for that.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/6400/c1073305248@github.com>

bugrasan

unread,
Mar 20, 2022, 2:33:02 PM3/20/22
to vim/vim, Subscribed

agree, thank you :-)


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/6400/c1073307359@github.com>

bugrasan

unread,
Mar 20, 2022, 2:33:06 PM3/20/22
to vim/vim, Subscribed

Closed #6400.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/6400/issue_event/6271587121@github.com>

Reply all
Reply to author
Forward
0 new messages