jsonc.vim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. " Vim syntax file
  2. " Language: JSONC (JSON with Comments)
  3. " Original Author: Izhak Jakov <izhak724@gmail.com>
  4. " Acknowledgement: Based off of vim-jsonc maintained by Kevin Locke <kevin@kevinlocke.name>
  5. " https://github.com/kevinoid/vim-jsonc
  6. " License: MIT
  7. " Last Change: 2021-07-01
  8. " Ensure syntax is loaded once, unless nested inside another (main) syntax
  9. " For description of main_syntax, see https://stackoverflow.com/q/16164549
  10. if !exists('g:main_syntax')
  11. if exists('b:current_syntax') && b:current_syntax ==# 'jsonc'
  12. finish
  13. endif
  14. let g:main_syntax = 'jsonc'
  15. endif
  16. " Based on vim-json syntax
  17. runtime! syntax/json.vim
  18. " Remove syntax group for comments treated as errors
  19. if !exists("g:vim_json_warnings") || g:vim_json_warnings
  20. syn clear jsonCommentError
  21. endif
  22. syn match jsonStringMatch /"\([^"]\|\\\"\)\+"\ze\(\_s*\/\/.*\_s*\)*[}\]]/ contains=jsonString
  23. syn match jsonStringMatch /"\([^"]\|\\\"\)\+"\ze\_s*\/\*\_.*\*\/\_s*[}\]]/ contains=jsonString
  24. syn match jsonTrailingCommaError /\(,\)\+\ze\(\_s*\/\/.*\_s*\)*[}\]]/
  25. syn match jsonTrailingCommaError /\(,\)\+\ze\_s*\/\*\_.*\*\/\_s*[}\]]/
  26. " Define syntax matching comments and their contents
  27. syn keyword jsonCommentTodo FIXME NOTE TBD TODO XXX
  28. syn region jsonLineComment start=+\/\/+ end=+$+ contains=@Spell,jsonCommentTodo keepend
  29. syn region jsonComment start='/\*' end='\*/' contains=@Spell,jsonCommentTodo fold
  30. " Link comment syntax comment to highlighting
  31. hi! def link jsonLineComment Comment
  32. hi! def link jsonComment Comment
  33. " Set/Unset syntax to avoid duplicate inclusion and correctly handle nesting
  34. let b:current_syntax = 'jsonc'
  35. if g:main_syntax ==# 'jsonc'
  36. unlet g:main_syntax
  37. endif