9

I've got some comments in a JSON file that were autogenerated by, and unfortunately it seems like vim can't recognize that they're just comments.

enter image description here

They're all red - which one of my plugins is doing this?

I don't really want to turn all of my syntax highlighting off, and I also don't want to clear the errors manually each time I run into the red highlighting.

" Enable syntax highlighting
syntax enable

" PLUGINS
call plug#begin('~/.vim/plugged')

"   deoplete - code completion
if has('nvim')
"      Add in a syntax file for deoplete typescripe, then add deoplete
    Plug 'HerringtonDarkholme/yats.vim'
    Plug 'mhartington/nvim-typescript', {'do': './install.sh'}
    Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
    Plug 'Shougo/deoplete.nvim'
    Plug 'roxma/nvim-yarp'
    Plug 'roxma/vim-hug-neovim-rpc'
    Plug 'ternjs/tern_for_vim', { 'do': 'npm install' }
endif

"   fzf - fuzzy find
Plug 'junegunn/fzf', { 'dir': '~/.vim/installed/fzf' }

"   lightline - a nice looking bottom bar
Plug 'itchyny/lightline.vim'
"       also get rid of the useless -- INSERT -- since we have a nice bar
set noshowmode

"   nerdtree - a little tree file browser 
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
"       map it to control-O
map <C-o> :NERDTreeToggle<CR>

"   gitgutter - adds some git context to the left side bar
Plug 'airblade/vim-gitgutter'
set updatetime=100

"   ale - asynchronous linting engine, highlights stuff
Plug 'w0rp/ale'

"   deoplete-ternjs - adds javascript to deoplete
Plug 'carlitux/deoplete-ternjs'
"   tern_for_vim - adds in the tern 'engine' or whatever to vim
Plug 'ternjs/tern_for_vim', { 'do': 'npm install && npm install -g tern' }

"   vimproc - async execution for things
Plug 'Shougo/vimproc.vim', { 'do': 'make' }
"   tsuquyomi - unpronouncable client for TSServer for completion and more
Plug 'Quramy/tsuquyomi', { 'do': 'npm install -g typescript' }

call plug#end()

" CONFIGURE - some of our plugins need configurations so add that in

let g:deoplete#enable_at_startup = 1
let g:deoplete#enable_ignore_case = 1
let g:deoplete#enable_smart_case = 1
let g:deoplete#enable_camel_case = 1
let g:deoplete#enable_refresh_always = 1
let g:deoplete#max_abbr_width = 0
let g:deoplete#max_menu_width = 0
let g:deoplete#omni#input_patterns = get(g:,'deoplete#omni#input_patterns',{})

let g:tern_request_timeout = 1
let g:tern_request_timeout = 6000
let g:tern#command = ["tern"]
let g:tern#arguments = ["--persistent"]
let g:deoplete#sources#tss#javascript_support = 1
let g:tsuquyomi_javascript_support = 1
let g:tsuquyomi_auto_open = 1
let g:tsuquyomi_disable_quickfix = 1
ARMATAV
  • 604
  • 6
  • 26
  • 5
    Unless I'm horribly mistaken, the JSON format doesn't allow comments. The thing you've used either generates something completely bogus, or some non-standard variant of JSON, or, as the answer [here](https://stackoverflow.com/questions/244777/can-comments-be-used-in-json) suggest, the comments need to be stripped out by another tool. What did you use to generate the JSON? In either case, vim is very likely to only recognize the standard JSON as valid, and highlights this as errors. – Michail Apr 13 '19 at 22:39
  • @Michail It was generated by `npx tsc` and some parameters found here: https://medium.freecodecamp.org/build-an-apollo-graphql-server-with-typescript-and-webpack-hot-module-replacement-hmr-3c339d05184f - wonder why it makes comments :/ – ARMATAV Apr 13 '19 at 23:34
  • In the article you've linked a `--removeComments` flag is mentioned. Perhaps you forgot to add it when you run `npx`? – Michail Apr 14 '19 at 10:21
  • 1
    In a pinch, you can always just `:set filetype=javascript` – dlitz Sep 30 '21 at 02:49

5 Answers5

11

As @Michail mentioned, JSON syntax does not support comments, so Vim marks them as error.

