Use enum for access level in XamlUiPartialClassGenerator (#2161)

This commit is contained in:
Visne
2021-10-27 08:52:09 +02:00
committed by GitHub
parent cd693875e6
commit 068c05c355
4 changed files with 38 additions and 33 deletions

View File

@@ -13,5 +13,6 @@
<Compile Link="XamlX\filename" Include="../XamlX/src/XamlX/**/*.cs" />
<Compile Remove="../XamlX/src/XamlX/**/SreTypeSystem.cs" />
<Compile Remove="../XamlX/src/XamlX/obj/**" />
<Compile Include="..\Robust.Client\UserInterface\ControlPropertyAccess.cs" />
</ItemGroup>
</Project>

View File

@@ -6,6 +6,7 @@ using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Robust.Client.UserInterface;
using XamlX.Ast;
using XamlX.Emit;
using XamlX.IL;
@@ -37,10 +38,10 @@ namespace Robust.Client.AutoGenerated
class NameVisitor : IXamlAstVisitor
{
private readonly List<(string name, string type, string access)> _names =
new List<(string name, string type, string access)>();
private readonly List<(string name, string type, AccessLevel access)> _names =
new List<(string name, string type, AccessLevel access)>();
public static List<(string name, string type, string access)> GetNames(IXamlAstNode node)
public static List<(string name, string type, AccessLevel access)> GetNames(IXamlAstNode node)
{
var visitor = new NameVisitor();
node.Visit(visitor);
@@ -88,7 +89,7 @@ namespace Robust.Client.AutoGenerated
var reg = (nameText.Text,
$@"{clrtype.Namespace}.{clrtype.Name}",
accessText?.Text ?? "Protected");
accessText != null ? (AccessLevel) Enum.Parse(typeof(AccessLevel), accessText.Text) : AccessLevel.Protected);
if (!_names.Contains(reg))
{
_names.Add(reg);
@@ -137,36 +138,38 @@ namespace Robust.Client.AutoGenerated
var namedControls = names.Select(info =>
{
(string name, string type, string access) = info;
(string name, string type, AccessLevel access) = info;
string accessStr;
switch (access)
{
case "Public":
access = "public";
case AccessLevel.Public:
accessStr = "public";
break;
case "Protected" when classSymbol.IsSealed:
case "PrivateProtected" when classSymbol.IsSealed:
case "Private":
access = "private";
case AccessLevel.Protected when classSymbol.IsSealed:
case AccessLevel.PrivateProtected when classSymbol.IsSealed:
case AccessLevel.Private:
accessStr = "private";
break;
case "Protected":
access = "protected";
case AccessLevel.Protected:
accessStr = "protected";
break;
case "Internal":
case "ProtectedInternal" when classSymbol.IsSealed:
access = "internal";
case AccessLevel.PrivateProtected:
accessStr = "private protected";
break;
case "ProtectedInternal":
access = "protected internal";
case AccessLevel.Internal:
case AccessLevel.ProtectedInternal when classSymbol.IsSealed:
accessStr = "internal";
break;
case "PrivateProtected":
access = "private protected";
case AccessLevel.ProtectedInternal:
accessStr = "protected internal";
break;
default:
throw new ArgumentException($"Invalid access level for control {name} in {fileName}: {access}.");
throw new ArgumentException($"Invalid access level \"{Enum.GetName(typeof(AccessLevel), access)}\" " +
$"for control {name} in file {fileName}.");
}
return $" {access} global::{type} {name} => this.FindControl<global::{type}>(\"{name}\");";
return $" {accessStr} global::{type} {name} => this.FindControl<global::{type}>(\"{name}\");";
});
return $@"// <auto-generated />
@@ -182,7 +185,6 @@ namespace {nameSpace}
";
}
public void Execute(GeneratorExecutionContext context)
{
var comp = (CSharpCompilation) context.Compilation;

View File

@@ -998,14 +998,4 @@ namespace Robust.Client.UserInterface
public readonly int OldIndex;
public readonly int NewIndex;
}
public enum AccessLevel
{
Public,
Protected,
Internal,
ProtectedInternal,
Private,
PrivateProtected,
}
}

View File

@@ -0,0 +1,12 @@
namespace Robust.Client.UserInterface
{
public enum AccessLevel
{
Public,
Protected,
Internal,
ProtectedInternal,
Private,
PrivateProtected,
}
}