From 8af4f8e6c04b163d39de4327fca7f69cce78c0ab Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Wed, 30 Dec 2020 12:13:50 +0100 Subject: [PATCH] Add jsonc, closes #635 --- README.md | 3 +- autoload/polyglot/sleuth.vim | 1 + ftdetect/polyglot.vim | 4 +++ indent/jsonc.vim | 5 +++ packages.yaml | 8 +++++ syntax/jsonc.vim | 61 ++++++++++++++++++++++++++++++++++++ tests/filetypes.vim | 1 + 7 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 indent/jsonc.vim create mode 100644 syntax/jsonc.vim diff --git a/README.md b/README.md index e88ed3dc7..c6f537b42 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A collection of language packs for Vim. > One to rule them all, one to find them, one to bring them all and in the darkness bind them. - It **won't affect your startup time**, as scripts are loaded only on demand\*. -- It **installs and updates 120+ times faster** than the 600 packages it consists of. +- It **installs and updates 120+ times faster** than the 601 packages it consists of. - It is also more secure (scripts loaded for every filetype are generated by vim-polyglot) - Best syntax and indentation support (no other features). Hand-selected language packs. - Automatically detects indentation (includes performance-optimized version of [vim-sleuth](https://github.com/tpope/vim-sleuth), can be disabled) @@ -111,6 +111,7 @@ On top of all language packs from [vim repository](https://github.com/vim/vim/tr - [jq](https://github.com/vito-c/jq.vim) (JSONiq syntax highlighting for jq files) - [json5](https://github.com/GutenYe/json5.vim) (JSON5 syntax highlighting for json5 files) - [json](https://github.com/elzr/vim-json) (JSON syntax highlighting for json, avsc, geojson, gltf, har and 13 more files) +- [jsonc](https://github.com/neoclide/jsonc.vim) (Syntax highlighting for cjson and jsonc files) - [jsonnet](https://github.com/google/vim-jsonnet) (Jsonnet syntax highlighting for jsonnet and libsonnet files) - [jst](https://github.com/briancollins/vim-jst) (EJS syntax highlighting for ejs, ect and jst files) - [jsx](https://github.com/MaxMEllon/vim-jsx-pretty) (JSX syntax highlighting for jsx files) diff --git a/autoload/polyglot/sleuth.vim b/autoload/polyglot/sleuth.vim index 155ae0b55..2fb3b8270 100644 --- a/autoload/polyglot/sleuth.vim +++ b/autoload/polyglot/sleuth.vim @@ -263,6 +263,7 @@ let s:globs = { \ 'jq': '*.jq,.jqrc,.jqrc*', \ 'json': '*.json,*.avsc,*.geojson,*.gltf,*.har,*.ice,*.JSON-tmLanguage,*.jsonl,*.mcmeta,*.tfstate,*.tfstate.backup,*.topojson,*.webapp,*.webmanifest,*.yy,*.yyp,*.jsonp,*.template,.arcconfig,.htmlhintrc,.tern-config,.tern-project,.watchmanconfig,composer.lock,mcmod.info,Pipfile.lock', \ 'json5': '*.json5', + \ 'jsonc': '*.cjson,*.jsonc,coc-settings.json,.eslintrc.json,.babelrc,.jshintrc,.jslintrc,.mocharc.json,coffeelint.json,tsconfig.json,jsconfig.json', \ 'jsonnet': '*.jsonnet,*.libsonnet', \ 'jsp': '*.jsp', \ 'jst': '*.ejs,*.ect,*.jst', diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim index 29a4a2c71..85c933e35 100644 --- a/ftdetect/polyglot.vim +++ b/ftdetect/polyglot.vim @@ -142,6 +142,10 @@ set cpo&vim " DO NOT EDIT CODE BELOW, IT IS GENERATED WITH MAKEFILE +if !has_key(g:polyglot_is_disabled, 'jsonc') + au BufNewFile,BufRead *.cjson,*.jsonc,{.,}babelrc,{.,}eslintrc.json,{.,}jshintrc,{.,}jslintrc,{.,}mocharc.json,coc-settings.json,coffeelint.json,jsconfig.json,tsconfig.json setf jsonc +endif + if !has_key(g:polyglot_is_disabled, 'mint') au BufNewFile,BufRead *.mint setf mint endif diff --git a/indent/jsonc.vim b/indent/jsonc.vim new file mode 100644 index 000000000..baffdae84 --- /dev/null +++ b/indent/jsonc.vim @@ -0,0 +1,5 @@ +if has_key(g:polyglot_is_disabled, 'jsonc') + finish +endif + +runtime! indent/json.vim diff --git a/packages.yaml b/packages.yaml index dc127f28d..a1bf7d401 100644 --- a/packages.yaml +++ b/packages.yaml @@ -5554,3 +5554,11 @@ filetypes: patterns: - pattern: '*.mint' description: Mint (https://www.mint-lang.com/) +--- +name: jsonc +remote: neoclide/jsonc.vim +filetypes: +- name: jsonc + patterns: + - pattern: '*.cjson,coc-settings.json,*.jsonc,.eslintrc.json,.babelrc,.jshintrc,.jslintrc,.mocharc.json,coffeelint.json,tsconfig.json,jsconfig.json' + description: 'JSON with comments (https://komkom.github.io/)' diff --git a/syntax/jsonc.vim b/syntax/jsonc.vim new file mode 100644 index 000000000..49c14bcc6 --- /dev/null +++ b/syntax/jsonc.vim @@ -0,0 +1,61 @@ +if has_key(g:polyglot_is_disabled, 'jsonc') + finish +endif + +" Syntax setup {{{1 +if exists('b:current_syntax') && b:current_syntax == 'jsonc' + finish +endif + +" Syntax: Strings {{{1 +syn region jsoncString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=jsoncEscape +syn region jsoncString start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=jsoncEscape + +" Syntax: JSON Keywords {{{1 +" Separated into a match and region because a region by itself is always greedy +syn match jsoncKeywordMatch /"\([^"]\|\\\"\)\+"[[:blank:]\r\n]*\:/ contains=jsonKeyword + +" Syntax: Escape sequences +syn match jsoncEscape "\\["\\/bfnrt]" contained +syn match jsoncEscape "\\u\x\{4}" contained + +" Syntax: Numbers {{{1 +syn match jsoncNumber "-\=\<\%(0\|[1-9]\d*\)\%(\.\d\+\)\=\%([eE][-+]\=\d\+\)\=\>" +syn keyword jsoncNumber Infinity -Infinity + +" Syntax: An integer part of 0 followed by other digits is not allowed. +syn match jsoncNumError "-\=\<0\d\.\d*\>" + +" Syntax: Boolean {{{1 +syn keyword jsoncBoolean true false + +" Syntax: Null {{{1 +syn keyword jsoncNull null + +" Syntax: Braces {{{1 +syn match jsoncBraces "[{}\[\]]" +syn match jsoncObjAssign /@\?\%(\I\|\$\)\%(\i\|\$\)*\s*\ze::\@!/ + +" Syntax: Comment {{{1 +syn region jsoncLineComment start=+\/\/+ end=+$+ keepend +syn region jsoncLineComment start=+^\s*\/\/+ skip=+\n\s*\/\/+ end=+$+ keepend fold +syn region jsoncComment start="/\*" end="\*/" fold + +" Define the default highlighting. {{{1 +hi def link jsoncString String +hi def link jsoncObjAssign Identifier +hi def link jsoncEscape Special +hi def link jsoncNumber Number +hi def link jsoncBraces Operator +hi def link jsoncNull Function +hi def link jsoncBoolean Boolean +hi def link jsoncLineComment Comment +hi def link jsoncComment Comment +hi def link jsoncNumError Error +hi def link jsoncKeywordMatch Label + +if !exists('b:current_syntax') + let b:current_syntax = 'jsonc' +endif + +" vim: fdm=marker diff --git a/tests/filetypes.vim b/tests/filetypes.vim index 08d4cb938..3b71ac76e 100644 --- a/tests/filetypes.vim +++ b/tests/filetypes.vim @@ -645,6 +645,7 @@ call TestFiletype('xpm') call TestFiletype('xpm2') call TestFiletype('context') call TestFiletype('mint') +call TestFiletype('jsonc') " DO NOT EDIT CODE ABOVE, IT IS GENERATED WITH MAKEFILE