-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCommentAttribute.cs
107 lines (96 loc) · 3.61 KB
/
CommentAttribute.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Text;
using JetBrains.Annotations;
namespace Automation {
[AttributeUsage(AttributeTargets.All)]
public class CommentAttribute : Attribute {
[NotNull] private string _text;
public CommentAttribute([NotNull] string text, ListPossibleOptions list= ListPossibleOptions.NoSelection)
{
_text = text;
List = list;
}
[NotNull]
public string Text {
get => _text;
set => _text = value.Replace("\n","").Replace("\r","");
}
[NotNull]
[ItemNotNull]
public List<string> TurnIntoComment(int indentDepth)
{
List<string> wrapped = WrapText(Text, 60);
switch (List) {
case ListPossibleOptions.ListOutputFileDefaults: {
wrapped.Add("Possible Options:");
foreach (var name in Enum.GetNames(typeof(OutputFileDefault))) {
wrapped.Add(name);
}
break;
}
case ListPossibleOptions.NoSelection:
break;
case ListPossibleOptions.ListCalcOptions:
wrapped.Add("Possible Options:");
foreach (var name in Enum.GetNames(typeof(CalcOption)))
{
wrapped.Add(name);
}
break;
case ListPossibleOptions.EnergyIntensityTypes:
wrapped.Add("Possible Options:");
foreach (var name in Enum.GetNames(typeof(EnergyIntensityType)))
{
wrapped.Add(name);
}
break;
case ListPossibleOptions.LoadTypePriorities:
wrapped.Add("Possible Options:");
foreach (var name in Enum.GetNames(typeof(LoadTypePriority)))
{
wrapped.Add(name);
}
break;
default:
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
throw new ArgumentOutOfRangeException(nameof(List));
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
}
string indentSpace = "";
for (int i = 0; i < indentDepth; i++) {
indentSpace += " ";
}
for (int i = 0; i < wrapped.Count; i++) {
wrapped[i] = indentSpace + "// " + wrapped[i];
}
return wrapped;
}
public ListPossibleOptions List { get; }
[NotNull]
[ItemNotNull]
public static List<string> WrapText([NotNull] string text, int targetLineLength)
{
string[] originalLines = text.Split(new[] { " " },
StringSplitOptions.None);
List<string> wrappedLines = new List<string>();
StringBuilder actualLine = new StringBuilder();
double actualWidth = 0;
foreach (var item in originalLines)
{
actualLine.Append(item + " ");
actualWidth += item.Length;
if (actualWidth > targetLineLength)
{
wrappedLines.Add(actualLine.ToString());
actualLine.Clear();
actualWidth = 0;
}
}
if (actualLine.Length > 0) {
wrappedLines.Add(actualLine.ToString());
}
return wrappedLines;
}
}
}