aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/TextBufferImpl.java
blob: 0a7ff26c73c602ec5d972600705296f78d339523 (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
// Copyright Vespa.ai. 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.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static com.yahoo.vespa.hosted.node.admin.task.util.editor.TextUtil.splitString;

/**
 * @author hakon
 */
public class TextBufferImpl implements TextBuffer {
    /** Invariant: {@code size() >= 1}. An empty text buffer {@code => [""]} */
    private final ArrayList<String> lines = new ArrayList<>();

    private Version version = new Version();

    TextBufferImpl() {
        lines.add("");
    }

    TextBufferImpl(String text) {
        this();
        write(getStartOfText(), text);
        // reset version
        version = new Version();
    }

    @Override
    public Version getVersion() {
        return version;
    }

    @Override
    public String getString() {
        return String.join("\n", lines);
    }

    @Override
    public int getMaxLineIndex() {
        return lines.size() - 1;
    }

    @Override
    public String getLine(int lineIndex) {
        return lines.get(lineIndex);
    }

    @Override
    public Position write(Position position, String text) {
        List<String> linesToInsert = new LinkedList<>(splitString(text, true, false));
        if (linesToInsert.isEmpty()) {
            return position;
        }

        // The position splits that line in two, and both prefix and suffix must be preserved
        linesToInsert.set(0, getLinePrefix(position) + linesToInsert.get(0));
        String lastLine = linesToInsert.get(linesToInsert.size() - 1);
        int endColumnIndex = lastLine.length();
        linesToInsert.set(linesToInsert.size() - 1, lastLine + getLineSuffix(position));

        // Set the first line at lineIndex, insert the rest.
        int lineIndex = position.lineIndex();
        int endLineIndex = lineIndex + linesToInsert.size() - 1;
        lines.set(lineIndex, linesToInsert.remove(0));
        lines.addAll(lineIndex + 1, linesToInsert);

        incrementVersion();

        return new Position(endLineIndex, endColumnIndex);
    }

    @Override
    public void clear() {
        lines.clear();
        lines.add("");
    }

    @Override
    public void delete(Position start, Position end) {
        if (start.isAfter(end)) {
            throw new IllegalArgumentException("start position " + start +
                    " is after end position " + end);
        }

        String prefix = getLinePrefix(start);
        String suffix = getLineSuffix(end);
        String stichedLine = prefix + suffix;

        lines.set(start.lineIndex(), stichedLine);

        deleteLines(start.lineIndex() + 1, end.lineIndex() + 1);

        incrementVersion();
    }

    private void deleteLines(int startIndex, int endIndex) {
        for (int fromIndex = endIndex, toIndex = startIndex; fromIndex <= getMaxLineIndex();
             ++toIndex, ++fromIndex) {
            lines.set(toIndex, lines.get(fromIndex));
        }

        truncate(getMaxLineIndex() - (endIndex - startIndex));
    }

    private void truncate(int newMaxLineIndex) {
        while (getMaxLineIndex() > newMaxLineIndex) {
            lines.remove(getMaxLineIndex());
        }
    }

    private void incrementVersion() {
        version = version.next();
    }
}