From 1c80c4bb08ae250d2eb19aabe0f31c1b2bdf5097 Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Sat, 18 Jul 2015 22:50:09 +0200 Subject: Add jsx support, closes #56 --- README.md | 1 + after/ftdetect/javascript.vim | 20 +++++++++ after/ftplugin/jsx.vim | 17 ++++++++ after/indent/jsx.vim | 98 +++++++++++++++++++++++++++++++++++++++++++ after/jsx-config.vim | 66 +++++++++++++++++++++++++++++ after/syntax/jsx.vim | 43 +++++++++++++++++++ build | 2 + 7 files changed, 247 insertions(+) create mode 100644 after/ftdetect/javascript.vim create mode 100644 after/ftplugin/jsx.vim create mode 100644 after/indent/jsx.vim create mode 100644 after/jsx-config.vim create mode 100644 after/syntax/jsx.vim diff --git a/README.md b/README.md index cc84c834..9b1e7b6d 100644 --- a/README.md +++ b/README.md @@ -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) - [json](https://github.com/sheerun/vim-json) (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) - [less](https://github.com/groenewege/vim-less) (syntax, indent, ftplugin, ftdetect) - [liquid](https://github.com/tpope/vim-liquid) (syntax, indent, ftplugin, ftdetect) diff --git a/after/ftdetect/javascript.vim b/after/ftdetect/javascript.vim new file mode 100644 index 00000000..900fce11 --- /dev/null +++ b/after/ftdetect/javascript.vim @@ -0,0 +1,20 @@ +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" Vim ftdetect file +" +" Language: JSX (JavaScript) +" Maintainer: Max Wang +" +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +exec 'source '.fnameescape(expand(':p:h:h').'/jsx-config.vim') + +fu! 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 EnableJSX() | set filetype=javascript.jsx | endif diff --git a/after/ftplugin/jsx.vim b/after/ftplugin/jsx.vim new file mode 100644 index 00000000..ee642090 --- /dev/null +++ b/after/ftplugin/jsx.vim @@ -0,0 +1,17 @@ +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" Vim ftplugin file +" +" Language: JSX (JavaScript) +" Maintainer: Max Wang +" 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 diff --git a/after/indent/jsx.vim b/after/indent/jsx.vim new file mode 100644 index 00000000..d0b4d4ef --- /dev/null +++ b/after/indent/jsx.vim @@ -0,0 +1,98 @@ +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" Vim indent file +" +" Language: JSX (JavaScript) +" Maintainer: Max Wang +" Depends: pangloss/vim-javascript +" +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +" Do nothing if we don't find the @jsx pragma (and we care). +exec 'source '.fnameescape(expand(':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+=*,<>>,<<>,/ + +" 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 diff --git a/after/jsx-config.vim b/after/jsx-config.vim new file mode 100644 index 00000000..5270a518 --- /dev/null +++ b/after/jsx-config.vim @@ -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 +" +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +" 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 +" +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" + +" 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') diff --git a/after/syntax/jsx.vim b/after/syntax/jsx.vim new file mode 100644 index 00000000..63a1248f --- /dev/null +++ b/after/syntax/jsx.vim @@ -0,0 +1,43 @@ +""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" Vim syntax file +" +" Language: JSX (JavaScript) +" Maintainer: Max Wang +" 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(':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=+<\@+ + \ end=++ + \ 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 diff --git a/build b/build index bd3980b4..065af518 100755 --- a/build +++ b/build @@ -4,6 +4,7 @@ set -E 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_JSX="after" OUTPUT="" @@ -119,6 +120,7 @@ PACKS=" julia:dcjones/julia-minimalist-vim json:sheerun/vim-json jst:briancollins/vim-jst + jsx:mxw/vim-jsx:_JSX latex:LaTeX-Box-Team/LaTeX-Box less:groenewege/vim-less liquid:tpope/vim-liquid -- cgit v1.2.3