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 formatting of HTML files #107

Closed
funfried opened this issue Mar 10, 2020 · 14 comments
Closed

Support formatting of HTML files #107

funfried opened this issue Mar 10, 2020 · 14 comments

Comments

@funfried
Copy link
Owner

No description provided.

@funfried
Copy link
Owner Author

So far I could not find a promising Java implementation of a HTML formatter, please let me know if someone knows any.

@Chris2011
Copy link

Chris2011 commented Sep 28, 2022

I wouldn't recommend to find one based on Java. Sure maybe there is one but for HTML, JS, CSS, Less, Sass, etc. there are node modules due to relies on it and it is a common web tech stack. So for formatting those files, there is a good one called prettier https://prettier.io/. If you search for prettier java you will find not that what you want, just a plugin for formatting Java code, but not written in java. So I would prefer to check whether it is possible with your plugin to implement this via node or not. I still have a plan to integrate prettier as a plugin for the web tech but lack of time, not soon unfortunately.

@Chris2011
Copy link

I had a look into the repo of spotless and in the readme there is this:
Spotless can format <antlr | c | c# | c++ | css | flow | graphql | groovy | html | java | javascript | json | jsx | kotlin | less | license headers | markdown | objective-c | protobuf | python | scala | scss | sql | typeScript | vue | yaml | anything> using <gradle | maven | anything>.

Isn't this looks promising for what you wanted?

@funfried
Copy link
Owner Author

funfried commented Oct 1, 2022

Hi @Chris2011 and thanks for your thoughts.

Actually I had a look in Spotless quite some time ago (already using the DBeaver SQL Formatter from there) and didn't really understand how they support neither HTML nor CSS, therefore I decided to do it like they guys from the formatter-maven-plugin did with JSoup. But regarding CSS I decided to have another look into Spotless yesterday and saw in their _ext subfolder that they used the Eclipse CSS Formatter for that which is also quite interesting, I guess I'll have another look into that and see what I can do.

Regarding prettier.io: I also looked into it and hoped I would find a Java-based implementation for it or at least some kind of wrapper, but I didn't and so I'm not sure if I really want to execute an external binary (npm) to format source code, it would be an option, but still I would need to check how well I can integrate it into the IDE. I might give it another shot though, but still I prefer the Java way, and - as said - Spotless seems very promising, maybe for even more languages.

Cheers,
Fabian

@Chris2011
Copy link

Yes, you are right, spotless seems nice to have. Also as I said I will try prettier implementation sooner or later. It should be doable. There is also a plugin for eslint, finding bugs/stuff in js files so there is no problem to use an external CLI tool and create a wrapper with Java.

@funfried
Copy link
Owner Author

funfried commented Oct 2, 2022

That's true, but I prefer the plain Java way 😉
But sure, it could be an additional plugin to extend the External Formatter Plugin with npm formatters, in that case the user could decide whether he/she wants to install Node.js and use those formatters and if not, just do not install the extension plugin.
I'll have a look, if I can use the npm binary from the NetBeans HTML/JS "Node.js" preferences panel, that would be great as it would feel a lot better integrated than providing my own npm binary path setting.

@Chris2011
Copy link

I just use ExternalExecuter and check the option from NetBeans node as far as I can remember. You can check this plugin: https://github.com/joakim-eriksson/nb-eslint or this one: https://github.com/Chris2011/NBAngularCLI/tree/develop

@funfried
Copy link
Owner Author

@Chris2011 I just had a closer look into spotless and prettier.

Regarding spotless I wasn't able to use their external libs based on the Eclipse formatters for CSS and HTML as it seems the Eclipse stuff needs some OSGi classes which I don't have.

Regarding prettier.io: I could not find a way to call prettier with some string and get a string in return, it seems I can just call prettier with a file and it directly modifies the file then, but I neither like the idea of creating a temporary file every time I want to format code nor to save the changes of a file before calling prettier. Do you have an idea how I could manage that? I mean there seem to be integrations for IntelliJ as well as for VS Code and I don't think they will save the files before formatting as well, but still, I couldn't find anything which looks promising there.

@Chris2011
Copy link

Chris2011 commented Oct 17, 2022

Thx for your deeper look, lemme check later how VS Code does this :).

You can try to create an in memory file with the content of the original file, take the formatted content and remove the in memory file. Or is this what you said by creating a temp file?

@Chris2011
Copy link

I also had a look into the prettier CLI. There is the range property: https://prettier.io/docs/en/options.html#range So if you don't want to create an in memory file you can take the text of the JEditor from offset 0 to length of file and take this instead of a file. But adding this instead of using a file can also lead to problems. I guess. So we have 2 options:

  • Create an in memory file and take this path (if possible) to format this file and rewrite the content of the in memory file to the original one
  • Take range from 0 to end of file and send it to the Prettier CLI

@funfried
Copy link
Owner Author

Thx for your deeper look, lemme check later how VS Code does this :).

You can try to create an in memory file with the content of the original file, take the formatted content and remove the in memory file. Or is this what you said by creating a temp file?

Yes, that's more or less what I meant with the temp file

@funfried
Copy link
Owner Author

I also had a look into the prettier CLI. There is the range property: https://prettier.io/docs/en/options.html#range So if you don't want to create an in memory file you can take the text of the JEditor from offset 0 to length of file and take this instead of a file. But adding this instead of using a file can also lead to problems. I guess. So we have 2 options:

  • Create an in memory file and take this path (if possible) to format this file and rewrite the content of the in memory file to the original one
  • Take range from 0 to end of file and send it to the Prettier CLI

But the latter would only work if I could pass a String to the Prettier CLI, but as I understood, I have to pass a file path or am I wrong?

funfried added a commit that referenced this issue Oct 18, 2022
@funfried
Copy link
Owner Author

Opened a new ticket regarding prettier.io #233

@funfried funfried added this to the 1.15.3 milestone Oct 18, 2022
@Chris2011
Copy link

But the latter would only work if I could pass a String to the Prettier CLI, but as I understood, I have to pass a file path or am I wrong?

I didn't test it out yet how it directly works.

funfried added a commit that referenced this issue Oct 18, 2022
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

2 participants