|
| 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 | +} |
0 commit comments