-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
detect on common react patterns #130
base: master
Are you sure you want to change the base?
Conversation
Nice idea! I agree though, I know I personally would appreciate this change even more if it could be disabled via an option. While 99% of users importing from |
Right; this should be an option, and should also I think skip comments in addition to whitespace. |
Could you add an option to enable/disable this check? |
ftdetect/javascript.vim
Outdated
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 | ||
\ if <SID>EnableJSX() | set filetype=javascript.jsx | endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undo this change?
ftdetect/javascript.vim
Outdated
" Whether to set the JSX filetype on *.js files. | ||
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 | ||
if g:jsx_ext_required && !exists('b:jsx_ext_found') && | ||
\ !(get(g:,'jsx_prevalent_declaration') && search(s:jsx_prevalent_pattern, 'nw')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this into a separate conditional? We could smush all three checks into one, but they're unrelated and I think it's clearer to separate them (and also probably avoids a line continuation).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that condition is a pre-req. If it was rearranged, it'd be dead code because of the return
ftdetect/javascript.vim
Outdated
" Whether to set the JSX filetype on *.js files. | ||
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 | ||
if g:jsx_ext_required && !exists('b:jsx_ext_found') && | ||
\ !(get(g:,'jsx_prevalent_declaration') && search(s:jsx_prevalent_pattern, 'nw')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a little strange to identify the declaration as "the prevalent declaration" rather than "the react import" or "the require react expression". Rename this variable to something like g:jsx_check_react_import
or something more self-documenting.
ftdetect/javascript.vim
Outdated
" importation, we guess the user writes jsx | ||
" endif | ||
let s:jsx_prevalent_pattern = | ||
\ '\v\C%^\_s*%(%(//.*\_$|/\*\_.{-1,}\*/)@>\_s*)*%(import\s+\k+\s+from\s+|%(\l+\s+)=\k+\s*\=\s*require\s*\(\s*)[`"'']react>' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm—this regex skips over comments and looks for something that looks like import ... from ... 'react
or require( ... 'react
(modulo whitespace, choice of quote, etc.). Is that correct, or have I missed something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's it. i did this with the \v
flag to reduce the size. pretty fast, and accurate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the first step in a sed js parser haha
" Whether to set the JSX filetype on *.js files. | ||
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 | ||
if g:jsx_ext_required && !exists('b:jsx_ext_found') && | ||
\ !(get(g:,'jsx_check_react_import') && search(s:jsx_prevalent_pattern, 'nw')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's right—I forgot this function was setting the filetype on .js
files only, not files in general.
Can you rebase this on master? I'm having trouble upstreaming the change. |
what about github.com ? says it is clean and mergeable |
should be fine now |
This uses the vim-jsx plugin to add JSX filetype support, also by setting g:jsx_ext_required to zero it enables JSX in all *.js files. It's not ideal but hopefully the plugin's ftdetect will get better at recognizing React patterns (i.e. when [1] gets merged). [1]: mxw/vim-jsx#130
maybe it should be an option though