aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/editor/TextBufferImpl.java
blob: 0c9d80db055d06ac8696ff42b96258aa498309dd (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
// Copyright 2018 Yahoo Holdings. 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.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 {
    private final LinkedList<String> lines = new LinkedList<>();

    private Version version = new Version();

    TextBufferImpl() {
        // Invariant about always size() >= 0, and an empty text buffer => [""]
        lines.add("");
    }

    TextBufferImpl(String text) {
        lines.add("");
        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 = splitString(text, true, false);
        if (linesToInsert.isEmpty()) {
            return position;
        }

        int lineIndex = position.lineIndex();

        String prefix = getLinePrefix(position);
        linesToInsert.set(0, prefix + linesToInsert.get(0));

        // Insert the current prefix to the first line
        int numberOfLinesToInsert = linesToInsert.size() - 1; // 1 will be set, the rest inserted
        String prefixOfCursorAfterwards = linesToInsert.get(numberOfLinesToInsert);

        // Append the current suffix to the last line
        String suffix = getLineSuffix(position);
        String lastLine = prefixOfCursorAfterwards + suffix;
        linesToInsert.set(numberOfLinesToInsert, lastLine);

        // The first line is overwritten with set()
        lines.set(lineIndex, linesToInsert.remove(0));

        // The following lines must be inserted
        lines.addAll(lineIndex + 1, linesToInsert);

        incrementVersion();

        return new Position(lineIndex + linesToInsert.size(), prefixOfCursorAfterwards.length());
    }

    @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) {
        int fromIndex = endIndex;
        for (int 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();
    }
}