padawin
  • 4,230
  • 15
  • 19
  • 3
    There is a zillion and half tools that use a JSON-with-comments though. – Jan Hudec Jun 01 '20 at 10:33
  • @JanHudec https://en.wikipedia.org/wiki/JSON "Comments were purposefully excluded from JSON. In 2012" – Audacious Tux Jul 04 '20 at 07:02
  • 11
    @TanjimHossain and you point is? Everybody knows JSON-with-comments is not JSON, but that does not prevent people (Microsoft being the worst offender) from creating tools that produce and consume JSON-with-comments. Which makes other people need syntax highlighting that can understand JSON-with-comments (and it isn't just JSON-with-errors, because other syntax errors like trailing commas are still errors). – Jan Hudec Jul 04 '20 at 10:07
  • Microsoft is the worst offender of this. They barely follow their own standards. The best I can think of is that the parser they're using strips those comments out, or they don't actually read it in as JSON, and convert it to a byte string without verifying it's contents. – FilBot3 Aug 24 '20 at 17:11
  • Something that could be done is use a conversion tool to change YAML to JSON and JSON to YAML, then you could put comments in the document. YAML supports comments, and YAML supports everything JSON does. – FilBot3 Aug 24 '20 at 17:12
  • this answer should be a comment! – alexzander Jun 10 '21 at 07:55
8

This fixed it for me:

autocmd FileType json syntax match Comment +\/\/.\+$+

Not sure where i got it from.

EDIT:

NOTE that for this to work you must let vim handle filetype detection by, filetype on. put that somewhere above the autocmd ...

Ali
  • 366
  • 4
  • 11
  • This doesn't do anything for me. – Jim Hessin Aug 03 '21 at 15:16
  • I guess you should put it in your vimrc and reopen vim. It won't do anything if you run it in command mode. It basically says: automatically, in any file with filetype json, treat syntax coloring of the following pattern like the syntax highlighting for a comment. in other words any string with the pattern after the word Comment will have the syntax coloring of a comment. – Ali Aug 03 '21 at 16:57
  • Of course I did that - still didn't do anything. – Jim Hessin Aug 04 '21 at 17:51
  • This works great for me. If the file extension is not json, they you can just execute `syntax match Comment +\/\/.\+$+` to unhighlight all lines that start with `//` – Abhilash Jan 15 '22 at 06:15
  • Nope, it doesn't work for me either. – Jarmos Apr 08 '22 at 08:44
  • Works for `// line comments`, but not for `/* block comments */` such as tsconfig.json uses. Nice and simple though. – jv-dev Aug 03 '22 at 20:00
  • 1
    @jv-dev for those file I think you can do: `autocmd FileType json syntax match Comment +\/\*.\+\*\/$+`. This is the syntax command. You can search for anything in `+regex+` and highlight them like `Comment` highlight group. – Ali Aug 06 '22 at 17:43
7

There is a plugin for commented json that I found to solve this issue: jsonc. You should check it out.

Jim Hessin
  • 137
  • 2
  • 11
  • This is a good answer, in that 'JSON with comments' is a side-spec that's frequently used. I believe the latest JSON spec version also allows comments, trailing commas, etc. Note you might have to explicitly activate this with `set syntax=jsonc` when you open the file, or add an appropriate `autocmd` to your vimrc. – Mark McKenna Nov 03 '21 at 13:12
  • From this, I found there is a filetype plugin, called `jsonc.vim` (in `/usr/share/vim/vim90/syntax/jsonc.vim`). So, you can add a [modeline](https://vimdoc.sourceforge.net/htmldoc/options.html#modeline) at the end of the json file: `// vim: ft=jsonc` – Alex Leach Feb 04 '23 at 19:40
2

While looking for some other weird things to do with (Neo)Vim's JSON highlighting, I found that you can disable such warnings with let g:vim_json_warnings=0. This is not really a "solution" as such (the proper solution would be adding comments to JSON syntax, even if it was an optional feature), but it certainly removes the giant ugly red boxes everywhere.

As mentioned in comments and the accepted answer, JSON doesn't have comments, but as it's such a common extension it is nice to be able to disable the warnings at the very least.

Will Eccles
  • 394
  • 1
  • 5
  • 15
2

To turn the JSON comment error highlighting off, add the following file in the user configuration folder.

after/syntax/json.vim

syntax clear jsonCommentError
syntax match jsonComment "//.*"
syntax match jsonComment "\(/\*\)\|\(\*/\)"
hi def link jsonComment Comment

To extend rules for NERDCommenter, add the following to vimrc.

let NERDCustomDelimiters = {'json': { 'left': '//', 'right': '' }}
packadd nerdcommenter
Konstantin Glukhov
  • 1,898
  • 3
  • 18
  • 25