From 7e1052ad0c88848a6cf6b6f74360c59d0808c607 Mon Sep 17 00:00:00 2001 From: michaelPotter Date: Wed, 16 Sep 2020 17:30:00 -0700 Subject: [PATCH 1/2] add option to colorize quotes --- ftplugin/json.vim | 5 +++++ readme.md | 1 + syntax/json.vim | 30 ++++++++++++++++++++---------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ftplugin/json.vim b/ftplugin/json.vim index 9860127..c7728a5 100644 --- a/ftplugin/json.vim +++ b/ftplugin/json.vim @@ -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 diff --git a/readme.md b/readme.md index e3e6c6c..1d411a3 100644 --- a/readme.md +++ b/readme.md @@ -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 ----------- diff --git a/syntax/json.vim b/syntax/json.vim index 8d35609..020036a 100644 --- a/syntax/json.vim +++ b/syntax/json.vim @@ -16,6 +16,22 @@ 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 @@ -23,11 +39,7 @@ syntax match jsonNoise /\%(:\|,\)/ " 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 " Syntax: JSON does not allow strings with single quotes, unlike JavaScript. syn region jsonStringSQError oneline start=+'+ skip=+\\\\\|\\"+ end=+'+ @@ -35,11 +47,7 @@ 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 @@ -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" From fb45893b996ab3e0c1cd6217caf07da8912a0d21 Mon Sep 17 00:00:00 2001 From: michaelPotter Date: Tue, 12 Mar 2024 18:19:55 -0700 Subject: [PATCH 2/2] Don't do ftdetect --- ftdetect/json.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ftdetect/json.vim b/ftdetect/json.vim index 2e45f02..1dd3246 100644 --- a/ftdetect/json.vim +++ b/ftdetect/json.vim @@ -1,5 +1,5 @@ -autocmd BufNewFile,BufRead *.json setlocal filetype=json -autocmd BufNewFile,BufRead *.jsonl setlocal filetype=json -autocmd BufNewFile,BufRead *.jsonp setlocal filetype=json -autocmd BufNewFile,BufRead *.geojson setlocal filetype=json -autocmd BufNewFile,BufRead *.template setlocal filetype=json +" autocmd BufNewFile,BufRead *.json setlocal filetype=json +" autocmd BufNewFile,BufRead *.jsonl setlocal filetype=json +" autocmd BufNewFile,BufRead *.jsonp setlocal filetype=json +" autocmd BufNewFile,BufRead *.geojson setlocal filetype=json +" autocmd BufNewFile,BufRead *.template setlocal filetype=json