AvaloniaRider icon indicating copy to clipboard operation
AvaloniaRider copied to clipboard

Make Rider understand Avalonia XAML

Open SparkyTD opened this issue 4 years ago • 4 comments

Would it be possible to make rider understand the non-standard XAML syntax that Avalonia uses? For example the control binding syntax that uses a #number sign for referencing other controls. Or the dollar sign.

Are there any plans to implement this, or is it outside the scope of this project?

SparkyTD avatar May 12 '20 18:05 SparkyTD

Hello, and thanks for raising this question.

Yes, it should be possible, and I'm planning to do that eventually (not in the nearest perspective though).

Right now, some aspects of Avalonia XAML support are managed by ReSharper core, and I'm not sure how extendable the XAML code is (generally, ReSharper is very extendable, but I'm not familiar with XAML-related parts, yet).

Most likely, a hybrid approach will be taken: some parts will require changes to ReSharper, but I want to extract as much Avalonia-specific logic as possible to this plugin.

Could you please provide concrete examples of XAML syntax that you'd want AvaloniaRider to support?

ForNeVeR avatar May 13 '20 15:05 ForNeVeR

Sure! Keep in mind that I am relatively new to Avalonia (coming from WPF), so this list might not have everything.

Direct text content

In Avalonia, ContentControls support direct text content. For example, the default Avalonia Window template looks like this:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvalonioTest.MainWindow"
        Title="AvaloniaTest">
    Welcome to Avalonia!
</Window>

Rider thinks that this is a syntax error, and a red line appears under the text. Error: "Text is not expected here" This is not as important as the other features on this list.

Binding to a named control

I can use a # symbol in a binding expression to reference another named control. Example:

<TextBlock Name="other" Text="asd" />
<TextBlock Text="{Binding #other.Text}" />

Again, Rider is trying to interpret this as WPF XAML and underlines the # symbol. Error: Unexpected Token

Binding to an Ancestor

The same thing happens with the ancestor references and special keywords. Examples:

<Border Tag="Hello World!">
    <TextBlock Text="{Binding $parent.Tag}"/>
    <Decorator>
        <TextBlock Text="{Binding $parent[Border].Tag}"/>
        <TextBlock Text="{Binding $parent[Border;1].Tag}"/>
    </Decorator>
    <local:MyControl Tag="Hello World!">
        <Decorator>
            <TextBlock Text="{Binding $parent[local:MyControl].Tag}"/>
        </Decorator>
    </local:MyControl>
</Border>

Error: Unexpected Token

Negating Values

Values can be negated with the ! symbol in the binding expression. This works with non boolean values too (refer to the docs).

<!-- Negating a Boolean -->
<StackPanel>
    <TextBox Name="input" IsEnabled="{Binding AllowInput}"/>
    <TextBlock IsVisible="{Binding !AllowInput}">Sorry, no can do!</TextBlock>
</StackPanel>

<!-- Converting an int to a Boolean (with Convert.ToBoolean()) and negating it -->
<Panel>
    <ListBox Items="{Binding Items}"/>
    <TextBlock IsVisible="{Binding !Items.Count}">No results found</TextBlock>
</Panel>

<!-- Converting an int to a Boolean (with Convert.ToBoolean()) -->
<Panel>
    <ListBox Items="{Binding Items}" IsVisible="{Binding !!Items.Count}"/>
</Panel>

Binding to Tasks and Observables

The following code binds Text to an IObservable<string> property, and every time it changes, the binding is re-evaluated.

<TextBlock Text="{Binding Name^.Length}"/>

Everything is red after the ^ character. Error: Cannot resolve symbol '^.Length'

All of these examples were taken from the Avalonia docs, you'll find more details there. Autocomplete would be very nice, but at first, I'd be happy if these wouldn't show up as errors, because it puts red squiggly lines on all the parent files and folders in the solution explorer tree.

SparkyTD avatar May 13 '20 16:05 SparkyTD

Even if you don't plan on implementing all of this in the near future, I would really appreciate a temporary solution that at least disables the errors.

SparkyTD avatar May 15 '20 10:05 SparkyTD

For some of these inspections, you should be able to disable them via the Alt-Enter menu, like this: image

ForNeVeR avatar May 17 '20 09:05 ForNeVeR