Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code formatting for SVG does not work in IntelliJ IDEA

I have the following SVG file generated by inkscape. As you can see, the formatting of the xml is quite messed up. For one thing, in my settings I have selected "Use Tab Character", but this file contains no tab characters. Many other things are also wrong, for example /><g and ><path.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:cc="http://creativecommons.org/ns#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns="http://www.w3.org/2000/svg"
        xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   version="1.1"
   x="0px"
   y="0px"
   width="105.83467"
   height="105.83467"
   viewBox="0 0 99.220001 99.220001"
   enable-background="new 0 0 100 100"
   xml:space="preserve"
   id="svg2"
   inkscape:version="0.92.1 r15371"
   sodipodi:docname="activity_template.svg"><metadata
     id="metadata13"><rdf:RDF><cc:Work
         rdf:about=""><dc:format>image/svg+xml</dc:format><dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" /><dc:title /></cc:Work></rdf:RDF></metadata><defs
     id="defs11" /><sodipodi:namedview
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1"
     objecttolerance="10"
     gridtolerance="10"
     guidetolerance="10"
     inkscape:pageopacity="0"
     inkscape:pageshadow="2"
     inkscape:window-width="1676"
     inkscape:window-height="1117"
     id="namedview9"
     showgrid="false"
     inkscape:zoom="5.7699913"
     inkscape:cx="2.3719576"
     inkscape:cy="60.109287"
     inkscape:window-x="0"
     inkscape:window-y="298"
     inkscape:window-maximized="0"
     inkscape:current-layer="svg2"
     fit-margin-top="0"
     fit-margin-left="0"
     fit-margin-right="0"
     fit-margin-bottom="0" /><g
     id="Background"
     transform="translate(-0.4542549,-0.33174549)" /><g
     id="Pictograms"
     transform="translate(-0.4542549,-0.33174549)"><path
       d="M 99.167833,18.811353 81.549745,0.77689213 c -0.19,-0.19 -0.501274,-0.36095895 -0.771274,-0.36127448 L 2.1282745,0.32370588 C 1.5682749,0.32305146 0.392628,1.5090001 0.39298039,2.059 l 0.0612745,95.634902 c 3.5239e-4,0.55 1.05274521,1.857486 1.61274511,1.857843 l 96.053823,0.06128 c 0.55,3.5e-4 1.613083,-1.001471 1.612746,-1.857843 l -0.03064,-77.84554 c -1.02e-4,-0.259999 -0.345098,-0.918284 -0.535096,-1.098289 z m -4.04848,-0.222549 -13.773431,-0.06127 0.06127,-13.7347055 z M 96.937,96.939 H 3.067 V 3.059 h 75.85 v 17.02 c 0,0.55 0.45,1 1,1 h 17.02 z"
       id="path6"
       inkscape:connector-curvature="0"
       sodipodi:nodetypes="ccsssssssscccccccccsscc" /></g><g
     id="Guides"
     transform="translate(-0.4542549,-0.33174549)" /><path
     id="path3021"
     d="m 88.804011,52.081722 c -4.779066,-1.280811 -5.862105,-12.487045 -1.941764,-14.749867 1.285092,-0.744856 2.281658,-1.801354 2.940042,-3.019664 -1.019682,-3.718289 -4.499102,-9.07526 -6.46227,-11.457944 -1.434918,-0.06678 -2.90237,0.261128 -4.237975,1.032525 -4.276503,2.470868 -13.117176,-4.137805 -11.800407,-9.056426 0.411811,-1.536801 0.194348,-3.400653 -0.141266,-4.489685 -2.430628,-1.2568381 -8.186569,-2.9477474 -12.461359,-3.366408 -0.921224,0.6703706 -1.976009,2.2910752 -2.386965,3.83815 -1.394679,5.201152 -12.431394,5.957139 -14.798666,1.85786 -0.790233,-1.36985 -2.245699,-2.645524 -3.251684,-3.0650415 -2.765385,0.8381785 -8.774749,4.3218805 -11.205378,6.3329915 0.167807,1.424646 0.200341,3.079596 1.017115,4.492254 2.36556,4.100135 -4.04106,13.217346 -9.10009,11.862051 -1.664368,-0.445202 -3.410928,-0.318491 -4.835572,0.267977 -1.1737914,2.599291 -2.9503161,9.454537 -3.1181228,12.219923 0.3356134,0.754274 2.3364504,2.078748 3.9366088,2.506826 6.886067,1.845874 6.940006,11.944241 1.95118,14.824352 -1.409233,0.814205 -2.616414,1.944332 -3.1189784,3.369833 0.8381784,2.681482 4.2062984,8.576976 6.3852174,11.091508 1.424644,0.166951 3.079595,-0.198628 4.49311,-1.016258 4.455438,-2.574462 13.366317,3.479421 11.860337,9.101802 -0.391264,1.463171 -0.174656,3.002541 0.0762,4.511946 3.352709,1.424644 9.417722,3.42891 12.770432,3.345007 1.089031,-1.005988 1.761115,-2.407516 2.143816,-3.838154 1.318481,-4.921188 12.202801,-6.49138 14.774695,-2.035941 0.718316,1.245708 1.724299,2.428061 2.897234,3.098431 3.51966,-1.005984 9.707103,-4.468281 11.804687,-6.6472 0.08219,-1.591595 -0.517119,-2.921206 -1.261975,-4.214002 -2.77994,-4.810744 3.714008,-13.23447 9.053857,-11.802976 1.462316,0.390408 3.054767,0.258559 4.396364,-0.0762 1.172934,-2.93405 3.118121,-8.175441 3.452022,-12.533277 -1.172933,-1.340742 -2.285937,-1.970016 -3.830443,-2.384396 z m -16.2704,3.250826 C 69.274222,67.504527 56.765774,74.725351 44.595508,71.465108 32.426953,68.203151 25.206129,55.692134 28.466373,43.521011 31.726618,31.349889 44.235066,24.128208 56.40362,27.389309 c 12.169409,3.2611 19.391091,15.770404 16.129991,27.943239 z"
     inkscape:connector-curvature="0"
     style="clip-rule:evenodd;fill-rule:evenodd" /></svg>

