json.vim 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. " Vim syntax file
  2. " Language: JSON
  3. " Maintainer: vacancy
  4. " Previous Maintainer: Eli Parra <eli@elzr.com>
  5. " Last Change: 2019 Sep 17
  6. " Version: 0.12
  7. if !exists("main_syntax")
  8. " quit when a syntax file was already loaded
  9. if exists("b:current_syntax")
  10. finish
  11. endif
  12. let main_syntax = 'json'
  13. endif
  14. syntax match jsonNoise /\%(:\|,\)/
  15. " NOTE that for the concealing to work your conceallevel should be set to 2
  16. " Syntax: JSON Keywords
  17. " Separated into a match and region because a region by itself is always greedy
  18. syn match jsonKeywordMatch /"\([^"]\|\\\"\)\+"[[:blank:]\r\n]*\:/ contains=jsonKeyword
  19. if has('conceal') && (!exists("g:vim_json_conceal") || g:vim_json_conceal==1)
  20. syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ concealends contained
  21. else
  22. syn region jsonKeyword matchgroup=jsonQuote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ contained
  23. endif
  24. " Syntax: Strings
  25. " Separated into a match and region because a region by itself is always greedy
  26. " Needs to come after keywords or else a json encoded string will break the
  27. " syntax
  28. syn match jsonStringMatch /"\([^"]\|\\\"\)\+"\ze[[:blank:]\r\n]*[,}\]]/ contains=jsonString
  29. if has('conceal') && (!exists("g:vim_json_conceal") || g:vim_json_conceal==1)
  30. syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ concealends contains=jsonEscape contained
  31. else
  32. syn region jsonString oneline matchgroup=jsonQuote start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=jsonEscape contained
  33. endif
  34. " Syntax: JSON does not allow strings with single quotes, unlike JavaScript.
  35. syn region jsonStringSQError oneline start=+'+ skip=+\\\\\|\\"+ end=+'+
  36. " Syntax: Escape sequences
  37. syn match jsonEscape "\\["\\/bfnrt]" contained
  38. syn match jsonEscape "\\u\x\{4}" contained
  39. " Syntax: Numbers
  40. syn match jsonNumber "-\=\<\%(0\|[1-9]\d*\)\%(\.\d\+\)\=\%([eE][-+]\=\d\+\)\=\>\ze[[:blank:]\r\n]*[,}\]]"
  41. " ERROR WARNINGS **********************************************
  42. if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
  43. " Syntax: Strings should always be enclosed with quotes.
  44. syn match jsonNoQuotesError "\<[[:alpha:]][[:alnum:]]*\>"
  45. syn match jsonTripleQuotesError /"""/
  46. " Syntax: An integer part of 0 followed by other digits is not allowed.
  47. syn match jsonNumError "-\=\<0\d\.\d*\>"
  48. " Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
  49. syn match jsonNumError "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"
  50. " Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
  51. syn match jsonCommentError "//.*"
  52. syn match jsonCommentError "\(/\*\)\|\(\*/\)"
  53. " Syntax: No semicolons in JSON
  54. syn match jsonSemicolonError ";"
  55. " Syntax: No trailing comma after the last element of arrays or objects
  56. syn match jsonTrailingCommaError ",\_s*[}\]]"
  57. " Syntax: Watch out for missing commas between elements
  58. syn match jsonMissingCommaError /\("\|\]\|\d\)\zs\_s\+\ze"/
  59. syn match jsonMissingCommaError /\(\]\|\}\)\_s\+\ze"/ "arrays/objects as values
  60. syn match jsonMissingCommaError /}\_s\+\ze{/ "objects as elements in an array
  61. syn match jsonMissingCommaError /\(true\|false\)\_s\+\ze"/ "true/false as value
  62. endif
  63. " ********************************************** END OF ERROR WARNINGS
  64. " Allowances for JSONP: function call at the beginning of the file,
  65. " parenthesis and semicolon at the end.
  66. " Function name validation based on
  67. " http://stackoverflow.com/questions/2008279/validate-a-javascript-function-name/2008444#2008444
  68. syn match jsonPadding "\%^[[:blank:]\r\n]*[_$[:alpha:]][_$[:alnum:]]*[[:blank:]\r\n]*("
  69. syn match jsonPadding ");[[:blank:]\r\n]*\%$"
  70. " Syntax: Boolean
  71. syn match jsonBoolean /\(true\|false\)\(\_s\+\ze"\)\@!/
  72. " Syntax: Null
  73. syn keyword jsonNull null
  74. " Syntax: Braces
  75. syn region jsonFold matchgroup=jsonBraces start="{" end=/}\(\_s\+\ze\("\|{\)\)\@!/ transparent fold
  76. syn region jsonFold matchgroup=jsonBraces start="\[" end=/]\(\_s\+\ze"\)\@!/ transparent fold
  77. " Define the default highlighting.
  78. " Only when an item doesn't have highlighting yet
  79. hi def link jsonPadding Operator
  80. hi def link jsonString String
  81. hi def link jsonTest Label
  82. hi def link jsonEscape Special
  83. hi def link jsonNumber Number
  84. hi def link jsonBraces Delimiter
  85. hi def link jsonNull Function
  86. hi def link jsonBoolean Boolean
  87. hi def link jsonKeyword Label
  88. if (!exists("g:vim_json_warnings") || g:vim_json_warnings==1)
  89. hi def link jsonNumError Error
  90. hi def link jsonCommentError Error
  91. hi def link jsonSemicolonError Error
  92. hi def link jsonTrailingCommaError Error
  93. hi def link jsonMissingCommaError Error
  94. hi def link jsonStringSQError Error
  95. hi def link jsonNoQuotesError Error
  96. hi def link jsonTripleQuotesError Error
  97. endif
  98. hi def link jsonQuote Quote
  99. hi def link jsonNoise Noise
  100. let b:current_syntax = "json"
  101. if main_syntax == 'json'
  102. unlet main_syntax
  103. endif
  104. " Vim settings
  105. " vim: ts=8 fdm=marker
  106. " MIT License
  107. " Copyright (c) 2013, Jeroen Ruigrok van der Werven, Eli Parra
  108. "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  109. "The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  110. "THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  111. "See https://twitter.com/elzr/status/294964017926119424