vim-prettier icon indicating copy to clipboard operation
vim-prettier copied to clipboard

Run Prettier only if project contains prettier configuration

Open charlyx opened this issue 5 years ago • 18 comments

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

feature

What is the current/expected behavior?

Currently Prettier is always called whether the project you're working on is using Prettier or not. I'd like to be able to run prettier only if the project is configured to use Prettier. This behaviour could be enabled using a variable.

What do you think?

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

0.2.7

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

Prettier: no prettier executable installation found.

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

Prettier: no prettier executable installation found.

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

I don't know.

charlyx avatar Apr 02 '19 08:04 charlyx

Hi @charlyx ,

How did you install vim-prettier ? If you installed using vim-plug you should still be able to use the prettier executable even if your project does not have prettier installed.

Otherwise you can always do the following:

  1. go to vim prettier installation directory ~/.vim/*/vim-prettier/
  2. install deps with either npm install or yarn install

Please note that depending on your OS the installation of vim-prettier may be in different directories.

mitermayer avatar Apr 03 '19 05:04 mitermayer

Hi @mitermayer, thanks for your answer. I don't have any problem running prettier. I'm asking for a feature I believe does not exist.

My problem is that I would like to not run prettier on every project. I would like to be able to run it only if my project is configured for using prettier. This is what I described in my issue:

Currently Prettier is always called whether the project you're working on is using Prettier or not. I'd like to be able to run prettier only if the project is configured to use Prettier. This behaviour could be enabled using a variable.

I hope my message makes it clearer.

Do you have any idea on the subject though?

charlyx avatar Apr 04 '19 09:04 charlyx

That sounds like a reasonable feature request I can work on it

mitermayer avatar Apr 05 '19 07:04 mitermayer

@mitermayer did you have a chance to work on it? If not, I'd be happy to make a PR. Do you have any guidelines to get me started?

charlyx avatar Apr 14 '19 19:04 charlyx

Hi @charlyx ,

Sorry been super busy lately with work! A PR would be super welcome! If you do a PR to release/1.x branch it will be a lot easier to merge as development for master branch are mostly to bugfixes. I probably need to work on vim-prettier for a day or two to finalize the release

mitermayer avatar Apr 15 '19 06:04 mitermayer

I would also love this feature.

Just remember that not all projects have a prettier configuration file. Some use the default prettier configuration. Perhaps vim-prettier could run only if prettier is named as a direct dependency in package.json.

openjck avatar Jun 20 '19 18:06 openjck

In the meantime, I solved this by adding this to my init.vim:

let g:prettier#autoformat = 0
if filereadable(findfile('.prettierrc.js', '.;'))
  echo "Using prettier..."
  autocmd BufWritePre *.js,*.jsx,*mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql,*.md,*.vue,*.yaml,*.html PrettierAsync
endif

This implementation only searches parent directories for .prettierrc.js but could be easily expanded to check for other naming variations as well.

atomdmac avatar Sep 11 '19 11:09 atomdmac

FYI: I have a branch that has a new prettier#autoformat_config_present setting that will toggle auto-formatting based on whether a Prettier config can be found.

However, I haven't submitted a PR yet because I haven't been able to get this to work under master; only under release/1.x. According the standard PR submission form, new features should be based off of master.

@mitermayer Do you have any guidance on how to proceed here? Maybe I should wait until the 1.x branch has been fully merged?

atomdmac avatar Oct 09 '19 19:10 atomdmac

Update: I've opened a formal PR here. There are still a few loose ends to tie up but feel free to comment.

atomdmac avatar Oct 13 '19 14:10 atomdmac

I will aim to look into this sometime this week or next weekend, been very busy at work but I really want to release 1.x soon!

There are some bugs that i want to fix and some "todo" that i want to clean, but overall is very stable

mitermayer avatar Oct 14 '19 04:10 mitermayer

👋 Just checking in - I know the holidays are coming up so no pressure if you're still tight on time. I've been using this change pretty regularly over the last month or so and have no major issues to report..

atomdmac avatar Nov 24 '19 05:11 atomdmac

Hi @atomdmac, will try to get on this in the holidays! sorry trying to wrap up end of the year goals at work

mitermayer avatar Nov 25 '19 04:11 mitermayer

This issue has been fixed already by @atomdmac

mitermayer avatar Feb 13 '20 11:02 mitermayer

@mitermayer

I updated my plugin and Im still having this issue. Am I missing something?

gWOLF3 avatar Apr 17 '20 01:04 gWOLF3

Will reopen this issue for further investigation

mitermayer avatar Apr 17 '20 03:04 mitermayer

@gWOLF3 Do you have any settings in your vimrc that pertain to this plugin? I'm wondering if maybe it could just be a configuration issue.

atomdmac avatar Apr 18 '20 15:04 atomdmac

I had the same issue today as @gWOLF3 . Following @mitermayer fixed it.

  1. go to vim prettier installation directory ~/.vim/*/vim-prettier/
  2. install deps with either npm install or yarn install

About vim-prettier I have nothing special in my .vimrc, only Plug 'prettier/vim-prettier', { 'do': 'npm install' }. (I didn't installed yarn) VIM v8.1, Catalina 10.15.4.

Great work, great plugin. Thanks all for your time.

dimitri-bourreau avatar May 30 '20 07:05 dimitri-bourreau

It could also be nice to have the feature to only format if Prettier is in your dependencies.

wylie39 avatar Feb 28 '21 15:02 wylie39