prettier / vim-prettier

A Vim plugin for Prettier
MIT License
1.78k stars 138 forks source link

Request: check for filetype javascript (not just the .js and .jsx extensions) #340

Open coolaj86 opened 1 year ago

coolaj86 commented 1 year ago

Easier to explain in code

See https://github.com/prettier/vim-prettier/pull/341/files?diff=unified&w=1.

Feature Request

Do you want to request a feature or report a bug?

Requesting two related features:

What is the current/expected behavior?

Presently must end in .js or .jsx in order to match for formatting.

1. Respect existing filetype

I have a very minimal config and I believe that vim-ale is already setting filetype to javascript - such as when the shebang looks like #!/usr/bin/env node or #!/usr/bin/node --debug, etc.

Update: It seems that adding this line to my ~/.vimrc causes this to function as intended:

autocmd FileType javascript,typescript autocmd BufWritePre <buffer> PrettierAsync

2. Set filetype to javascript for js shebangs

Something (possibly vim-ale?) already sets filetype to javascript when the shebang matches node, such as:

But there are a number of other popular js runtimes that should be detected as JS as well, such as:

When I add this line to my ~/.vimrc I almost get the expected behavior. It then works as described for deno and zx... but curiously not for bun.

" set filetype to javascript by shebang
autocmd BufRead,BufNewFile * if getline(1) =~ '#!/.*\<\(bun\|deno\|zx\)\>' | setfiletype javascript | endif

What version of vim-prettier are you using - (output of :PrettierVersion) ?

1.0.0-beta

What version of prettier are you using - (output of :PrettierCliVersion) ?

3.8.4

What is your prettier executable path - (output of :PrettierCliPath) ?

~/.local/opt/node/bin/prettier

Did this work in previous versions of vim-prettier and/or prettier ?

No.

coolaj86 commented 1 year ago

I think vim-ale may be setting the filetype to javascript based on shebang already.

I put this in my local vim-prettier config at ~/.vim/plugins/prettier.vim (which is sourced from ~/.vimrc):

augroup RunPrettierByFiletype
    " run if the filetype is detected as javascript or typescript
    autocmd FileType javascript,typescript autocmd BufWritePre <buffer> PrettierAsync
augroup END

Now prettier runs when shebangs for node, deno, or zx, but strangely not bun.

I'm not sure where to look to debug that. I searched in ~/.vim/ for deno as I thought that would be the most unique, but I can't seem to find anywhere that it does shebang detection for it.