Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional support for comments #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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