Skip to content
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

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

Open
2 tasks
coolaj86 opened this issue Mar 6, 2023 · 1 comment
Open
2 tasks

Comments

@coolaj86
Copy link
Contributor

coolaj86 commented Mar 6, 2023

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:

  • Respect existing filetype, if any
  • Set filetype to javascript if shebang is bun, deno, node, or zx

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:

  • #!/usr/bin/env node
  • #!/usr/local/bin/node --debug

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

  • #!/usr/bin/env bun
  • #!/usr/bin/env zx
  • #!/usr/bin/env deno

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
Copy link
Contributor Author

coolaj86 commented Mar 6, 2023

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.

@coolaj86 coolaj86 changed the title Request: check shebang when setting filetype (or how to set local config such) Request: check for filetype javascript (not just the .js and .jsx extensions) Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant