prettier/vim-prettier

Support custom prettier plugins

andreiglingeanu opened this issue ยท 12 comments

Currently, there a couple of prettier plugins that will offer consistent formatting for other languages. I tried prettier-php these days, it worked smooth from CLI but I was not able to get the plugin to work properly with this plugin, it just didn't passed a proper parser option to the prettier CLI command, it was probably just my bad knowledge of VimL...

Is there a plan for getting a smooth vim-prettier experience with custom plugins?

Thanks a lot for the awesome work!

Hi @andreiglingeanu,

This is denifitly something we want to have integrated in vim-prettier. I haven't had a look into it just yet but will do as soon as I can.

In the meantime also accepting PR's for it.

Integration should be straight forward since vim-prettier is a simple wrapper around prettier itself therefore all things that can be done through the CLI should be possible to be integrated on vim-prettier.

Feel free to submit a PR for it otherwise will try to look into it during the weekend

Sounds great! I'll try to have a look but I'm not 100% sure I'll be able to handle it, thanks for a quick heads up, though!

Getting back to it, I found a solution. It turns out that vim-prettier defaults to flow parser when it does not recognize the type of the buffer it is in.

How I got plugin-php to work:

  1. git clone git@github.com:prettier/plugin-php.git ~/code/plugin-php
  2. add to ~/.prettierrc.js: plugins: ["/Users/username/code/plugin-php"]
  3. At this point, we are ready to use the new parser and we need to instruct vim-prettier by defining the b:prettier_ft_default_args variable for that specific file type. I show how to do it below:
autocmd FileType php let b:prettier_ft_default_args = {
  \ 'parser': 'php',
  \ }

As for integrating it into the plugin, we could take a two approaches:

  1. Provide b:prettier_ft_default_args for all known plugins, that is for swift, php and python
  2. Just point out in README an example of how to define the b:prettier_ft_default_args with the newly installed prettier parser

I tend to think that the second option is more than enough.

Hi @andreiglingeanu,

Thanks for looking into this! We could also add to vim-prettier file package.json the dependencies for 'php', 'python' and 'swift' prettier plugins there (assuming they are plain javascript modules). That way in theory it should work out of the box if using the prettier exec that comes with vim-prettier when the user does not have the other plugins installed.

I think it make sense to create a file type detect and a file type plugin for 'swift', 'php' and 'python' and define a default parser for them in there like you describe on '1'. Users can always overwrite that behaviour with a .prettierrc configuration file

I've opened a PR with some progress towards this. Could use some help and feedback. Thanks!

Once I get a beta version of vim-prettier 1.0 will try including this PR in there and see if I can debug a bit more on what the failures were

Link to PR?

@docwhat, thats the link to the PR #120, we may need to update it in order to be mergeable with v1.0 now thats all refactored

I will have some time to keep working on this during the weekend. Will be porting the open PR to branch 1.x as this is one of the last major tasks for 1.x release

I will try to merge this week ! Finally have sometime to work on this

This has been merged on #155 on release/1.x branch

@mitermayer tested it, works nicely! Thanks for that!

But there is a small glitch anyway, when --use-tabs is present in the final command, it gets inserted without a space between prettier bin & other args, like this:

                                                    here we need a space
----------------------------------------------------^^^-------
~/.vim/bundle/vim-prettier/node_modules/.bin/prettier--use-tabs --tab-width=4  --parser=php   --semi=true --single-quote=false --bracket-spacing=true --jsx-brac
ket-same-line=false --arrow-parens=avoid --trailing-comma=none --config-precedence=prefer-file --prose-wrap=preserve --stdin-filepath=/private/tmp/test.php --loglevel error

This easily gets fixed by #158