From d14988b23ea6f748ffbf0c9cdb087c52432014e9 Mon Sep 17 00:00:00 2001 From: Jarno Kamminga Date: Wed, 6 Nov 2024 07:35:28 +0100 Subject: [PATCH] Added proper support for namespacing and fixed OnlyPropertiesCodeGenerator --- .../Avalonia.NameGenerator.VisualBasic.csproj | 2 +- .../InitializeComponentCodeGenerator.cs | 103 ++++++++++++++---- .../Generator/OnlyPropertiesCodeGenerator.cs | 55 ++++++++-- 3 files changed, 128 insertions(+), 32 deletions(-) diff --git a/src/Avalonia.NameGenerator.VisualBasic/Avalonia.NameGenerator.VisualBasic.csproj b/src/Avalonia.NameGenerator.VisualBasic/Avalonia.NameGenerator.VisualBasic.csproj index 9412b31..de790b7 100644 --- a/src/Avalonia.NameGenerator.VisualBasic/Avalonia.NameGenerator.VisualBasic.csproj +++ b/src/Avalonia.NameGenerator.VisualBasic/Avalonia.NameGenerator.VisualBasic.csproj @@ -5,7 +5,7 @@ true false XamlNameReferenceGenerator.VisualBasic - 1.6.1-beta1 + 1.6.1-beta2 MIT https://github.com/eqxmedianl/Avalonia.NameGenerator/releases https://github.com/eqxmedianl/Avalonia.NameGenerator/tree/visualbasic diff --git a/src/Avalonia.NameGenerator.VisualBasic/Generator/InitializeComponentCodeGenerator.cs b/src/Avalonia.NameGenerator.VisualBasic/Generator/InitializeComponentCodeGenerator.cs index 0bb729e..0c83c17 100644 --- a/src/Avalonia.NameGenerator.VisualBasic/Generator/InitializeComponentCodeGenerator.cs +++ b/src/Avalonia.NameGenerator.VisualBasic/Generator/InitializeComponentCodeGenerator.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Xml.Linq; using Avalonia.NameGenerator.VisualBasic.Domain; @@ -6,22 +7,9 @@ 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 - = @" ' Should the dev tools be attached. -"; public InitializeComponentCodeGenerator(IXamlTypeSystem types) { @@ -30,18 +18,60 @@ public InitializeComponentCodeGenerator(IXamlTypeSystem types) public string GenerateCode(string className, string nameSpace, IXamlType xamlType, IEnumerable names) { + var usingNameSpace = nameSpace.Contains("."); + var indent = (usingNameSpace) ? " " : ""; + var properties = new List(); var initializations = new List(); 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 $@"' + if (usingNameSpace) + { + var vbNameSpace = string.Join(".", nameSpace.Split('.').Skip(1)); + + return $@"' + +Imports Avalonia +Imports Avalonia.Controls +Imports Avalonia.Markup.Xaml + +Namespace {vbNameSpace} + + Partial Class {className} + +{string.Join("\n", properties)} + + ' + ' Wires up the controls and optionally loads XAML markup and attaches dev tools (if Avalonia.Diagnostics package is referenced). + ' + ' Should the XAML be loaded into the component. +{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 $@"' Imports Avalonia Imports Avalonia.Controls @@ -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). ' ' Should the XAML be loaded into the component. -{(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} ' Should the dev tools be attached. +"; + } + private static bool IsWindow(IXamlType xamlType) { var type = xamlType; diff --git a/src/Avalonia.NameGenerator.VisualBasic/Generator/OnlyPropertiesCodeGenerator.cs b/src/Avalonia.NameGenerator.VisualBasic/Generator/OnlyPropertiesCodeGenerator.cs index 3356da5..48828a2 100644 --- a/src/Avalonia.NameGenerator.VisualBasic/Generator/OnlyPropertiesCodeGenerator.cs +++ b/src/Avalonia.NameGenerator.VisualBasic/Generator/OnlyPropertiesCodeGenerator.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Xml.Linq; using Avalonia.NameGenerator.VisualBasic.Domain; using XamlX.TypeSystem; @@ -9,23 +10,55 @@ internal class OnlyPropertiesCodeGenerator : ICodeGenerator { public string GenerateCode(string className, string nameSpace, IXamlType xamlType, IEnumerable 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 $@"// -using Avalonia.Controls; + if (usingNameSpace) + { + var vbNameSpace = string.Join(".", nameSpace.Split('.').Skip(1)); + return $@"' + +Imports Avalonia +Imports Avalonia.Controls +Imports Avalonia.Markup.Xaml + +Namespace {vbNameSpace} + + Partial Class {className} + +{lines} + + End Class + +End Namespace +"; + } + else + { + return $@"' + +Imports Avalonia +Imports Avalonia.Controls +Imports Avalonia.Markup.Xaml -namespace {nameSpace} -{{ - partial class {className} - {{ +Partial Class {className} + {lines} - }} -}} + +End Class "; + } } } \ No newline at end of file