plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Add option to automatically ignore files with inline HTML

Open cbirdsong opened this issue 2 years ago • 6 comments

Would it be practical to add an option to automatically ignore files that contain inline HTML? I usually do this via .prettierignore so I can use prettier on non-templating files, but it’s not possible to do that on many legacy projects.

cbirdsong avatar Feb 17 '22 13:02 cbirdsong

Interesting idea! In the main codebase that I'm using the plugin on, I was lucky enough that all the template files have a different file extension (.phtml) - which made it easy to ignore them. But I can imagine that's not always the case. I'm not sure if it's possible to implement this purely at the plugin level, or if it needs a new API on prettier itself. cc @alexander-akait @cseufert

czosel avatar Feb 18 '22 13:02 czosel

In theory we can return just string and do nothing if file contains HTML, but that sounds like trying to fix another problem, I'm not a fan of such solutions.

@cbirdsong Can you provide more information why you want to ignore?

alexander-akait avatar Feb 18 '22 13:02 alexander-akait

I work a lot on Wordpress themes, and on legacy projects I'll often encounter something built with the standard Wordpress approach of interspersing a lot of PHP logic in and around your HTML. Here is a prettier-php playground link with the site header chunk from _s, the official Wordpress starter theme. In this case, Prettier only messes up the indenting because those really long lines that contain functions, but in other situations I've had it add extra ?> close tags. (Don't have one handy, unfortunately.)

My usual workaround for this is adding folders with templating files to .prettierignore, but that's not always practical with the (lack of) structure in some of these projects.

cbirdsong avatar Feb 24 '22 17:02 cbirdsong

To me this sounds like a valid requirement. It would certainly better if we’d be able to format mixtures of PHP and HTML better, but that’s a tough problem. @alexander-akait is it possible for us to just return the input without changes in the prettier plugin API though? If I’m not mistaken, prettier plugins always have to return a prettier doc data structure.

czosel avatar Feb 24 '22 19:02 czosel

We can return doc with string and nothing do with string inside, but we really need to focus on HTML formatting, maybe revisit AST

alexander-akait avatar Feb 24 '22 19:02 alexander-akait

Does anyone know how the VueJS Prettier plugin works? as it seems to be able to switch from HTML to JS, and CSS/SCSS on the fly within the one file, it would be amazing if we could achieve something similar with the PHP plugin.

cseufert avatar Feb 27 '22 23:02 cseufert