@Override
    public void fromString(String text) {
        String[] new_lines = text.split(eol);
        int n_old = size();
        int n_new = new_lines.length;
        // Replace common lines
        for (int i = 0; i < Math.min(n_old, n_new); i++) {
            lines.get(i).setText(new_lines[i]);
        }
        // Remove extra lines from the old version
        for (int i = n_new; i < n_old; i++) {
            lines.remove(n_new);
        }
        // Add new lines for the new version
        for (int i = n_old; i < n_new; i++) {
            lines.add(i, new Line(new_lines[i]));
        }
        // Shift to the last line if currently out of the document
        if (currentLineNumber >= n_new) {
            selectLine(n_new - 1);
        }
    }