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

Support custom prettier plugins #119

Closed
andreiglingeanu opened this issue Mar 5, 2018 · 12 comments
Closed

Support custom prettier plugins #119

andreiglingeanu opened this issue Mar 5, 2018 · 12 comments

Comments

@andreiglingeanu
Copy link
Contributor

andreiglingeanu commented Mar 5, 2018

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!

@mitermayer
Copy link
Member

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

@andreiglingeanu
Copy link
Contributor Author

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!

@andreiglingeanu
Copy link
Contributor Author

andreiglingeanu commented Mar 9, 2018

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.

@mitermayer
Copy link
Member

mitermayer commented Mar 9, 2018

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

@andreiglingeanu
Copy link
Contributor Author

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

@mitermayer
Copy link
Member

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

@docwhat
Copy link
Contributor

docwhat commented May 4, 2018

Link to PR?

@mitermayer
Copy link
Member

@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

@mitermayer mitermayer self-assigned this May 25, 2018
@mitermayer
Copy link
Member

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

@mitermayer
Copy link
Member

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

@mitermayer
Copy link
Member

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

@andreiglingeanu
Copy link
Contributor Author

andreiglingeanu commented Nov 6, 2018

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants