-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsUsingStatementExtensions.cs
More file actions
46 lines (41 loc) · 2.01 KB
/
CsUsingStatementExtensions.cs
File metadata and controls
46 lines (41 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2022 CodeFactory, LLC
//*****************************************************************************
using CodeFactory.DotNet.CSharp;
namespace CodeFactory.Formatting.CSharp
{
/// <summary>
/// Extensions class that manage extensions that support CodeFactory models that implement the <see cref="CsUsingStatement"/> model.
/// </summary>
public static class CsUsingStatementExtensions
{
/// <summary>
/// Extension method that creates a fully formatted using statement.
/// </summary>
/// <example>
/// Format with alias using [alias] = [namespace];
/// Format without alias using [namespace];
/// </example>
/// <param name="source">The source using statement to use.</param>
/// <param name="includeAlias">Optional flag that determines if a alias is included to add it to the using statement definition.</param>
/// <param name="alias">Optional flag that allows you to set a custom alias. If set this will always overrider the includeAlias flag and any internal alias assigned to the model.</param>
/// <returns>Fully formatted using statement, or null if namespace data is missing from the model</returns>
public static string CSharpFormatUsingStatement(this CsUsingStatement source, bool includeAlias = true, string alias = null)
{
var nameSpace = source.ReferenceNamespace;
if (string.IsNullOrEmpty(nameSpace)) return null;
var formattedAlias = alias;
bool useAlias = !string.IsNullOrEmpty(formattedAlias);
if (!useAlias)
{
if (includeAlias & source.HasAlias)
{
formattedAlias = source.Alias;
useAlias = true;
}
}
return !useAlias ? $"using {nameSpace};" : $"using {formattedAlias} = {nameSpace};";
}
}
}