summaryrefslogtreecommitdiffstats
path: root/ftdetect/polyglot.vim
diff options
context:
space:
mode:
Diffstat (limited to 'ftdetect/polyglot.vim')
-rw-r--r--ftdetect/polyglot.vim34
1 files changed, 22 insertions, 12 deletions
diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim
index a996ad42..5dc3b474 100644
--- a/ftdetect/polyglot.vim
+++ b/ftdetect/polyglot.vim
@@ -568,7 +568,7 @@ endif
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'jsx') == -1
augroup filetypedetect
- " jsx, from javascript.vim in mxw/vim-jsx:_ALL
+ " jsx, from javascript.vim in amadeus/vim-jsx
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vim ftdetect file
"
@@ -577,20 +577,27 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'jsx') == -1
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
-" Whether the .jsx extension is required.
-if !exists('g:jsx_ext_required')
- let g:jsx_ext_required = 0
-endif
-
-" Whether the @jsx pragma is required.
-if !exists('g:jsx_pragma_required')
- let g:jsx_pragma_required = 0
-endif
-
let s:jsx_pragma_pattern = '\%^\_s*\/\*\*\%(\_.\%(\*\/\)\@!\)*@jsx\_.\{-}\*\/'
+" if g:jsx_check_react_import == 1
+" parse the first line of js file (skipping comments). When it has a 'react'
+" importation, we guess the user writes jsx
+" endif
+let s:jsx_prevalent_pattern =
+ \ '\v\C%^\_s*%(%(//.*\_$|/\*\_.{-}\*/)@>\_s*)*%(import\s+\k+\s+from\s+|%(\l+\s+)=\k+\s*\=\s*require\s*\(\s*)[`"'']react>'
+
" Whether to set the JSX filetype on *.js files.
fu! <SID>EnableJSX()
+ " Whether the .jsx extension is required.
+ if !exists('g:jsx_ext_required')
+ let g:jsx_ext_required = 0
+ endif
+
+ " Whether the @jsx pragma is required.
+ if !exists('g:jsx_pragma_required')
+ let g:jsx_pragma_required = 0
+ endif
+
if g:jsx_pragma_required && !exists('b:jsx_ext_found')
" Look for the @jsx pragma. It must be included in a docblock comment
" before anything else in the file (except whitespace).
@@ -598,7 +605,10 @@ fu! <SID>EnableJSX()
endif
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
+ if g:jsx_ext_required && !exists('b:jsx_ext_found') &&
+ \ !(get(g:,'jsx_check_react_import') && search(s:jsx_prevalent_pattern, 'nw'))
+ return 0
+ endif
return 1
endfu