godot/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/IdentifierUtils.cs

206 lines
6.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace GodotTools.ProjectEditor
{
public static class IdentifierUtils
{
public static string SanitizeQualifiedIdentifier(string qualifiedIdentifier, bool allowEmptyIdentifiers)
{
if (string.IsNullOrEmpty(qualifiedIdentifier))
throw new ArgumentException($"{nameof(qualifiedIdentifier)} cannot be empty", nameof(qualifiedIdentifier));
string[] identifiers = qualifiedIdentifier.Split('.');
for (int i = 0; i < identifiers.Length; i++)
{
identifiers[i] = SanitizeIdentifier(identifiers[i], allowEmpty: allowEmptyIdentifiers);
}
return string.Join(".", identifiers);
}
3.2 New csproj style with backport of Godot.NET.Sdk This is a cherry-pick of ced77b1e9b5ffe0eb66de3d730d8583d12366c91 with several 3.2 specific alterations. There are a lot of build issues coming from old style projects. At this point fixing every single one of those would require adding patch after patch to the project file, which is a considerable amount work and makes the csproj even more bloated than it already is. As such I decided this effort would be better spent back-porting the Sdk style support that's already available in 4.0-dev to the 3.2 branch. This will prevent many issues, but it will also introduce other benefits, among them: - While target framework stays as .NET Framework v4.7.2, it can be changed to .NET Standard 2.0 or greater if desired. - It makes it much easier to add future patches. They are added to Godot.NET.Sdk and the only change required in Godot code is to update the Sdk version to use. - Default Godot define constants are also backported, which fixes IDE issues with the preprocessor. There are a few differences in the changes applied during patching of the csproj compared to 4.0 with the purpose of preventing breaking builds: - 'TargetFramework' stays net472 both for new projects and when importing old ones. It can be manually changed to netstandard 2.0+ if desired though. The following features are enabled by default for new projects. Enabling them in imported projects may result in errors that must be fixed manually: - 'EnableDefaultCompileItems' is disabled as it can result in undesired C# source files being included. Existing include items are kept. As long as 'EnableDefaultCompileItems' remains disabled, Godot will continue taking care of adding and removing C# files to the csproj. - 'GenerateAssemblyInfo' is disabled as it guarantees a build error because of conflicts between the existing 'AssemblyInfo.cs' and the auto-generated one. - 'Deterministic' is disabled because it doesn't like wildcards in the assembly version (1.0.*) that was in the old 'AssemblyInfo.cs'. Of importance: This is a breaking change. A great effort was put in avoiding build errors after upgrading a project, but there may still be exceptions. This also breaks forward compatibility. Projects opened with Godot 3.2.3 won't work out of the box with older Godot versions. This was already the case with changes introduced in 3.2.2. Albeit C# support in 3.2.x was still labeled as alpha, we've been trying to treat it as stable for some time. Still the amount of problems this change solves justifies it, but no more changes that break project compatibility are to be introduced from now on (at least for 3.x).
2020-07-20 13:48:12 +00:00
/// <summary>
/// Skips invalid identifier characters including decimal digit numbers at the start of the identifier.
/// </summary>
private static void SkipInvalidCharacters(string source, int startIndex, StringBuilder outputBuilder)
{
for (int i = startIndex; i < source.Length; i++)
{
char @char = source[i];
switch (char.GetUnicodeCategory(@char))
{
case UnicodeCategory.UppercaseLetter:
case UnicodeCategory.LowercaseLetter:
case UnicodeCategory.TitlecaseLetter:
case UnicodeCategory.ModifierLetter:
case UnicodeCategory.LetterNumber:
case UnicodeCategory.OtherLetter:
outputBuilder.Append(@char);
break;
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.ConnectorPunctuation:
case UnicodeCategory.DecimalDigitNumber:
// Identifiers may start with underscore
if (outputBuilder.Length > startIndex || @char == '_')
outputBuilder.Append(@char);
break;
}
}
}
public static string SanitizeIdentifier(string identifier, bool allowEmpty)
{
if (string.IsNullOrEmpty(identifier))
{
if (allowEmpty)
return "Empty"; // Default value for empty identifiers
throw new ArgumentException($"{nameof(identifier)} cannot be empty if {nameof(allowEmpty)} is false", nameof(identifier));
}
if (identifier.Length > 511)
identifier = identifier.Substring(0, 511);
var identifierBuilder = new StringBuilder();
int startIndex = 0;
if (identifier[0] == '@')
{
identifierBuilder.Append('@');
startIndex += 1;
}
3.2 New csproj style with backport of Godot.NET.Sdk This is a cherry-pick of ced77b1e9b5ffe0eb66de3d730d8583d12366c91 with several 3.2 specific alterations. There are a lot of build issues coming from old style projects. At this point fixing every single one of those would require adding patch after patch to the project file, which is a considerable amount work and makes the csproj even more bloated than it already is. As such I decided this effort would be better spent back-porting the Sdk style support that's already available in 4.0-dev to the 3.2 branch. This will prevent many issues, but it will also introduce other benefits, among them: - While target framework stays as .NET Framework v4.7.2, it can be changed to .NET Standard 2.0 or greater if desired. - It makes it much easier to add future patches. They are added to Godot.NET.Sdk and the only change required in Godot code is to update the Sdk version to use. - Default Godot define constants are also backported, which fixes IDE issues with the preprocessor. There are a few differences in the changes applied during patching of the csproj compared to 4.0 with the purpose of preventing breaking builds: - 'TargetFramework' stays net472 both for new projects and when importing old ones. It can be manually changed to netstandard 2.0+ if desired though. The following features are enabled by default for new projects. Enabling them in imported projects may result in errors that must be fixed manually: - 'EnableDefaultCompileItems' is disabled as it can result in undesired C# source files being included. Existing include items are kept. As long as 'EnableDefaultCompileItems' remains disabled, Godot will continue taking care of adding and removing C# files to the csproj. - 'GenerateAssemblyInfo' is disabled as it guarantees a build error because of conflicts between the existing 'AssemblyInfo.cs' and the auto-generated one. - 'Deterministic' is disabled because it doesn't like wildcards in the assembly version (1.0.*) that was in the old 'AssemblyInfo.cs'. Of importance: This is a breaking change. A great effort was put in avoiding build errors after upgrading a project, but there may still be exceptions. This also breaks forward compatibility. Projects opened with Godot 3.2.3 won't work out of the box with older Godot versions. This was already the case with changes introduced in 3.2.2. Albeit C# support in 3.2.x was still labeled as alpha, we've been trying to treat it as stable for some time. Still the amount of problems this change solves justifies it, but no more changes that break project compatibility are to be introduced from now on (at least for 3.x).
2020-07-20 13:48:12 +00:00
SkipInvalidCharacters(identifier, startIndex, identifierBuilder);
if (identifierBuilder.Length == startIndex)
{
// All characters were invalid so now it's empty. Fill it with something.
identifierBuilder.Append("Empty");
}
identifier = identifierBuilder.ToString();
if (identifier[0] != '@' && IsKeyword(identifier, anyDoubleUnderscore: true))
identifier = '@' + identifier;
return identifier;
}
static bool IsKeyword(string value, bool anyDoubleUnderscore)
{
// Identifiers that start with double underscore are meant to be used for reserved keywords.
// Only existing keywords are enforced, but it may be useful to forbid any identifier
// that begins with double underscore to prevent issues with future C# versions.
if (anyDoubleUnderscore)
{
if (value.Length > 2 && value[0] == '_' && value[1] == '_' && value[2] != '_')
return true;
}
else
{
if (DoubleUnderscoreKeywords.Contains(value))
return true;
}
return Keywords.Contains(value);
}
private static readonly HashSet<string> DoubleUnderscoreKeywords = new HashSet<string>
{
"__arglist",
"__makeref",
"__reftype",
"__refvalue",
};
private static readonly HashSet<string> Keywords = new HashSet<string>
{
"as",
"do",
"if",
"in",
"is",
"for",
"int",
"new",
"out",
"ref",
"try",
"base",
"bool",
"byte",
"case",
"char",
"else",
"enum",
"goto",
"lock",
"long",
"null",
"this",
"true",
"uint",
"void",
"break",
"catch",
"class",
"const",
"event",
"false",
"fixed",
"float",
"sbyte",
"short",
"throw",
"ulong",
"using",
"where",
"while",
"yield",
"double",
"extern",
"object",
"params",
"public",
"return",
"sealed",
"sizeof",
"static",
"string",
"struct",
"switch",
"typeof",
"unsafe",
"ushort",
"checked",
"decimal",
"default",
"finally",
"foreach",
"partial",
"private",
"virtual",
"abstract",
"continue",
"delegate",
"explicit",
"implicit",
"internal",
"operator",
"override",
"readonly",
"volatile",
"interface",
"namespace",
"protected",
"unchecked",
"stackalloc",
};
}
}