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 option to colorize quotes #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -11,6 +11,11 @@ if !exists("g:vim_json_syntax_conceal")
let g:vim_json_syntax_conceal = 1
end

"don't colorize quotes by default
if !exists("g:vim_json_color_quotes")
let g:vim_json_color_quotes = 0
end

"have warnings by default
if !exists("g:vim_json_warnings")
let g:vim_json_warnings = 1
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Specific customizations
* 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`.)
* 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.
* Added option for **colored quotes**. To enable it add `let g:vim_json_color_quotes=1` to your `.vimrc`.

Screenshots
-----------
Expand Down
30 changes: 20 additions & 10 deletions syntax/json.vim
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,38 @@ if !exists("main_syntax")
let main_syntax = 'json'
endif

" parse options and set vars
if has('conceal') && g:vim_json_syntax_conceal == 1
let conceal = " concealends"
else
let conceal = ""
endif

if g:vim_json_color_quotes == 1
let keyword_quote_match_group = "jsonKeywordQuote"
let string_quote_match_group = "jsonStringQuote"
else
let keyword_quote_match_group = "jsonQuote"
let string_quote_match_group = "jsonQuote"
endif


syntax match jsonNoise /\%(:\|,\)/

" NOTE that for the concealing to work your conceallevel should be set to 2

" Syntax: Strings
" Separated into a match and region because a region by itself is always greedy
syn match jsonStringMatch /"\([^"]\|\\\"\)\+"\ze[[:blank:]\r\n]*[,}\]]/ contains=jsonString
if has('conceal') && g:vim_json_syntax_conceal == 1
syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ concealends contains=jsonEscape contained
else
syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=jsonEscape contained
endif
execute 'syn region jsonString oneline matchgroup=' . string_quote_match_group . ' start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=jsonEscape contained' . conceal
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of running different syn commands based on user options, we'll set some vars and build the desired syn command from those


" Syntax: JSON does not allow strings with single quotes, unlike JavaScript.
syn region jsonStringSQError oneline start=+'+ skip=+\\\\\|\\"+ end=+'+

" Syntax: JSON Keywords
" Separated into a match and region because a region by itself is always greedy
syn match jsonKeywordMatch /"\([^"]\|\\\"\)\+"[[:blank:]\r\n]*\:/ contains=jsonKeyword
if has('conceal') && g:vim_json_syntax_conceal == 1
syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ concealends contains=jsonEscape contained
else
syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ contains=jsonEscape contained
endif
execute 'syn region jsonKeyword matchgroup=' . keyword_quote_match_group . ' start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ contains=jsonEscape contained' . conceal

" Syntax: Escape sequences
syn match jsonEscape "\\["\\/bfnrt]" contained
Expand Down Expand Up @@ -121,6 +129,8 @@ if version >= 508 || !exists("did_json_syn_inits")
endif
hi def link jsonQuote Quote
hi def link jsonNoise Noise
hi def link jsonKeywordQuote jsonKeyword
hi def link jsonStringQuote jsonString
endif

let b:current_syntax = "json"
Expand Down