pmahend1 / PrettyXML

Pretty XML is a XML Formatter extension for Visual Studio Code
MIT License
27 stars 17 forks source link

Allow the user to specify which elements have attributes on separate lines #162

Open piersdeseilligny opened 3 months ago

piersdeseilligny commented 3 months ago

There's certain situations, especially XAML, where it'd be great to have further control over when attributes are positioned on the same line, and when they're positioned on separate lines.

For example, in XAML, a very common scenario for readability is to have the top-level element (such as ContentPage) have all attributes on separate lines, but any other element has in-line attributes.

A straightforward way of enabling this could be a new setting, such as "Blacklist For 'Position All Attributes On First Line'", in which you could specify elements ("ContentPage,Shell,Application"), for which PrettyXML would ignore the "Position All Attributes on First Line" setting.

pmahend1 commented 3 months ago

Thanks for requesting a new feature. Would it not be inconsistent to have only top level elements or specific elements to have attributes in new line? Also user would have to keep updating that list based on their needs.

piersdeseilligny commented 3 months ago

So Microsoft isn't very consistent with this themselves, but if you look through some examples, it's a very common way of formatting xaml - if you don't have too many attributes (which ideally you shouldn't anyway), it's a good way of seeing the overall structure of a document more clearly

On this example:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="...">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" Default="20">
            <On Platform="iOS" Value="0,20,0,0" />
            <On Platform="Android" Value="10,20,20,10" />
        </OnPlatform>
    </ContentPage.Padding>
    ...
</ContentPage>

It's arguably easier to immediately recognise the structure of the document, because the elements are the only items on new lines, rather than:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="...">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" 
                    Default="20">
            <On Platform="iOS" 
                Value="0,20,0,0" />
            <On Platform="Android" 
                Value="10,20,20,10" />
        </OnPlatform>
    </ContentPage.Padding>
    ...
</ContentPage>

This is especially true on very large documents, or when you have very different numbers of attributes across elements. However, the root element tends to have a lot of very long attributes, which are a nightmare to read if they're all on the one line.

As a user, you could easily configure this at the workspace-level, so you wouldn't be constantly updating it. For example on MAUI Projects, the following settings.json would apply in 90% of cases:

{
    "prettyxml.settings.blacklistForAllAttributesOnFirstLine": "ContentPage,ContentView,ResourceDictionary,Application,Shell"
}

So you could just re-use that settings.json in MAUI workspaces, and keep the default settings for PrettyXML on everything else.

pmahend1 commented 3 months ago

I don't think that is a standard formatting. I have seen Microsoft use different formatting over the years. It seems to be personal preference of different developers. The example you are pointing at seems to one off from other examples where they are mostly following each attribute on new line formatting.

The idea of not having all attributes in same line is not clog up horizontal space, it is less readable. If there are too many attributes with longer values the horizontal space takes too much space you will end up scrolling horizontally and vertically a lot.

if you don't have too many attributes (which ideally you shouldn't anyway), it's a good way of seeing the overall structure of a document more clearly

Attributes In Newline Threshold setting already does this. You just to have set it according to your needs, in this case set it to may be 2. By this way it will be consistent.

pmahend1 commented 6 days ago

Do you still need this feature?