So, I would like to re-format my .svg file, but when I do "Reformat Code" (Ctrl+Shift+L) in IntelliJ IDEA, nothing happens.

I tried going to "File Types" and re-assigning .svg files to the XML file type, no difference.

I tried renaming the .svg file to .xml, no difference.

If I do "Auto-indent Lines" (Ctrl+Alt+I) the current line is in fact properly indented with tabs, so if I do this for each line I can at least have the entire file properly indented, but no other formatting problems are fixed.

How can I have IntelliJ IDEA re-format my .svg file?

like image 391
Mike Nakis Avatar asked Jan 29 '23 02:01

Mike Nakis


1 Answers

It turns out that in this particular .svg file, the <svg> tag contains a sneaky xml:space="preserve" attribute.

And it turns out that IntelliJ IDEA pays attention to this attribute, so the "Reformat Code" command does nothing.

Removing this attribute allows proper re-formatting of the .svg file.

(So, in light of that, the fact that IntelliJ IDEA's "Indent Lines" command does not pay attention to the xml:space="preserve" attribute might actually be a bug.)

Apparently some .svg files contain this attribute, some don't, and its presence or absence does not seem to affect the .svg file in any other way.

Useful comments attached to the answer for posterity:

ccprog says: Inkscape adds this attribute to all text elements. It is a behaviour I have not found any possibility to change. Nonetheless, this is the first time I have seen it on the root element. You might want to look at the configuration folder of Inkscape (look at Settings -> System to find the locations for user/global configs). There are some files templates/default..svg used to initialize empty files on startup. Maybe the attribute has sneaked its way in there. You need to edit them/set a local one by hand.

Robert Longson says: w3.org/TR/SVG/struct.html#XMLSpaceAttribute

like image 102
Mike Nakis Avatar answered Feb 05 '23 15:02

Mike Nakis