Skip to content

Commit

Permalink
Add optional support for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLocehiliosan committed Sep 25, 2015
1 parent f5e3181 commit 9dc5d13
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ftplugin/json.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ if !exists("g:vim_json_warnings")
let g:vim_json_warnings = 1
end

"have comments off by default
if !exists("g:vim_json_comments")
let g:vim_json_comments = 0
end

"set concealcursor blank by default
"this should turn off the concealing in the current line (where the cursor is at),
"on all modes (normal, visual, insert)
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Specific customizations
* 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).
* Warn about *trailing commas* after the last element in arrays or objects.
* (All warnings can be turned off with a `let g:vim_json_warnings=0` in your `vimrc`.)
* Optionally allow comment syntax highlighting. (turn this on with a `let g:vim_json_comments=1` in your `vimrc`.)
* 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.

Screenshots
Expand Down
23 changes: 20 additions & 3 deletions syntax/json.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
" Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
syn match jsonNumError "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"

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

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

" COMMENTS ****************************************************
if (exists("g:vim_json_comments") && g:vim_json_comments==1)
syn keyword jsonCommentTodo TODO FIXME XXX TBD contained
syn match jsonLineComment "\/\/.*" contains=@Spell,jsonCommentTodo
syn match jsonCommentSkip "^[ \t]*\*\($\|[ \t]\+\)"
syn region jsonComment start="/\*" end="\*/" contains=@Spell,jsonCommentTodo
endif

" ********************************************** END OF ERROR WARNINGS
" Allowances for JSONP: function call at the beginning of the file,
" parenthesis and semicolon at the end.
Expand Down Expand Up @@ -117,6 +127,13 @@ if version >= 508 || !exists("did_json_syn_inits")
hi def link jsonNoQuotesError Error
hi def link jsonTripleQuotesError Error
endif

if (exists("g:vim_json_comments") && g:vim_json_comments==1)
hi def link jsonCommentTodo Todo
hi def link jsonLineComment Comment
hi def link jsonComment Comment
endif

hi def link jsonQuote Quote
hi def link jsonNoise Noise
endif
Expand Down

0 comments on commit 9dc5d13

Please sign in to comment.