6

I have the problem that if I autoformat a XHTML file (JSF 1.2) in Eclipse with the keys CTRL + SHIFT + f, the output looks really bad and is not really readable anymore.

My formated code:

<span class="label viertel alignright"> 
    <h:outputLabel for="itLine5" value="E-Mail:" /> 
</span>

Becomes this:

<span class="label viertel alignright"> <h:outputLabel
       for="itLine5" value="E-Mail:" /> </span>

In this small example, it's still readable - but in longer code it's just chaos!

How do I make sure that my XHTML code is formatted in a readable manner?

I use this version of Eclipse Helios:

  • Eclipse Java EE IDE for Web Developers.
  • Version: Helios Service Release 1 - Build id: 20100917-0705

I use these JBoss Tools:

  • JBoss Archies Tools 3.2.0
  • JBoss Tools RichFaces 3.2.1
  • JBossASTools 2.2.0
  • Hibernate Tools 3.40.0
  • JBoss Seam Tools: 3.2.1
Magnilex
  • 10,219
  • 8
  • 49
  • 72
Joergi
  • 1,593
  • 2
  • 36
  • 74
  • 1
    Good question, would love if someone has an answer for it. :) I tend to not autoformat my xhtml-code. – Magnilex Nov 13 '12 at 13:52
  • 1
    I'm sure there is a cood checkstyle template for that, i hope somebody got one! – Joergi Nov 14 '12 at 10:37
  • 1
    i hope the bounty helps fixing this problem! – Joergi Nov 16 '12 at 12:19
  • For Helios, do you have any plugins installed to help you create and work with JSF projects and xhtml? – Magnilex Nov 16 '12 at 13:54
  • i have the JBoss tools installed. and i can't switch to Juno, i tried, but we have some dependencies which will not work with Indigo or Juno :( – Joergi Nov 16 '12 at 16:32
  • Exactly which version of Helios are you using? And which version of JBoss tools? – Magnilex Nov 19 '12 at 14:56
  • @MagnusTengdahl i updated my question with the plugins – Joergi Nov 19 '12 at 15:35
  • 1
    I downloaded your version of Eclipse and have tried to download the same version of Jboss Tools with no success. Tried both marketplace and the old fashioned update sites. Sorry, but I'm giving up. – Magnilex Nov 22 '12 at 13:03
  • to be honest: i installed it a long time ago... but a new developer is trying now to get everything working with a newer eclipse, then we try your trick (and you will get the bounty, of course) – Joergi Nov 22 '12 at 15:07

2 Answers2

7

I think I have a solution that works well enough (Eclipse Juno). A lot of tags/elements are specified as inline causing the wierd formatting. <span> for example is in that list. You could try to remove the list of inline elements (don't worry, you can always select "Restore Defaults"). While you're at it, change the width to something more useful than the default (72 in my case). The preferences are found in Window > Preferences > Web > HTML Files > Editor:

HTML file settings

I removed all the inline elements in the list and formatted a big xhtml file with good result. I set my line width to 120 which is what I use for Java. Of course, this change will affect regular HTML files as well.

From your example, this code:

<span class="label viertel alignright"> <h:outputLabel
        for="itLine5" value="E-Mail:" />
</span>

was formatted to:

<span class="label viertel alignright">
    <h:outputLabel for="itLine5" value="E-Mail:" />
</span>

Is this good enough for you?

Magnilex
  • 10,219
  • 8
  • 49
  • 72
5

I don't use CTRL + SHIFT + F to format the code but only CTRL + I to indent. This function works well only if all the code of the page is selected, so I use CTRL+A to select all and CTRL+I to indent.

The indent function preserves the line breaks and only indents the code. I'm using Juno, hope this also works in Helios!

doubleDown
  • 7,066
  • 1
  • 28
  • 45
Daniela Mogini
  • 289
  • 1
  • 5
  • 16
  • hmmm it's not really helping, if the code is already damaged from my `CTRL + SHIFT + f` ;) and if i have a new xhtml file, it make it looks nicer, but there are also some bad side effects on the tabbing which let it loook bad. – Joergi Nov 23 '12 at 09:25
  • Yes, you're right. There are some bad side effects when the tag is not empty. I just use it as a quick fix while writing xhtml. – Daniela Mogini Nov 23 '12 at 09:36