elzr/vim-json

Color strings differently than other value types

khepler opened this issue ยท 7 comments

I would like the ability to color values of type string differently than values of other types. This would make obvious which values are strings with concealment enabled.

For example, this is GitHub's JSON highlighting:

{
    "key1":"1.0",
    "key2":1.0
}

๐Ÿ‘ This is the only thing keeping me from using concealment

elzr commented

image

This took long enough but it's finally here! Since the beginning these different value types were being recognized and tagged appropriately in the syntax file, as Number and Boolean. But apparently most color schemes don't have distinct colors for them. I despaired of how to solve this in a general way for a long time, finally I just ended up tagging numbers and booleans as Delimiter. It's hackish but it works.

FYI the commit is not on master, or has been reverted. I had to change the syntax file to change numbers and bools to Delimiter.

As mentioned above, this change has been reverted. While updating the syntax file to set numbers and bools to Delimiter is a solution that works universally and might be a good solution for this package's defaults, it feels a tiny bit hacky to me, and a more correct solution, albeit one that relies on end-user configuration, seems to be simply to update either your colorscheme or your .vimrc to color String, Number, and Boolean differently.

For example, add something like this to ~/.vimrc:

highlight String ctermfg=green
highlight Number ctermfg=blue
highlight Boolean ctermfg=magenta

Maybe it would be a good idea if a tip along these lines could be added to the documentation.

@ctranstrum A better option is to add it in json.vim file ~/.vim/ftplugin/json.vim with all other shenanigans needed for it to work with indentLine plugin, and maybe more...

highlight String ctermfg=green
highlight Number ctermfg=blue
highlight Boolean ctermfg=magenta

" Multiple folding recipes to not open in folded mode
" au BufRead * normal zR
" set foldlevel=99
setlocal foldmethod=syntax
set foldlevel=99

" etc

Quick testing json:

{
  "bool": true,
  "string": "500",
  "integer, NOT a string": 500,
  "null value": null,
  "error": Error,
  "to do": TODO,
  "array": [ "John", "Peter" ],
  "object": { "name": "John", "age": 30 }
}

Maybe useful too:

highlight Normal ctermfg=grey
highlight Function ctermfg=darkred  " Null value
highlight Error ctermfg=red

I think it should be jsonConstant, jsonBoolean, etc. like here https://github.com/elzr/vim-json/blob/master/syntax/json.vim#L103; not just Constant, Boolean, etc. because that overwrites all filetype syntax-es (or I suffer it if I code it in ~/.vim/ftplugin/json.vim as soon as I open a json file).