Skip to content

Commit 684c235

Browse files
Add optional support for comments
1 parent f5e3181 commit 684c235

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

ftplugin/json.vim

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ if !exists("g:vim_json_warnings")
1616
let g:vim_json_warnings = 1
1717
end
1818

19+
"have comments off by default
20+
if !exists("g:vim_json_comments")
21+
let g:vim_json_comments = 0
22+
end
23+
1924
"set concealcursor blank by default
2025
"this should turn off the concealing in the current line (where the cursor is at),
2126
"on all modes (normal, visual, insert)

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Specific customizations
2323
* Warn about *[missing commas](https://github.com/elzr/vim-json/issues/18)* between elements of an object [and elsewhere](https://github.com/elzr/vim-json/issues/34).
2424
* Warn about *trailing commas* after the last element in arrays or objects.
2525
* (All warnings can be turned off with a `let g:vim_json_warnings=0` in your `vimrc`.)
26+
* Optionally allow comment syntax highlighting. (turn this one with a `let g:vim_json_comments=1` in your `vimrc`.)
2627
* Recognize `.jsonp` file type. In [JSONP](http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about), the wrapping function call at the beginning and the closing semicolon are recognized.
2728

2829
Screenshots

syntax/json.vim

+20-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
6060
" Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
6161
syn match jsonNumError "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"
6262

63-
" Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
64-
syn match jsonCommentError "//.*"
65-
syn match jsonCommentError "\(/\*\)\|\(\*/\)"
63+
if (!exists("g:vim_json_comments") || g:vim_json_comments==0)
64+
" Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
65+
syn match jsonCommentError "//.*"
66+
syn match jsonCommentError "\(/\*\)\|\(\*/\)"
67+
endif
6668

6769
" Syntax: No semicolons in JSON
6870
syn match jsonSemicolonError ";"
@@ -77,6 +79,14 @@ if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
7779
syn match jsonMissingCommaError /\(true\|false\)\_s\+\ze"/ "true/false as value
7880
endif
7981

82+
" COMMENTS ****************************************************
83+
if (exists("g:vim_json_comments") && g:vim_json_comments==1)
84+
syn keyword jsonCommentTodo TODO FIXME XXX TBD contained
85+
syn match jsonLineComment "\/\/.*" contains=@Spell,jsonCommentTodo
86+
syn match jsonCommentSkip "^[ \t]*\*\($\|[ \t]\+\)"
87+
syn region jsonComment start="/\*" end="\*/" contains=@Spell,jsonCommentTodo
88+
endif
89+
8090
" ********************************************** END OF ERROR WARNINGS
8191
" Allowances for JSONP: function call at the beginning of the file,
8292
" parenthesis and semicolon at the end.
@@ -117,6 +127,13 @@ if version >= 508 || !exists("did_json_syn_inits")
117127
hi def link jsonNoQuotesError Error
118128
hi def link jsonTripleQuotesError Error
119129
endif
130+
131+
if (exists("g:vim_json_comments") && g:vim_json_comments==1)
132+
hi def link jsonCommentTodo Todo
133+
hi def link jsonLineComment Comment
134+
hi def link jsonComment Comment
135+
endif
136+
120137
hi def link jsonQuote Quote
121138
hi def link jsonNoise Noise
122139
endif

0 commit comments

Comments
 (0)