Skip to content

Commit be6f32b

Browse files
committed
Add NewContentAggregator and factory
Replaces CodeBlockInsertRefactoringAction
1 parent 7a89dac commit be6f32b

File tree

5 files changed

+138
-291
lines changed

5 files changed

+138
-291
lines changed

Rubberduck.Refactorings/CodeBlockInsert/CodeBlockInsertModel.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

Rubberduck.Refactorings/CodeBlockInsert/CodeBlockInsertRefactoringAction.cs

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Rubberduck.Refactorings.EncapsulateField
6+
{
7+
public enum NewContentType
8+
{
9+
ImplementsDeclaration,
10+
WithEventsDeclaration,
11+
EnumerationTypeDeclaration,
12+
UserDefinedTypeDeclaration,
13+
DeclarationBlock,
14+
CodeSectionBlock,
15+
}
16+
17+
public interface INewContentAggregator
18+
{
19+
void AddNewContent(NewContentType contentType, string block);
20+
void AddNewContent(string contentIdentifier, string block);
21+
string RetrieveBlock(params NewContentType[] newContentTypes);
22+
string RetrieveBlock(params string[] contentIdentifiers);
23+
int NewLineLimit { set; get; }
24+
}
25+
26+
public class NewContentAggregator : INewContentAggregator
27+
{
28+
private readonly static string _doubleSpace = $"{Environment.NewLine}{Environment.NewLine}";
29+
private readonly Dictionary<NewContentType, List<string>> _newContent;
30+
private Dictionary<string, List<string>> _unStructuredContent;
31+
32+
public NewContentAggregator()
33+
{
34+
_newContent = new Dictionary<NewContentType, List<string>>
35+
{
36+
{ NewContentType.UserDefinedTypeDeclaration, new List<string>() },
37+
{ NewContentType.DeclarationBlock, new List<string>() },
38+
{ NewContentType.CodeSectionBlock, new List<string>() },
39+
};
40+
41+
_unStructuredContent = new Dictionary<string, List<string>>();
42+
}
43+
44+
public void AddNewContent(NewContentType contentType, string newContent)
45+
{
46+
if (!string.IsNullOrEmpty(newContent))
47+
{
48+
_newContent[contentType].Add(newContent);
49+
}
50+
}
51+
52+
public void AddNewContent(string contentIdentifier, string newContent)
53+
{
54+
if (!string.IsNullOrEmpty(newContent))
55+
{
56+
if (!_unStructuredContent.ContainsKey(contentIdentifier))
57+
{
58+
_unStructuredContent.Add(contentIdentifier, new List<string>());
59+
}
60+
_unStructuredContent[contentIdentifier].Add(newContent);
61+
}
62+
}
63+
64+
public string RetrieveBlock(params NewContentType[] newContentTypes)
65+
{
66+
var block = string.Empty;
67+
foreach (var newContentType in newContentTypes)
68+
{
69+
var newContent = string.Join(_doubleSpace, _newContent[newContentType]);
70+
if (!string.IsNullOrEmpty(newContent))
71+
{
72+
block = string.IsNullOrEmpty(block)
73+
? newContent
74+
: $"{block}{_doubleSpace}{newContent}";
75+
}
76+
}
77+
return LimitNewLines(block.Trim(), NewLineLimit);
78+
}
79+
80+
public string RetrieveBlock(params string[] contentIdentifiers)
81+
{
82+
var block = string.Empty;
83+
foreach (var identifier in contentIdentifiers)
84+
{
85+
if (_unStructuredContent.TryGetValue(identifier, out var adHocContent))
86+
{
87+
var newContent = string.Join(_doubleSpace, adHocContent);
88+
if (!string.IsNullOrEmpty(newContent))
89+
{
90+
block = string.IsNullOrEmpty(block)
91+
? newContent
92+
: $"{block}{_doubleSpace}{newContent}";
93+
}
94+
}
95+
}
96+
97+
return string.IsNullOrEmpty(block)
98+
? null
99+
: LimitNewLines(block.Trim(), NewLineLimit);
100+
}
101+
102+
public int NewLineLimit { set; get; } = 2;
103+
104+
private static string LimitNewLines(string content, int maxConsecutiveNewlines = 2)
105+
{
106+
var target = string.Concat(Enumerable.Repeat(Environment.NewLine, maxConsecutiveNewlines + 1).ToList());
107+
var replacement = string.Concat(Enumerable.Repeat(Environment.NewLine, maxConsecutiveNewlines).ToList());
108+
var guard = 0;
109+
var maxAttempts = 100;
110+
while (++guard < maxAttempts && content.Contains(target))
111+
{
112+
content = content.Replace(target, replacement);
113+
}
114+
115+
if (guard >= maxAttempts)
116+
{
117+
throw new FormatException($"Unable to limit consecutive '{Environment.NewLine}' strings to {maxConsecutiveNewlines}");
118+
}
119+
return content;
120+
}
121+
}
122+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+

2+
namespace Rubberduck.Refactorings.EncapsulateField
3+
{
4+
public interface INewContentAggregatorFactory
5+
{
6+
INewContentAggregator Create();
7+
}
8+
9+
public class NewContentAggregatorFactory : INewContentAggregatorFactory
10+
{
11+
public INewContentAggregator Create()
12+
{
13+
return new NewContentAggregator();
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)