xml-formatter icon indicating copy to clipboard operation
xml-formatter copied to clipboard

Is there support for Visual Studio like formatting each attribute in new line with indentation?

Open pmahend1 opened this issue 4 years ago • 6 comments

Expected:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:d="http://xamarin.com/schemas/2014/forms/design" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             mc:Ignorable="d" 
             x:Class="App9.MainPage">
    <StackLayout>
        <Label Text="Welcome to Xamarin.Forms!" 
            HorizontalOptions="Center" 
            VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>

Default

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="App9.MainPage">
	<StackLayout>
		<Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
	</StackLayout>
</ContentPage>

pmahend1 avatar Mar 27 '20 02:03 pmahend1

Thanks for your message.

According to you, what should be the rule to wrap the attributes into the next line ?

chrisbottin avatar Apr 12 '20 08:04 chrisbottin

I am just wondering if we can format XML as above, see the first attribute is on same line as of start tag and remaining attributes aligned with first attribute?

pmahend1 avatar Apr 12 '20 20:04 pmahend1

I will look into it if that can be done easily but I can't give you a time estimate.

chrisbottin avatar Apr 18 '20 11:04 chrisbottin

@chrisbottin I think it can be done by having a parameter on line length. E.g. above 120 character length if there are at least 2 attributes, the 2nd, 3rd, etc gets the padding of (tag's padding + tags length + 1 (opening '<') + 1 (space)) or simply just an extra padding as it would be a child element. These are just ideas, I am not sure what other issues might be here.

dgergely avatar Oct 19 '20 05:10 dgergely

Thanks for your comment @dgergely, I agree it's a good approach. I might take a look this week.

chrisbottin avatar Oct 19 '20 12:10 chrisbottin

I couldnt find this feature in any npm packages or even standard C# XMLWriter unfortunately. I ended up writing it down through C# and using electron-edge-js to call dll for my extension PrettyXML Yes the approach is to track element space length and attribute space length separately.

pmahend1 avatar Oct 19 '20 14:10 pmahend1