Skip to content

Commit

Permalink
Fix namespace lib adding usings and fix lib namespace definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajidur78 committed Nov 25, 2023
1 parent f209f4f commit f433153
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
48 changes: 15 additions & 33 deletions Source/Libraries/HedgeModManager.CodeCompiler/CSharpCode.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using HedgeModManager.CodeCompiler.PreProcessor;

namespace HedgeModManager.CodeCompiler;
namespace HedgeModManager.CodeCompiler;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using PreProcessor;
using System.Text;
using Foundation;

public class CSharpCode : ICode
{
private string mBody = string.Empty;
private SyntaxTreeEx? mCachedSyntaxTree;

public string ID { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;

Expand Down Expand Up @@ -259,6 +258,8 @@ public CompilationUnitSyntax CreateCompilationUnit(IIncludeResolver? includeReso
// No processing for naked codes
if (Naked)
{
unit = AddImports(unit.AddUsings(CodeProvider.PredefinedUsingDirectives));

return unit;
}

Expand Down Expand Up @@ -402,51 +403,32 @@ public CompilationUnitSyntax CreateCompilationUnit(IIncludeResolver? includeReso
.WithUsings(unit.Usings)
.AddUsings(CodeProvider.PredefinedUsingDirectives);

foreach (var reference in GetReferences())
{
var dotIndex = reference.LastIndexOf('.');
if (dotIndex < 0)
continue;
compileUnit = AddImports(compileUnit);
return compileUnit;

var namespaceRef = reference.Substring(0, dotIndex);
if (string.IsNullOrEmpty(namespaceRef))
CompilationUnitSyntax AddImports(CompilationUnitSyntax unit)
{
foreach (var import in GetImports())
{
continue;
unit = unit.AddUsings(MakeUsingDirective(import, true));
}

compileUnit = compileUnit.AddUsings(MakeUsingDirective(namespaceRef));
}

foreach (var import in GetImports())
{
compileUnit = compileUnit.AddUsings(MakeUsingDirective(import, true));
return unit;
}

return compileUnit;


MemberDeclarationSyntax MakeRootMember()
{
if (string.IsNullOrEmpty(libNamespace))
{
return classUnit;
}

return SyntaxFactory.NamespaceDeclaration(SyntaxFactory.IdentifierName(libNamespace)).AddMembers(classUnit);
return SyntaxFactory.NamespaceDeclaration(SyntaxFactoryEx.MakeName(libNamespace)).AddMembers(classUnit);
}

UsingDirectiveSyntax MakeUsingDirective(string usingName, bool isStatic = false)
{
var names = usingName.Split('.');
NameSyntax nameSyntax = SyntaxFactory.IdentifierName(names[0]);

for (var i = 1; i < names.Length; i++)
{
if (string.IsNullOrEmpty(names[i]))
continue;

nameSyntax = SyntaxFactory.QualifiedName(nameSyntax, SyntaxFactory.IdentifierName(names[i]));
}
NameSyntax nameSyntax = SyntaxFactoryEx.MakeName(usingName);

if (isStatic)
{
Expand Down
16 changes: 16 additions & 0 deletions Source/Libraries/HedgeModManager.CodeCompiler/SyntaxFactoryEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public static SyntaxToken GetToken(string name)
return type;
}

public static NameSyntax MakeName(string name)
{
var names = name.Split('.');
NameSyntax nameSyntax = SyntaxFactory.IdentifierName(names[0]);

for (var i = 1; i < names.Length; i++)
{
if (string.IsNullOrEmpty(names[i]))
continue;

nameSyntax = SyntaxFactory.QualifiedName(nameSyntax, SyntaxFactory.IdentifierName(names[i]));
}

return nameSyntax;
}

public static MethodDeclarationSyntax MethodDeclaration(string identifier, string returnType, BlockSyntax body, params string[] modifiers)
{
var mods = new SyntaxToken[modifiers.Length];
Expand Down

0 comments on commit f433153

Please sign in to comment.