Browse Source

Drop redundant own-namespace qualifier from generated ctor params

Enum-typed constructor parameters were emitted via SymbolDisplayFormat.FullyQualifiedFormat so
that an enum in another namespace resolves without a using. For enums declared in the same namespace
as the generated file (ICSharpCode.Decompiler.CSharp.Syntax) that produced a redundant
global::ICSharpCode.Decompiler.CSharp.Syntax. prefix, e.g. 'global::...Syntax.ClassType classType'.
The prefix is now stripped for the file's own namespace while cross-namespace enums stay qualified.
The global:: on Identifier.Create in NameSlot setters is deliberately kept: those run in expression
position where a string property literally named 'Identifier' (e.g. SimpleType) would otherwise
shadow the type.

Assisted-by: Claude:claude-opus-4-8:Claude Code
pull/3807/head
Siegfried Pammer 3 weeks ago committed by Siegfried Pammer
parent
commit
9618c35bae
  1. 13
      ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs

13
ICSharpCode.Decompiler.Generators/DecompilerSyntaxTreeGenerator.cs

@ -167,7 +167,7 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator @@ -167,7 +167,7 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
if (property.Type.TypeKind == TypeKind.Enum && property.SetMethod != null && !property.IsStatic)
{
ctorParams ??= new();
ctorParams.Add((property.Name, property.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), "", false, false));
ctorParams.Add((property.Name, CtorParamTypeName(property.Type), "", false, false));
}
}
@ -184,6 +184,17 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator @@ -184,6 +184,17 @@ internal class DecompilerSyntaxTreeGenerator : IIncrementalGenerator
// rules do not apply.
static string FieldName(string propertyName) => "slot_" + propertyName;
// Type name for an enum-typed constructor parameter. Fully-qualified so an enum in another
// namespace resolves without a using, minus the generated file's own namespace prefix, which is
// redundant there. Used only in parameter-type position, so no member-name shadowing applies
// (unlike Identifier.Create in NameSlot setters, which must stay global::-qualified).
static string CtorParamTypeName(ITypeSymbol type)
{
const string ownNamespacePrefix = "global::ICSharpCode.Decompiler.CSharp.Syntax.";
string name = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
return name.StartsWith(ownNamespacePrefix) ? name.Substring(ownNamespacePrefix.Length) : name;
}
void WriteGeneratedMembers(SourceProductionContext context, AstNodeAdditions source)
{
var builder = new StringBuilder();

Loading…
Cancel
Save