Skip to content

Conversation

@agriffis
Copy link
Contributor

@agriffis agriffis commented Jan 29, 2020

If npx is available, use it for running prettier. This will find a project-local prettier, if available, which allows everybody on the team to run the same version. Otherwise people have different prettier versions, and code formatting toggles back and forth between commits.

Since npx can't tell us if prettier is available, we check with npx prettier --version and cache the result.

Also, since by default npx will do a temporary install of things you try to run, we use the --no-install flag to avoid that. We only want npx to find what's already there.

Along the way, kill the opinionated prettier options that were in vim-codefmt, since they unhelpfully override local and project config files.

@agriffis
Copy link
Contributor Author

@lhchavez @infokiller Looks like you've previously worked on prettier in vim-codefmt. I'd be interested in your thoughts on this.

@infokiller
Copy link
Contributor

The general idea makes sense and the code looks clean.
Why did you remove the opinionated prettier options that were not related to textwidth?

@agriffis
Copy link
Contributor Author

@infokiller Thanks for looking it over.

Regarding the opinionated options, I think they don't belong in vim-codefmt. Prettier already supplies defaults. The role of vim-codefmt is to bridge between Vim and the formatter, not to introduce a new set of defaults. Principle of least surprise.

However I think it makes sense, within reason, to propagate editor settings to formatters. As a user, I don't expect a formatter to run contrary to my Vim settings. For example, if I always write code at 100 columns width, I would like formatters to respect that instead of needing to configure them individually.

One of the nice outcomes is that it works with editorconfig. Per-project settings propagate seamlessly to formatters.

In fact, it would probably make sense for the prettier plugin to additionally respect shiftwidth, but that doesn't apply to zprint which is where I first implemented this concept. I might update this PR to add that.

Possibly this default policy should be settable by users. Perhaps there should be a global vim-codefmt setting that controls whether to inform formatters about Vim settings, so that users can easily change the policy in one place. What do you think?

@infokiller
Copy link
Contributor

Thanks for the great explanation. Sounds good to me, and I personally would also prefer vim to pass its settings to the formatters.
Regarding the opinionated prettier settings, I think they were introduced to make it behave more like clang-format, which IIRC was originally the only formatter for javascript in vim-codefmt.

@dbarnett
Copy link
Contributor

One note on inheriting formatting settings: it's not always an uncontroversial improvement to treat the buffer's style settings as the correct ones. We do see plenty of cases where repo owners configure opinionated local project style settings like .clang-format and that's supposed to trump textwidth, shiftwidth, etc., which devs sadly tend to ignore and expect things to "just work" when they save the file. There are also plugins like https://github.com/tpope/vim-sleuth that configure those settings with guesses and don't pay attention to local project configs like that. It would be great if something like editorconfig could clean up this mess for us, but do tread carefully on trying to change that behavior.

@agriffis
Copy link
Contributor Author

agriffis commented Jan 30, 2020

@infokiller @dbarnett Thanks to both of you for your thoughtful comments.

I just pushed a commit that refactors things and adds a new codefmt setting apply_vim_settings.

The idea here is that vim-codefmt can optionally apply buffer settings to formatters across the board, and it's easy for the user to disable this behavior globally for vim-codefmt.

I updated both the prettier and zprint plugins as a demonstration of using this, although zprint is a somewhat confusing example because it doesn't take normal command-line options, and I wanted apply_vim_settings to work reasonably even if zprint_options is also customized.

If you like where this is going, then I'll update the tests and also rebase into a few sensible commits rather than the existing stream-of-development.

@agriffis
Copy link
Contributor Author

agriffis commented Jan 30, 2020

Also, I made apply_vim_settings true by default because I think that makes sense out of the box, but making it false would be more conservative.

@agriffis agriffis changed the title feat: project-local prettier with npx npx prettier and apply_vim_settings Feb 10, 2020
@agriffis
Copy link
Contributor Author

agriffis commented Feb 10, 2020

@dbarnett This is ready for proper review and pull when you're ready...

I rebased into four atomic commits, with tests passing on each commit. This PR now does two things:

  1. Use the project-local prettier via npx if available.

  2. Add the apply_vim_settings flag and use it in the prettier and zprint plugins. It defaults to zero, though, so nobody gets unpleasantly surprised. The downside is that people have to discover the flag, but that's the tradeoff.

I could do this as separate pull requests, but they'd be sequential anyway, so I settled for atomic commits. Let me know what you think.

@dbarnett
Copy link
Contributor

Sorry for the code review bomb... On the overall direction, I do like being able to hook the vim settings into the formatter's settings better, but I still have reservations about setting up a global apply_vim_settings flag that only "mostly" solves the cases I run into day-to-day.

I like the flag helper. Would you be up for sending a quick PR to add that first and streamline the scope of this PR?

@agriffis
Copy link
Contributor Author

Thanks @dbarnett. Busy at the moment but I'll get back to this soon

@agriffis agriffis changed the title npx prettier and apply_vim_settings npx prettier Feb 25, 2020
@agriffis
Copy link
Contributor Author

@dbarnett I knocked this down to purely npx prettier and killing prettier options, as two separate commits.

All the stuff related to apply_vim_settings I'll move to a separate PR for reworking.

@agriffis
Copy link
Contributor Author

@dbarnett Let me know if you have any more questions about this

Copy link
Contributor

@dbarnett dbarnett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run vimdoc to update the generated help file with your changes?

@dbarnett
Copy link
Contributor

PR description looks stale. Can you update it to clarify the latest state of the PR?

Prettier has its own defaults which can be overridden by user's
.prettierrc and project-local package.json as well. Don't impose a new
and conflicting set of defaults in vim-codefmt.
If npx is available, use it in preference to calling prettier directly.
This enables project-local prettier, which is important for teams to use
matching versions.

This doesn't change anything for non-project prettier installs, because
npx will fall back to global prettier.

Since the existence of npx doesn't actually imply the existence of
prettier, we have to call it once to find out if it's usable. Since this
is expensive (compared to not calling it, anyway), cache the result as
s:prettier_is_available and invalidate if prettier_executable changes.
@agriffis
Copy link
Contributor Author

Can you run vimdoc to update the generated help file with your changes?

Done

PR description looks stale. Can you update it to clarify the latest state of the PR?

Done

@agriffis agriffis changed the title npx prettier feat(prettier): use npx for project-local prettier Feb 26, 2020
@dbarnett dbarnett merged commit 6d69f93 into google:master Feb 26, 2020
@dbarnett
Copy link
Contributor

Merged! Thanks for bearing with me on this one. =)

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants