|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Globalization; |
| 5 | +using System.Linq; |
| 6 | +using System.Management.Automation.Language; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions; |
| 9 | + |
| 10 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A class to represent text to which `TextEdit`s can be applied. |
| 14 | + /// </summary> |
| 15 | + public class EditableText |
| 16 | + { |
| 17 | + private TextLines lines { get; set; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// The text that is available for editing. |
| 21 | + /// </summary> |
| 22 | + public string Text { get { return String.Join(NewLine, lines); } } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// The lines in the Text. |
| 26 | + /// </summary> |
| 27 | + public string[] Lines { get { return lines.ToArray(); } } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The new line character in the Text. |
| 31 | + /// </summary> |
| 32 | + public string NewLine { get; private set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Construct an EditableText type object. |
| 36 | + /// </summary> |
| 37 | + /// <param name="text"></param> |
| 38 | + public EditableText(string text) |
| 39 | + { |
| 40 | + if (text == null) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(text)); |
| 43 | + } |
| 44 | + |
| 45 | + string[] lines; |
| 46 | + NewLine = GetNewLineCharacters(text, out lines); |
| 47 | + this.lines = new TextLines(lines); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Apply edits defined by a TextEdit object to Text. |
| 52 | + /// </summary> |
| 53 | + /// <param name="textEdit">A TextEdit object that encapsulates the text and the range that need to be replaced.</param> |
| 54 | + /// <returns>An editable object which contains the supplied edit.</returns> |
| 55 | + public EditableText ApplyEdit(TextEdit textEdit) |
| 56 | + { |
| 57 | + ValidateTextEdit(textEdit); |
| 58 | + |
| 59 | + var editLines = textEdit.Lines; |
| 60 | + |
| 61 | + // Get the first fragment of the first line |
| 62 | + string firstLineFragment = |
| 63 | + lines[textEdit.StartLineNumber - 1] |
| 64 | + .Substring(0, textEdit.StartColumnNumber - 1); |
| 65 | + |
| 66 | + // Get the last fragment of the last line |
| 67 | + string endLine = lines[textEdit.EndLineNumber - 1]; |
| 68 | + string lastLineFragment = |
| 69 | + endLine.Substring( |
| 70 | + textEdit.EndColumnNumber - 1, |
| 71 | + lines[textEdit.EndLineNumber - 1].Length - textEdit.EndColumnNumber + 1); |
| 72 | + |
| 73 | + // Remove the old lines |
| 74 | + for (int i = 0; i <= textEdit.EndLineNumber - textEdit.StartLineNumber; i++) |
| 75 | + { |
| 76 | + lines.RemoveAt(textEdit.StartLineNumber - 1); |
| 77 | + } |
| 78 | + |
| 79 | + // Build and insert the new lines |
| 80 | + int currentLineNumber = textEdit.StartLineNumber; |
| 81 | + for (int changeIndex = 0; changeIndex < editLines.Length; changeIndex++) |
| 82 | + { |
| 83 | + // Since we split the lines above using \n, make sure to |
| 84 | + // trim the ending \r's off as well. |
| 85 | + string finalLine = editLines[changeIndex].TrimEnd('\r'); |
| 86 | + |
| 87 | + // Should we add first or last line fragments? |
| 88 | + if (changeIndex == 0) |
| 89 | + { |
| 90 | + // Append the first line fragment |
| 91 | + finalLine = firstLineFragment + finalLine; |
| 92 | + } |
| 93 | + if (changeIndex == editLines.Length - 1) |
| 94 | + { |
| 95 | + // Append the last line fragment |
| 96 | + finalLine = finalLine + lastLineFragment; |
| 97 | + } |
| 98 | + |
| 99 | + lines.Insert(currentLineNumber - 1, finalLine); |
| 100 | + currentLineNumber++; |
| 101 | + } |
| 102 | + |
| 103 | + return new EditableText(String.Join(NewLine, lines)); |
| 104 | + } |
| 105 | + |
| 106 | + // TODO Add a method that takes multiple edits, checks if they are unique and applies them. |
| 107 | + |
| 108 | + public override string ToString() |
| 109 | + { |
| 110 | + return Text; |
| 111 | + } |
| 112 | + |
| 113 | + private void ValidateTextEdit(TextEdit textEdit) |
| 114 | + { |
| 115 | + if (textEdit == null) |
| 116 | + { |
| 117 | + throw new NullReferenceException(nameof(textEdit)); |
| 118 | + } |
| 119 | + |
| 120 | + ValidateTextEditExtent(textEdit); |
| 121 | + } |
| 122 | + |
| 123 | + private void ValidateTextEditExtent(TextEdit textEdit) |
| 124 | + { |
| 125 | + if (textEdit.StartLineNumber > Lines.Length |
| 126 | + || textEdit.EndLineNumber > Lines.Length |
| 127 | + || textEdit.StartColumnNumber > Lines[textEdit.StartLineNumber - 1].Length |
| 128 | + || textEdit.EndColumnNumber > Lines[textEdit.EndLineNumber - 1].Length + 1) |
| 129 | + { |
| 130 | + throw new ArgumentException(String.Format( |
| 131 | + CultureInfo.CurrentCulture, |
| 132 | + Strings.EditableTextRangeIsNotContained)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static string GetNewLineCharacters(string text, out string[] lines) |
| 137 | + { |
| 138 | + int numNewLineChars = GetNumNewLineCharacters(text, out lines); |
| 139 | + if (lines.Length == 1) |
| 140 | + { |
| 141 | + return Environment.NewLine; |
| 142 | + } |
| 143 | + |
| 144 | + return text.Substring(lines[0].Length, numNewLineChars); |
| 145 | + } |
| 146 | + |
| 147 | + private static int GetNumNewLineCharacters(string text, out string[] lines) |
| 148 | + { |
| 149 | + lines = text.GetLines().ToArray(); |
| 150 | + if (lines.Length == 1) |
| 151 | + { |
| 152 | + return Environment.NewLine.Length; |
| 153 | + } |
| 154 | + |
| 155 | + var charsInLines = lines.Sum(line => line.Length); |
| 156 | + var numCharDiff = text.Length - charsInLines; |
| 157 | + int remainder = numCharDiff % (lines.Length - 1); |
| 158 | + if (remainder != 0) |
| 159 | + { |
| 160 | + throw new ArgumentException( |
| 161 | + String.Format(CultureInfo.CurrentCulture, Strings.EditableTextInvalidLineEnding), |
| 162 | + nameof(text)); |
| 163 | + } |
| 164 | + |
| 165 | + return numCharDiff / (lines.Length - 1); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments