Skip to content

Commit

Permalink
JSONP recognition of function call, syntax file sprucing up
Browse files Browse the repository at this point in the history
  • Loading branch information
elzr committed Aug 13, 2013
1 parent f26d1ab commit 37c223d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
8 changes: 8 additions & 0 deletions jsonp-test.jsonp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

whañteverJavascriptName ( {
"success":true,
"url":"http://google.com",
"shortUrl":"http://b1t.co/54"
});


5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Better JSON for VIM
===================

*Distinct highlighting of keywords vs values, JSON-specific (non-JS) warnings, quote concealing.*
![JSON syntax coloring](http://farm8.staticflickr.com/7234/7185560283_102f6b753d.jpg)
Customization of Jeroen Ruigrok van der Werven's [vim-json highlighting script](http://www.vim.org/scripts/script.php?script_id=1945) with Rogerz Zhang's [indent script](https://github.com/vim-scripts/vim-json-bundle).
[Pathogen-friendly.](https://github.com/tpope/vim-pathogen)
<br>![JSON syntax coloring](http://farm8.staticflickr.com/7234/7185560283_102f6b753d.jpg)
[Pathogen-friendly.](https://github.com/tpope/vim-pathogen)

Specific customizations
-----------------------
Expand All @@ -17,6 +17,7 @@ Specific customizations
* Warn about *decimals* smaller than 1 that don't start with a 0 (so `.1` gives a warning, it should be `0.1`).
* Warn about *comments* and *trailing semicolons*.
* Warn about *trailing commas* after the last element in arrays or objects.
* Recognize `.jsonp` file type. In [JSONP](http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about), the wrapping function call at the beginning and closing semicolon are recognized.

Screenshots
-----------
Expand Down
29 changes: 18 additions & 11 deletions syntax/json.vim
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ syn region jsonStringSQ oneline start=+'+ skip=+\\\\\|\\"+ end=+'+

" Syntax: JSON Keywords
" Separated into a match and region because a region by itself is always greedy
syn match jsonKeywordMatch /"[^\"\:]\+"\s*\:/ contains=jsonKeywordRegion
syn match jsonKeywordMatch /"[^\"\:]\+"[[:blank:]\r\n]*\:/ contains=jsonKeywordRegion
if has('conceal')
syn region jsonKeywordRegion matchgroup=Quote start=/"/ end=/"\ze\s*\:/ concealends contained
syn region jsonKeywordRegion matchgroup=Quote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ concealends contained
else
syn region jsonKeywordRegion matchgroup=Quote start=/"/ end=/"\ze\s*\:/ contained
syn region jsonKeywordRegion matchgroup=Quote start=/"/ end=/"\ze[[:blank:]\r\n]*\:/ contained
endif

" Syntax: Escape sequences
syn match jsonEscape "\\["\\/bfnrt]" contained
syn match jsonEscape "\\u\x\{4}" contained

" Syntax: Strings should always be enclosed with quotes.
syn match jsonNoQuotes "\<\w\+\>"

" Syntax: Numbers
syn match jsonNumber "-\=\<\%(0\|[1-9]\d*\)\%(\.\d\+\)\=\%([eE][-+]\=\d\+\)\=\>"

" ERROR WARNINGS **********************************************
"
" Syntax: Strings should always be enclosed with quotes.
syn match jsonNoQuotes "\<[[:alpha:]]\+\>"

" Syntax: An integer part of 0 followed by other digits is not allowed.
syn match jsonNumError "-\=\<0\d\.\d*\>"

" Syntax: Decimals smaller than one should begin with 0 (so .1 should be 0.1).
syn match jsonNumError "\:\@<=\s*\zs\.\d\+"
syn match jsonNumError "\:\@<=[[:blank:]\r\n]*\zs\.\d\+"

" Syntax: No comments in JSON, see http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
syn match jsonCommentError "//.*"
Expand All @@ -63,16 +63,22 @@ syn match jsonSemicolonError ";"
syn match jsonCommaError ",\_s*[}\]]"

" ********************************************** END OF ERROR WARNINGS
" Allowances for JSONP: function call at the beginning of the file,
" parenthesis and semicolon at the end.
" Function name validation based on
" http://stackoverflow.com/questions/2008279/validate-a-javascript-function-name/2008444#2008444
syn match jsonPadding "\%^[[:blank:]\r\n]*[_$[:alpha:]][_$[:alnum:]]*[[:blank:]\r\n]*("
syn match jsonPadding ");[[:blank:]\r\n]*\%$"

" Syntax: Boolean
syn keyword jsonBoolean true false
syn keyword jsonBoolean true false

" Syntax: Null
syn keyword jsonNull null
syn keyword jsonNull null

" Syntax: Braces
syn region jsonFold matchgroup=jsonBraces start="{" end="}" transparent fold
syn region jsonFold matchgroup=jsonBraces start="\[" end="]" transparent fold
syn region jsonFold matchgroup=jsonBraces start="{" end="}" transparent fold
syn region jsonFold matchgroup=jsonBraces start="\[" end="]" transparent fold

" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
Expand All @@ -84,6 +90,7 @@ if version >= 508 || !exists("did_json_syn_inits")
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsonPadding Operator
HiLink jsonString String
HiLink jsonEscape Special
HiLink jsonNumber Number
Expand Down

0 comments on commit 37c223d

Please sign in to comment.