Add jsx support, closes #56

This commit is contained in:
Adam Stankiewicz 2015-07-18 22:50:09 +02:00
parent 548653cafe
commit 1c80c4bb08
7 changed files with 247 additions and 0 deletions

View File

@ -51,6 +51,7 @@ Optionally download one of the [releases](https://github.com/sheerun/vim-polyglo
- [julia](https://github.com/dcjones/julia-minimalist-vim) (syntax, indent, ftdetect) - [julia](https://github.com/dcjones/julia-minimalist-vim) (syntax, indent, ftdetect)
- [json](https://github.com/sheerun/vim-json) (syntax, indent, ftdetect) - [json](https://github.com/sheerun/vim-json) (syntax, indent, ftdetect)
- [jst](https://github.com/briancollins/vim-jst) (syntax, indent, ftdetect) - [jst](https://github.com/briancollins/vim-jst) (syntax, indent, ftdetect)
- [jsx](https://github.com/mxw/vim-jsx) (after)
- [latex](https://github.com/LaTeX-Box-Team/LaTeX-Box) (syntax, indent, ftplugin) - [latex](https://github.com/LaTeX-Box-Team/LaTeX-Box) (syntax, indent, ftplugin)
- [less](https://github.com/groenewege/vim-less) (syntax, indent, ftplugin, ftdetect) - [less](https://github.com/groenewege/vim-less) (syntax, indent, ftplugin, ftdetect)
- [liquid](https://github.com/tpope/vim-liquid) (syntax, indent, ftplugin, ftdetect) - [liquid](https://github.com/tpope/vim-liquid) (syntax, indent, ftplugin, ftdetect)

View File

@ -0,0 +1,20 @@
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim ftdetect file
"
" Language: JSX (JavaScript)
" Maintainer: Max Wang <mxawng@gmail.com>
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
exec 'source '.fnameescape(expand('<sfile>:p:h:h').'/jsx-config.vim')
fu! <SID>EnableJSX()
if g:jsx_pragma_required && !b:jsx_pragma_found | return 0 | endif
if g:jsx_ext_required && !exists('b:jsx_ext_found') | return 0 | endif
return 1
endfu
autocmd BufNewFile,BufRead *.jsx let b:jsx_ext_found = 1
autocmd BufNewFile,BufRead *.jsx set filetype=javascript.jsx
autocmd BufNewFile,BufRead *.js
\ if <SID>EnableJSX() | set filetype=javascript.jsx | endif

17
after/ftplugin/jsx.vim Normal file
View File

@ -0,0 +1,17 @@
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim ftplugin file
"
" Language: JSX (JavaScript)
" Maintainer: Max Wang <mxawng@gmail.com>
" Depends: pangloss/vim-javascript
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" modified from html.vim
if exists("loaded_matchit")
let b:match_ignorecase = 0
let b:match_words = '(:),\[:\],{:},<:>,' .
\ '<\@<=\([^/][^ \t>]*\)[^>]*\%(>\|$\):<\@<=/\1>'
endif
setlocal suffixesadd+=.jsx

98
after/indent/jsx.vim Normal file
View File

@ -0,0 +1,98 @@
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim indent file
"
" Language: JSX (JavaScript)
" Maintainer: Max Wang <mxawng@gmail.com>
" Depends: pangloss/vim-javascript
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Do nothing if we don't find the @jsx pragma (and we care).
exec 'source '.fnameescape(expand('<sfile>:p:h:h').'/jsx-config.vim')
if g:jsx_pragma_required && !b:jsx_pragma_found | finish | endif
" Do nothing if we don't have the .jsx extension (and we care).
if g:jsx_ext_required && !exists('b:jsx_ext_found') | finish | endif
" Prologue; load in XML indentation.
if exists('b:did_indent')
let s:did_indent=b:did_indent
unlet b:did_indent
endif
exe 'runtime! indent/xml.vim'
if exists('s:did_indent')
let b:did_indent=s:did_indent
endif
setlocal indentexpr=GetJsxIndent()
" JS indentkeys
setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
" XML indentkeys
setlocal indentkeys+=*<Return>,<>>,<<>,/
" Self-closing tag regex.
let s:sctag = '^\s*\/>\s*;\='
" Get all syntax types at the beginning of a given line.
fu! SynSOL(lnum)
return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")')
endfu
" Get all syntax types at the end of a given line.
fu! SynEOL(lnum)
let lnum = prevnonblank(a:lnum)
let col = strlen(getline(lnum))
return map(synstack(lnum, col), 'synIDattr(v:val, "name")')
endfu
" Check if a syntax attribute is XMLish.
fu! SynAttrXMLish(synattr)
return a:synattr =~ "^xml" || a:synattr =~ "^jsx"
endfu
" Check if a synstack is XMLish (i.e., has an XMLish last attribute).
fu! SynXMLish(syns)
return SynAttrXMLish(get(a:syns, -1))
endfu
" Check if a synstack has any XMLish attribute.
fu! SynXMLishAny(syns)
for synattr in a:syns
if SynAttrXMLish(synattr)
return 1
endif
endfor
return 0
endfu
" Check if a synstack denotes the end of a JSX block.
fu! SynJSXBlockEnd(syns)
return get(a:syns, -1) == 'jsBraces' && SynAttrXMLish(get(a:syns, -2))
endfu
" Cleverly mix JS and XML indentation.
fu! GetJsxIndent()
let cursyn = SynSOL(v:lnum)
let prevsyn = SynEOL(v:lnum - 1)
" Use XML indenting if the syntax at the end of the previous line was either
" JSX or was the closing brace of a jsBlock whose parent syntax was JSX.
if (SynXMLish(prevsyn) || SynJSXBlockEnd(prevsyn)) && SynXMLishAny(cursyn)
let ind = XmlIndentGet(v:lnum, 0)
" Align '/>' with '<' for multiline self-closing tags.
if getline(v:lnum) =~? s:sctag
let ind = ind - &sw
endif
" Then correct the indentation of any JSX following '/>'.
if getline(v:lnum - 1) =~? s:sctag
let ind = ind + &sw
endif
else
let ind = GetJavascriptIndent()
endif
return ind
endfu

66
after/jsx-config.vim Normal file
View File

@ -0,0 +1,66 @@
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vimscript file
"
" Set up a bunch of configuration variables.
"
" Also check (if desired) whether or not the @jsx pragma is correctly included
" in '%'. Set the result in b:jsx_pragma_found.
"
" Language: JSX (JavaScript)
" Maintainer: Max Wang <mxawng@gmail.com>
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Only check once.
if exists('b:jsx_pragma_found')
finish
endif
" Whether the .jsx extension is required to enable JSX syntax/indent.
if !exists('g:jsx_ext_required')
let g:jsx_ext_required = 1
endif
" Whether the @jsx pragma is required to enable JSX syntax/indent.
if !exists('g:jsx_pragma_required')
let g:jsx_pragma_required = 0
endif
if !g:jsx_pragma_required | finish | endif
" Look for the @jsx pragma. It must be included in a docblock comment before
" anything else in the file (except whitespace).
let s:jsx_pragma_pattern = '\%^\_s*\/\*\*\%(\_.\%(\*\/\)\@!\)*@jsx\_.\{-}\*\/'
let b:jsx_pragma_found = search(s:jsx_pragma_pattern, 'npw')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vimscript file
"
" Set up a bunch of configuration variables.
"
" Also check (if desired) whether or not the @jsx pragma is correctly included
" in '%'. Set the result in b:jsx_pragma_found.
"
" Language: JSX (JavaScript)
" Maintainer: Max Wang <mxawng@gmail.com>
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Only check once.
if exists('b:jsx_pragma_found')
finish
endif
" Whether the .jsx extension is required to enable JSX syntax/indent.
if !exists('g:jsx_ext_required')
let g:jsx_ext_required = 1
endif
" Whether the @jsx pragma is required to enable JSX syntax/indent.
if !exists('g:jsx_pragma_required')
let g:jsx_pragma_required = 0
endif
if !g:jsx_pragma_required | finish | endif
" Look for the @jsx pragma. It must be included in a docblock comment before
" anything else in the file (except whitespace).
let s:jsx_pragma_pattern = '\%^\_s*\/\*\*\%(\_.\%(\*\/\)\@!\)*@jsx\_.\{-}\*\/'
let b:jsx_pragma_found = search(s:jsx_pragma_pattern, 'npw')

43
after/syntax/jsx.vim Normal file
View File

@ -0,0 +1,43 @@
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim syntax file
"
" Language: JSX (JavaScript)
" Maintainer: Max Wang <mxawng@gmail.com>
" Depends: pangloss/vim-javascript
"
" CREDITS: Inspired by Facebook.
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Do nothing if we don't find the @jsx pragma (and we care).
exec 'source '.fnameescape(expand('<sfile>:p:h:h').'/jsx-config.vim')
if g:jsx_pragma_required && !b:jsx_pragma_found | finish | endif
" Do nothing if we don't have the .jsx extension (and we care).
if g:jsx_ext_required && !exists('b:jsx_ext_found') | finish | endif
" Prologue; load in XML syntax.
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
unlet b:current_syntax
endif
syn include @XMLSyntax syntax/xml.vim
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
endif
" Highlight JSX regions as XML; recursively match.
syn region jsxRegion contains=@XMLSyntax,jsxRegion,jsBlock,javascriptBlock
\ start=+<\@<!<\z([a-zA-Z][a-zA-Z0-9:\-.]*\)+
\ skip=+<!--\_.\{-}-->+
\ end=+</\z1\_\s\{-}>+
\ end=+/>+
\ keepend
\ extend
" JSX attributes should color as JS. Note the trivial end pattern; we let
" jsBlock take care of ending the region.
syn region xmlString contained start=+{+ end=++ contains=jsBlock,javascriptBlock
" Add jsxRegion to the lowest-level JS syntax cluster.
syn cluster jsExpression add=jsxRegion

2
build
View File

@ -4,6 +4,7 @@ set -E
DIRS="syntax indent compiler autoload ftplugin ftdetect after/syntax after/indent after/ftplugin after/ftdetect" DIRS="syntax indent compiler autoload ftplugin ftdetect after/syntax after/indent after/ftplugin after/ftdetect"
DIRS_BASIC="syntax indent ftdetect after/syntax after/indent after/ftdetect" DIRS_BASIC="syntax indent ftdetect after/syntax after/indent after/ftdetect"
DIRS_JSX="after"
OUTPUT="" OUTPUT=""
@ -119,6 +120,7 @@ PACKS="
julia:dcjones/julia-minimalist-vim julia:dcjones/julia-minimalist-vim
json:sheerun/vim-json json:sheerun/vim-json
jst:briancollins/vim-jst jst:briancollins/vim-jst
jsx:mxw/vim-jsx:_JSX
latex:LaTeX-Box-Team/LaTeX-Box latex:LaTeX-Box-Team/LaTeX-Box
less:groenewege/vim-less less:groenewege/vim-less
liquid:tpope/vim-liquid liquid:tpope/vim-liquid