forked from MochiLibraries/Biohazrd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSharpCodeWriter.cs
68 lines (56 loc) · 2.56 KB
/
CSharpCodeWriter.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Biohazrd.OutputGeneration;
using System.IO;
namespace Biohazrd.CSharp
{
[ProvidesOutputSessionFactory]
public partial class CSharpCodeWriter : CLikeCodeWriter
{
protected CSharpCodeWriter(OutputSession session, string filePath)
: base(session, filePath)
{ }
private static OutputSession.WriterFactory<CSharpCodeWriter> FactoryMethod => (session, filePath) => new CSharpCodeWriter(session, filePath);
public LeftAdjustedScope DisableScope(bool disabled, string? message)
{
if (!disabled)
{ return default; }
EnsureSeparation();
LeftAdjustedScope ret;
if (message is null)
{ ret = CreateLeftAdjustedScope("#if false", "#endif"); }
else
{ ret = CreateLeftAdjustedScope($"#if false // {message}", "#endif"); }
NoSeparationNeededBeforeNextLine();
return ret;
}
public LeftAdjustedScope DisableScope(string? message = null)
=> DisableScope(true, message);
public void WriteIdentifier(string identifier)
=> Write(SanitizeIdentifier(identifier));
protected override void WriteBetweenHeaderAndCode(StreamWriter writer)
{
foreach (string usingNamespace in UsingNamespaces)
{ writer.WriteLine($"using {usingNamespace};"); }
if (UsingNamespaces.Count > 0)
{ writer.WriteLine(); }
}
protected override void WriteOutHeaderComment(StreamWriter writer)
{
// Roslyn has special understanding of the <auto-generated> comment to indicate the file should not be auto-formatted or receive code analysis warnings
// Unfortunately another part of this special understanding is that it automatically disables nullability even if it's enabled project-wide
// Biohazrd does not generally generate anything that needs nullable annotations, but custom declarations might, so we enable it by default
// See https://github.com/InfectedLibraries/Biohazrd/issues/149
if (OutputSession.GeneratedFileHeaderLines.Length == 0)
{
writer.WriteLine("// <auto-generated />");
base.WriteOutHeaderComment(writer); // Sanity
}
else
{
writer.WriteLine("// <auto-generated>");
base.WriteOutHeaderComment(writer);
writer.WriteLine("// </auto-generated>");
}
writer.WriteLine("#nullable enable");
}
}
}