Skip to content

Commit

Permalink
Added proper support for namespacing and fixed OnlyPropertiesCodeGene…
Browse files Browse the repository at this point in the history
…rator
  • Loading branch information
jarnokamminga committed Nov 6, 2024
1 parent ac15c1e commit d14988b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>
<PackageId>XamlNameReferenceGenerator.VisualBasic</PackageId>
<PackageVersion>1.6.1-beta1</PackageVersion>
<PackageVersion>1.6.1-beta2</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>https://github.com/eqxmedianl/Avalonia.NameGenerator/releases</PackageReleaseNotes>
<RepositoryUrl>https://github.com/eqxmedianl/Avalonia.NameGenerator/tree/visualbasic</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

using Avalonia.NameGenerator.VisualBasic.Domain;
using XamlX.TypeSystem;

namespace Avalonia.NameGenerator.VisualBasic.Generator;

internal class InitializeComponentCodeGenerator: ICodeGenerator
internal class InitializeComponentCodeGenerator : ICodeGenerator
{
private readonly bool _diagnosticsAreConnected;
private const string AttachDevToolsCodeBlock = @"
#If DEBUG Then
' Attach dev tools (if Avalonia.Diagnostics package is referenced).
If (attachDevTools) Then
MyBase.AttachDevTools()
End If
#End If
";
private const string AttachDevToolsParameterDocumentation
= @" ' <param name=""attachDevTools"">Should the dev tools be attached.</param>
";

public InitializeComponentCodeGenerator(IXamlTypeSystem types)
{
Expand All @@ -30,18 +18,60 @@ public InitializeComponentCodeGenerator(IXamlTypeSystem types)

public string GenerateCode(string className, string nameSpace, IXamlType xamlType, IEnumerable<ResolvedName> names)
{
var usingNameSpace = nameSpace.Contains(".");
var indent = (usingNameSpace) ? " " : "";

var properties = new List<string>();
var initializations = new List<string>();
foreach (var resolvedName in names)
{
var (typeName, name, fieldModifier) = resolvedName;
properties.Add($" {fieldModifier} Property {name} As {typeName}");
initializations.Add($" {name} = FindNameScope().Find(Of {typeName})(\"{name}\")");
properties.Add($"{indent} {fieldModifier} Property {name} As {typeName}");
initializations.Add($"{indent} {name} = FindNameScope()?.Find(Of {typeName})(\"{name}\")");
}

var attachDevTools = _diagnosticsAreConnected && IsWindow(xamlType);

return $@"' <auto-generated />
if (usingNameSpace)
{
var vbNameSpace = string.Join(".", nameSpace.Split('.').Skip(1));

return $@"' <auto-generated />
Imports Avalonia
Imports Avalonia.Controls
Imports Avalonia.Markup.Xaml
Namespace {vbNameSpace}
Partial Class {className}
{string.Join("\n", properties)}
' <summary>
' Wires up the controls and optionally loads XAML markup and attaches dev tools (if Avalonia.Diagnostics package is referenced).
' </summary>
' <param name=""loadXaml"">Should the XAML be loaded into the component.</param>
{GetAttachDevToolsParameterDocumentation(attachDevTools, usingNameSpace)}
Private Sub InitializeComponent(Optional loadXaml As Boolean = True{(attachDevTools ? ", Optional attachDevTools As Boolean = True" : string.Empty)})
If loadXaml Then
AvaloniaXamlLoader.Load(Me)
End If
{GetAttachDevToolsCodeBlock(attachDevTools, usingNameSpace)}
{string.Join("\n", initializations)}
End Sub
End Class
End Namespace
";
}
else
{
return $@"' <auto-generated />
Imports Avalonia
Imports Avalonia.Controls
Expand All @@ -55,22 +85,55 @@ Partial Public Class {className}
' Wires up the controls and optionally loads XAML markup and attaches dev tools (if Avalonia.Diagnostics package is referenced).
' </summary>
' <param name=""loadXaml"">Should the XAML be loaded into the component.</param>
{(attachDevTools ? AttachDevToolsParameterDocumentation : string.Empty)}
{GetAttachDevToolsParameterDocumentation(attachDevTools, usingNameSpace)}
Private Sub InitializeComponent(Optional loadXaml As Boolean = True{(attachDevTools ? ", Optional attachDevTools As Boolean = True" : string.Empty)})
If loadXaml Then
AvaloniaXamlLoader.Load(Me)
End If
{(attachDevTools ? AttachDevToolsCodeBlock : string.Empty)}
{GetAttachDevToolsCodeBlock(attachDevTools, usingNameSpace)}
{string.Join("\n", initializations)}
End Sub
End Class
";
}
}


private static string GetAttachDevToolsCodeBlock(bool attachDevTools, bool usingNameSpace)
{
if (!attachDevTools)
{
return string.Empty;
}

var indent = (usingNameSpace) ? " " : "";
return $@"
#If DEBUG Then
{indent} ' Attach dev tools (if Avalonia.Diagnostics package is referenced).
{indent} If (attachDevTools) Then
{indent} MyBase.AttachDevTools()
{indent} End If
#End If
";
}

private string GetAttachDevToolsParameterDocumentation(bool attachDevTools, bool usingNameSpace)
{
if (!attachDevTools)
{
return string.Empty;
}

var indent = (usingNameSpace) ? " " : "";
return $@"{indent} ' <param name=""attachDevTools"">Should the dev tools be attached.</param>
";
}

private static bool IsWindow(IXamlType xamlType)
{
var type = xamlType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Avalonia.NameGenerator.VisualBasic.Domain;
using XamlX.TypeSystem;

Expand All @@ -9,23 +10,55 @@ internal class OnlyPropertiesCodeGenerator : ICodeGenerator
{
public string GenerateCode(string className, string nameSpace, IXamlType xamlType, IEnumerable<ResolvedName> names)
{
var usingNameSpace = nameSpace.Contains(".");
var indent = (usingNameSpace) ? " " : "";

var namedControls = names
.Select(info => " " +
$"{info.FieldModifier} {info.TypeName} {info.Name} => " +
$"this.FindNameScope()?.Find<{info.TypeName}>(\"{info.Name}\");")
.Select(info => $@"{indent} {info.FieldModifier} ReadOnly Property {info.Name} As {info.TypeName}
{indent} Get
{indent} Return FindNameScope()?.Find(Of {info.TypeName})(""{info.Name}"")
{indent} End Get
{indent} End Property
")
.ToList();
var lines = string.Join("\n", namedControls);
return $@"// <auto-generated />

using Avalonia.Controls;
if (usingNameSpace)
{
var vbNameSpace = string.Join(".", nameSpace.Split('.').Skip(1));
return $@"' <auto-generated />
Imports Avalonia
Imports Avalonia.Controls
Imports Avalonia.Markup.Xaml
Namespace {vbNameSpace}
Partial Class {className}
{lines}
End Class
End Namespace
";
}
else
{
return $@"' <auto-generated />
Imports Avalonia
Imports Avalonia.Controls
Imports Avalonia.Markup.Xaml
namespace {nameSpace}
{{
partial class {className}
{{
Partial Class {className}
{lines}
}}
}}
End Class
";
}
}
}

0 comments on commit d14988b

Please sign in to comment.