|
22 | 22 | import com.google.gson.JsonPrimitive; |
23 | 23 | import java.util.ArrayList; |
24 | 24 | import java.util.List; |
| 25 | +import java.util.regex.Pattern; |
25 | 26 | import jdk.jshell.SourceCodeAnalysis; |
26 | 27 | import org.eclipse.lsp4j.Position; |
| 28 | +import org.netbeans.api.annotations.common.NonNull; |
27 | 29 |
|
28 | 30 | /** |
29 | 31 | * |
30 | 32 | * @author atalati |
31 | 33 | */ |
32 | 34 | public class NotebookUtils { |
| 35 | + private static final Pattern LINE_ENDINGS = Pattern.compile("\\R"); |
33 | 36 |
|
34 | 37 | public static String normalizeLineEndings(String text) { |
35 | | - if (text == null) { |
36 | | - return null; |
37 | | - } |
38 | | - |
39 | | - if (text.indexOf('\r') == -1) { |
40 | | - return text; |
41 | | - } |
42 | | - |
43 | | - StringBuilder normalized = new StringBuilder(text.length()); |
44 | | - int len = text.length(); |
45 | | - |
46 | | - for (int i = 0; i < len; i++) { |
47 | | - char c = text.charAt(i); |
48 | | - if (c == '\r') { |
49 | | - if (i + 1 < len && text.charAt(i + 1) == '\n') { |
50 | | - i++; |
51 | | - } |
52 | | - normalized.append('\n'); |
53 | | - } else { |
54 | | - normalized.append(c); |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - return normalized.toString(); |
| 38 | + return text == null ? null : LINE_ENDINGS.matcher(text).replaceAll("\n"); |
59 | 39 | } |
60 | 40 |
|
61 | 41 | public static int getOffset(String content, Position position) { |
62 | 42 | if (content == null || position == null) { |
63 | 43 | return 0; |
64 | 44 | } |
65 | 45 |
|
66 | | - String[] lines = content.split("\n", -1); |
67 | | - int offset = 0; |
68 | 46 | int targetLine = position.getLine(); |
69 | | - int targetChar = position.getCharacter(); |
70 | | - |
71 | 47 | if (targetLine < 0) { |
72 | 48 | return 0; |
73 | 49 | } |
74 | | - if (targetLine >= lines.length) { |
75 | | - return content.length(); |
76 | | - } |
| 50 | + int targetChar = Math.max(0, position.getCharacter()); |
77 | 51 |
|
78 | | - for (int i = 0; i < targetLine; i++) { |
79 | | - offset += lines[i].length() + 1; |
80 | | - } |
| 52 | + int lineStartIndex = -1; |
| 53 | + int lineEndIndex = -1; |
| 54 | + int line = -1; |
81 | 55 |
|
82 | | - String currentLine = lines[targetLine]; |
83 | | - int charPosition = Math.min(Math.max(targetChar, 0), currentLine.length()); |
84 | | - offset += charPosition; |
| 56 | + // find the start line in content |
| 57 | + do { |
| 58 | + lineStartIndex = lineEndIndex + 1; |
| 59 | + lineEndIndex = content.indexOf('\n', lineStartIndex); |
| 60 | + line++; |
| 61 | + } while (line < targetLine && lineEndIndex >= 0); |
85 | 62 |
|
86 | | - return Math.min(offset, content.length()); |
| 63 | + return line < targetLine ? content.length() : Math.min(lineStartIndex + targetChar, lineEndIndex < 0 ? content.length() : lineEndIndex); |
87 | 64 | } |
88 | 65 |
|
89 | | - public static Position getPosition(String content, int offset) { |
90 | | - if (content == null || offset <= 0) { |
| 66 | + public static Position getPosition(String text, int offset) { |
| 67 | + if (text == null || offset < 0) { |
91 | 68 | return new Position(0, 0); |
92 | 69 | } |
93 | 70 |
|
94 | | - int clampedOffset = Math.min(offset, content.length()); |
95 | | - |
96 | | - String textUpToOffset = content.substring(0, clampedOffset); |
97 | | - String[] lines = textUpToOffset.split("\n", -1); |
98 | | - |
99 | | - int line = lines.length - 1; |
100 | | - int character = lines[line].length(); |
101 | | - |
102 | | - return new Position(line, character); |
| 71 | + offset = Math.min(offset, text.length()); |
| 72 | + int lineStartIndex = -1; |
| 73 | + int lineEndIndex = -1; |
| 74 | + int line = -1; |
| 75 | + |
| 76 | + // count line endings in content upto offset |
| 77 | + do { |
| 78 | + lineStartIndex = lineEndIndex + 1; |
| 79 | + lineEndIndex = text.indexOf('\n', lineStartIndex); |
| 80 | + line++; |
| 81 | + } while (lineEndIndex >= 0 && offset > lineEndIndex); |
| 82 | + |
| 83 | + if (offset == lineEndIndex) { |
| 84 | + return new Position(line + 1, 0); |
| 85 | + } else { |
| 86 | + return new Position(line, offset - lineStartIndex); |
| 87 | + } |
103 | 88 | } |
104 | 89 |
|
105 | 90 | public static boolean checkEmptyString(String input) { |
@@ -158,4 +143,76 @@ public static List<String> getCodeSnippets(SourceCodeAnalysis analysis, String c |
158 | 143 |
|
159 | 144 | return codeSnippets; |
160 | 145 | } |
| 146 | + |
| 147 | + /** |
| 148 | + * Applies the supplied change, that is encoded as a diff |
| 149 | + * i.e. `{range-start, range-end, text-replacement}`, to the supplied text. |
| 150 | + * |
| 151 | + * This diff format can encode additions, deletions and modifications at a |
| 152 | + * single range in the text. |
| 153 | + * |
| 154 | + * The supplied text is expected to contain normalized line endings, and, the |
| 155 | + * new text adheres to the line ending normalization. |
| 156 | + * |
| 157 | + * @param text existing text |
| 158 | + * @param start start of the range of replaced text |
| 159 | + * @param end end of the range of replaced text |
| 160 | + * @param replacement text to be added at the supplied position in text |
| 161 | + * @throws IllegalArgumentException - when the supplied diff range is invalid |
| 162 | + */ |
| 163 | + public static String applyChange(@NonNull String text, @NonNull Position start, @NonNull Position end, @NonNull String replacement) throws IllegalArgumentException { |
| 164 | + int startLine = start.getLine(); |
| 165 | + int startLineOffset = start.getCharacter(); |
| 166 | + int endLine = end.getLine(); |
| 167 | + int endLineOffset = end.getCharacter(); |
| 168 | + |
| 169 | + if (startLine < 0 || endLine < startLine || (endLine == startLine && endLineOffset < startLineOffset)) { |
| 170 | + throw new IllegalArgumentException("Invalid range positions"); |
| 171 | + } |
| 172 | + |
| 173 | + if (replacement.length() == 0 && startLine == endLine && startLineOffset == endLineOffset) { |
| 174 | + return text; // Nothing to be done; no addition nor deletion |
| 175 | + } |
| 176 | + |
| 177 | + final int textLength = text.length(); |
| 178 | + |
| 179 | + int lineStartIndex = -1; |
| 180 | + int lineEndIndex = -1; |
| 181 | + int line = -1; |
| 182 | + |
| 183 | + // find the start line in content |
| 184 | + do { |
| 185 | + lineStartIndex = lineEndIndex + 1; |
| 186 | + lineEndIndex = text.indexOf('\n', lineStartIndex); |
| 187 | + line++; |
| 188 | + } while (line < startLine && lineEndIndex >= 0); |
| 189 | + |
| 190 | + if (line < startLine) { |
| 191 | + throw new IllegalArgumentException("Invalid range start out of bounds"); |
| 192 | + } |
| 193 | + |
| 194 | + StringBuilder result = new StringBuilder(textLength + replacement.length()); |
| 195 | + |
| 196 | + // append content before the change |
| 197 | + result.append(text, 0, Math.min(lineStartIndex + startLineOffset, lineEndIndex < 0 ? textLength : lineEndIndex)); |
| 198 | + // append added text, with line ending normalization |
| 199 | + result.append(LINE_ENDINGS.matcher(replacement).replaceAll("\n")); |
| 200 | + |
| 201 | + // find the end line in content |
| 202 | + while (line < endLine && lineEndIndex >= 0) { |
| 203 | + lineStartIndex = lineEndIndex + 1; |
| 204 | + lineEndIndex = text.indexOf('\n', lineStartIndex); |
| 205 | + line++; |
| 206 | + } |
| 207 | + |
| 208 | + if (line < endLine) { |
| 209 | + throw new IllegalArgumentException("Invalid range end out of bounds"); |
| 210 | + } |
| 211 | + |
| 212 | + if (lineStartIndex >= 0) { |
| 213 | + // append content after the change |
| 214 | + result.append(text, Math.min(lineStartIndex + endLineOffset, lineEndIndex < 0 ? textLength : lineEndIndex), textLength); |
| 215 | + } |
| 216 | + return result.toString(); |
| 217 | + } |
161 | 218 | } |
0 commit comments