aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/CursorImpl.java
blob: 88b406ff15a821cfe5bdddd4e7f74b09fbc59f43 (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.editor;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;

import static com.yahoo.collections.Comparables.max;
import static com.yahoo.collections.Comparables.min;

/**
 * @author hakon
 */
public class CursorImpl implements Cursor {
    private final TextBuffer textBuffer;
    private final Object unique = new Object();

    private Position position;

    /**
     * Creates a cursor to a text buffer.
     *
     * WARNING: The text buffer MUST NOT be accessed outside this cursor. This cursor
     * takes sole ownership of the text buffer.
     *
     * @param textBuffer the text buffer this cursor owns and operates on
     */
    CursorImpl(TextBuffer textBuffer) {
        this.textBuffer = textBuffer;
        position = textBuffer.getStartOfText();
    }

    @Override
    public Position getPosition() {
        return position;
    }

    @Override
    public Mark createMark() {
        return new Mark(position, textBuffer.getVersion(), unique);
    }

    @Override
    public String getBufferText() {
        return textBuffer.getString();
    }

    @Override
    public String getLine() {
        return textBuffer.getLine(position);
    }

    @Override
    public String getPrefix() {
        return textBuffer.getLinePrefix(position);
    }

    @Override
    public String getSuffix() {
        return textBuffer.getLineSuffix(position);
    }

    @Override
    public String getTextTo(Mark mark) {
        validateMark(mark);

        Position start = min(mark.position(), position);
        Position end = max(mark.position(), position);

        return textBuffer.getSubstring(start, end);
    }

    @Override
    public Cursor moveToStartOfBuffer() {
        position = textBuffer.getStartOfText();
        return this;
    }

    @Override
    public Cursor moveToEndOfBuffer() {
        position = textBuffer.getEndOfText();
        return this;
    }

    @Override
    public Cursor moveToStartOfLine() {
        position = textBuffer.getStartOfLine(position);
        return this;
    }

    @Override
    public Cursor moveToStartOfPreviousLine() {
        position = textBuffer.getStartOfPreviousLine(position);
        return this;
    }

    @Override
    public Cursor moveToStartOfNextLine() {
        position = textBuffer.getStartOfNextLine(position);
        return this;
    }

    @Override
    public Cursor moveToStartOf(int lineIndex) {
        validateLineIndex(lineIndex);
        position = new Position(lineIndex, 0);
        return this;
    }

    @Override
    public Cursor moveToEndOfLine() {
        position = textBuffer.getEndOfLine(position);
        return this;
    }

    @Override
    public Cursor moveToEndOfPreviousLine() {
        return moveToStartOfPreviousLine().moveToEndOfLine();
    }

    @Override
    public Cursor moveToEndOfNextLine() {
        return moveToStartOfNextLine().moveToEndOfLine();
    }

    @Override
    public Cursor moveToEndOf(int lineIndex) {
        return moveToStartOf(lineIndex).moveToEndOfLine();
    }

    @Override
    public Cursor moveForward() {
        return moveForward(1);
    }

    @Override
    public Cursor moveForward(int times) {
        position = textBuffer.forward(position, times);
        return this;
    }

    @Override
    public Cursor moveBackward() {
        return moveBackward(1);
    }

    @Override
    public Cursor moveBackward(int times) {
        position = textBuffer.backward(position, times);
        return this;
    }

    @Override
    public Cursor moveTo(Mark mark) {
        validateMark(mark);
        position = mark.position();
        return this;
    }

    @Override
    public boolean skipBackward(String text) {
        String prefix = getPrefix();
        if (prefix.endsWith(text)) {
            position = new Position(position.lineIndex(), position.columnIndex() - text.length());
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean skipForward(String text) {
        String suffix = getSuffix();
        if (suffix.startsWith(text)) {
            position = new Position(position.lineIndex(), position.columnIndex() + text.length());
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Optional<Match> moveForwardToStartOfMatch(Pattern pattern) {
        return moveForwardToXOfMatch(pattern, match -> position = match.startOfMatch());
    }

    @Override
    public Optional<Match> moveForwardToEndOfMatch(Pattern pattern) {
        return moveForwardToXOfMatch(pattern, match -> position = match.endOfMatch());
    }

    private Optional<Match> moveForwardToXOfMatch(Pattern pattern, Consumer<Match> callback) {
        Optional<Match> match = textBuffer.findForward(position, pattern);
        match.ifPresent(callback);
        return match;
    }

    @Override
    public Cursor moveTo(Position position) {
        validatePosition(position);
        this.position = position;
        return this;
    }

    @Override
    public Cursor moveTo(int lineIndex, int columnIndex) {
        return moveTo(new Position(lineIndex, columnIndex));
    }

    @Override
    public Cursor write(String text) {
        position = textBuffer.write(position, text);
        return this;
    }

    @Override
    public Cursor writeLine(String line) {
        return write(line).write("\n");
    }

    @Override
    public Cursor writeLines(String... lines) {
        return writeLines(List.of(lines));
    }

    @Override
    public Cursor writeLines(Iterable<String> lines) {
        return writeLine(String.join("\n", lines));
    }

    @Override
    public Cursor writeNewline() {
        return write("\n");
    }

    @Override
    public Cursor writeNewlineAfter() {
        return writeNewline().moveBackward();
    }

    @Override
    public Cursor deleteAll() {
        moveToStartOfBuffer();
        textBuffer.clear();
        return this;
    }

    @Override
    public Cursor deleteLine() {
        moveToStartOfLine();
        textBuffer.delete(position, textBuffer.getStartOfNextLine(position));
        return this;
    }

    @Override
    public Cursor deletePrefix() {
        Position originalPosition = position;
        moveToStartOfLine();
        textBuffer.delete(position, originalPosition);
        return this;
    }

    @Override
    public Cursor deleteSuffix() {
        textBuffer.delete(position, textBuffer.getEndOfLine(position));
        return this;
    }

    @Override
    public Cursor deleteForward() {
        return deleteForward(1);
    }

    @Override
    public Cursor deleteForward(int times) {
        Position end = textBuffer.forward(position, times);
        textBuffer.delete(position, end);
        return this;
    }

    @Override
    public Cursor deleteBackward() {
        return deleteBackward(1);
    }

    @Override
    public Cursor deleteBackward(int times) {
        Position end = position;
        moveBackward(times);
        textBuffer.delete(position, end);
        return this;
    }

    @Override
    public Cursor deleteTo(Mark mark) {
        validateMark(mark);
        Position start = min(mark.position(), position);
        Position end = max(mark.position(), position);

        textBuffer.delete(start, end);
        return this;
    }

    @Override
    public boolean replaceMatch(Pattern pattern, Function<Match, String> replacer) {
        Optional<Match> match = moveForwardToStartOfMatch(pattern);
        if (match.isEmpty()) {
            return false;
        }

        textBuffer.delete(match.get().startOfMatch(), match.get().endOfMatch());
        write(replacer.apply(match.get()));
        return true;
    }

    @Override
    public int replaceMatches(Pattern pattern, Function<Match, String> replacer) {
        int count = 0;

        for (; replaceMatch(pattern, replacer); ++count) {
            // empty
        }

        return count;
    }

    private void validatePosition(Position position) {
        validateLineIndex(position.lineIndex());

        int maxColumnIndex = textBuffer.getLine(position.lineIndex()).length();
        if (position.columnIndex() < 0 || position.columnIndex() > maxColumnIndex) {
            throw new IndexOutOfBoundsException("Column index of " + position.coordinateString() +
                    " is not in permitted range [0," + maxColumnIndex + "]");
        }
    }

    private void validateLineIndex(int lineIndex) {
        int maxLineIndex = textBuffer.getMaxLineIndex();
        if (lineIndex < 0 || lineIndex > maxLineIndex) {
            throw new IndexOutOfBoundsException("Line index " + lineIndex +
                    " not in permitted range [0," + maxLineIndex + "]");
        }
    }

    private void validateMark(Mark mark) {
        if (mark.secret() != unique) {
            throw new IllegalArgumentException("Unknown mark " + mark);
        }

        if (!mark.version().equals(textBuffer.getVersion())) {
            throw new IllegalArgumentException("Mark " + mark + " is outdated");
        }
    }
}