-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathParagraph.java
331 lines (281 loc) · 11.3 KB
/
Paragraph.java
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
package org.fxmisc.richtext;
import static org.fxmisc.richtext.TwoDimensional.Bias.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javafx.scene.control.IndexRange;
import org.fxmisc.richtext.TwoDimensional.Position;
public final class Paragraph<S, PS> implements CharSequence {
@SafeVarargs
private static <T> List<T> list(T head, T... tail) {
if(tail.length == 0) {
return Collections.singletonList(head);
} else {
ArrayList<T> list = new ArrayList<>(1 + tail.length);
list.add(head);
Collections.addAll(list, tail);
return list;
}
}
private final List<StyledText<S>> segments;
private final TwoLevelNavigator navigator;
private final PS paragraphStyle;
public Paragraph(PS paragraphStyle, String text, S style) {
this(paragraphStyle, new StyledText<>(text, style));
}
@SafeVarargs
public Paragraph(PS paragraphStyle, StyledText<S> text, StyledText<S>... texts) {
this(paragraphStyle, list(text, texts));
}
Paragraph(PS paragraphStyle, List<StyledText<S>> segments) {
assert !segments.isEmpty();
this.segments = segments;
this.paragraphStyle = paragraphStyle;
navigator = new TwoLevelNavigator(segments::size,
i -> segments.get(i).length());
}
public List<StyledText<S>> getSegments() {
return Collections.unmodifiableList(segments);
}
public PS getParagraphStyle() {
return paragraphStyle;
}
private int length = -1;
@Override
public int length() {
if(length == -1) {
length = segments.stream().mapToInt(StyledText::length).sum();
}
return length;
}
@Override
public char charAt(int index) {
Position pos = navigator.offsetToPosition(index, Forward);
return segments.get(pos.getMajor()).charAt(pos.getMinor());
}
public String substring(int from, int to) {
return toString().substring(from, Math.min(to, length()));
}
public String substring(int from) {
return toString().substring(from);
}
public Paragraph<S, PS> concat(Paragraph<S, PS> p) {
if(length() == 0) {
return p;
}
if(p.length() == 0) {
return this;
}
StyledText<S> left = segments.get(segments.size() - 1);
StyledText<S> right = p.segments.get(0);
if(Objects.equals(left.getStyle(), right.getStyle())) {
StyledText<S> segment = left.concat(right);
List<StyledText<S>> segs = new ArrayList<>(segments.size() + p.segments.size() - 1);
segs.addAll(segments.subList(0, segments.size()-1));
segs.add(segment);
segs.addAll(p.segments.subList(1, p.segments.size()));
return new Paragraph<>(paragraphStyle, segs);
} else {
List<StyledText<S>> segs = new ArrayList<>(segments.size() + p.segments.size());
segs.addAll(segments);
segs.addAll(p.segments);
return new Paragraph<>(paragraphStyle, segs);
}
}
public Paragraph<S, PS> concat(CharSequence str) {
if(str.length() == 0) {
return this;
}
List<StyledText<S>> segs = new ArrayList<>(segments);
int lastIdx = segments.size() - 1;
segs.set(lastIdx, segments.get(lastIdx).concat(str));
return new Paragraph<>(paragraphStyle, segs);
}
public Paragraph<S, PS> insert(int offset, CharSequence str) {
if(offset < 0 || offset > length()) {
throw new IndexOutOfBoundsException(String.valueOf(offset));
}
Position pos = navigator.offsetToPosition(offset, Backward);
int segIdx = pos.getMajor();
int segPos = pos.getMinor();
StyledText<S> seg = segments.get(segIdx);
StyledText<S> replacement = seg.spliced(segPos, segPos, str);
List<StyledText<S>> segs = new ArrayList<>(segments);
segs.set(segIdx, replacement);
return new Paragraph<>(paragraphStyle, segs);
}
@Override
public Paragraph<S, PS> subSequence(int start, int end) {
return trim(end).subSequence(start);
}
public Paragraph<S, PS> trim(int length) {
if(length >= length()) {
return this;
} else {
Position pos = navigator.offsetToPosition(length, Backward);
int segIdx = pos.getMajor();
List<StyledText<S>> segs = new ArrayList<>(segIdx + 1);
segs.addAll(segments.subList(0, segIdx));
segs.add(segments.get(segIdx).subSequence(0, pos.getMinor()));
return new Paragraph<>(paragraphStyle, segs);
}
}
public Paragraph<S, PS> subSequence(int start) {
if(start < 0) {
throw new IllegalArgumentException("start must not be negative (was: " + start + ")");
} else if(start == 0) {
return this;
} else if(start <= length()) {
Position pos = navigator.offsetToPosition(start, Forward);
int segIdx = pos.getMajor();
List<StyledText<S>> segs = new ArrayList<>(segments.size() - segIdx);
segs.add(segments.get(segIdx).subSequence(pos.getMinor()));
segs.addAll(segments.subList(segIdx + 1, segments.size()));
return new Paragraph<>(paragraphStyle, segs);
} else {
throw new IndexOutOfBoundsException(start + " not in [0, " + length() + "]");
}
}
public Paragraph<S, PS> delete(int start, int end) {
return trim(start).concat(subSequence(end));
}
public Paragraph<S, PS> restyle(S style) {
return new Paragraph<>(paragraphStyle, toString(), style);
}
public Paragraph<S, PS> restyle(int from, int to, S style) {
if(from >= length()) {
return this;
} else {
to = Math.min(to, length());
Paragraph<S, PS> left = subSequence(0, from);
Paragraph<S, PS> middle = new Paragraph<>(paragraphStyle, substring(from, to), style);
Paragraph<S, PS> right = subSequence(to);
return left.concat(middle).concat(right);
}
}
public Paragraph<S, PS> restyle(int from, StyleSpans<? extends S> styleSpans) {
int len = styleSpans.length();
if(styleSpans.equals(getStyleSpans(from, from + len))) {
return this;
}
Paragraph<S, PS> left = trim(from);
Paragraph<S, PS> right = subSequence(from + len);
String middleString = substring(from, from + len);
List<StyledText<S>> middleSegs = new ArrayList<>(styleSpans.getSpanCount());
int offset = 0;
for(StyleSpan<? extends S> span: styleSpans) {
int end = offset + span.getLength();
String text = middleString.substring(offset, end);
middleSegs.add(new StyledText<>(text, span.getStyle()));
offset = end;
}
Paragraph<S, PS> middle = new Paragraph<>(paragraphStyle, middleSegs);
return left.concat(middle).concat(right);
}
public Paragraph<S, PS> setParagraphStyle(PS paragraphStyle) {
return new Paragraph<>(paragraphStyle, segments);
}
/**
* Returns the style of character with the given index.
* If {@code charIdx < 0}, returns the style at the beginning of this paragraph.
* If {@code charIdx >= this.length()}, returns the style at the end of this paragraph.
*/
public S getStyleOfChar(int charIdx) {
if(charIdx < 0) {
return segments.get(0).getStyle();
}
Position pos = navigator.offsetToPosition(charIdx, Forward);
return segments.get(pos.getMajor()).getStyle();
}
/**
* Returns the style at the given position. That is the style of the
* character immediately preceding {@code position}. If {@code position}
* is 0, then the style of the first character (index 0) in this paragraph
* is returned. If this paragraph is empty, then some style previously used
* in this paragraph is returned.
* If {@code position > this.length()}, then it is equivalent to
* {@code position == this.length()}.
*
* <p>In other words, {@code getStyleAtPosition(p)} is equivalent to
* {@code getStyleOfChar(p-1)}.
*/
public S getStyleAtPosition(int position) {
if(position < 0) {
throw new IllegalArgumentException("Paragraph position cannot be negative (" + position + ")");
}
Position pos = navigator.offsetToPosition(position, Backward);
return segments.get(pos.getMajor()).getStyle();
}
/**
* Returns the range of homogeneous style that includes the given position.
* If {@code position} points to a boundary between two styled ranges,
* then the range preceding {@code position} is returned.
*/
public IndexRange getStyleRangeAtPosition(int position) {
Position pos = navigator.offsetToPosition(position, Backward);
int start = position - pos.getMinor();
int end = start + segments.get(pos.getMajor()).length();
return new IndexRange(start, end);
}
public StyleSpans<S> getStyleSpans() {
StyleSpansBuilder<S> builder = new StyleSpansBuilder<>(segments.size());
for(StyledText<S> seg: segments) {
builder.add(seg.getStyle(), seg.length());
}
return builder.create();
}
public StyleSpans<S> getStyleSpans(int from, int to) {
Position start = navigator.offsetToPosition(from, Forward);
Position end = to == from
? start
: start.offsetBy(to - from, Backward);
int startSegIdx = start.getMajor();
int endSegIdx = end.getMajor();
int n = endSegIdx - startSegIdx + 1;
StyleSpansBuilder<S> builder = new StyleSpansBuilder<>(n);
if(startSegIdx == endSegIdx) {
StyledText<S> seg = segments.get(startSegIdx);
builder.add(seg.getStyle(), to - from);
} else {
StyledText<S> startSeg = segments.get(startSegIdx);
builder.add(startSeg.getStyle(), startSeg.length() - start.getMinor());
for(int i = startSegIdx + 1; i < endSegIdx; ++i) {
StyledText<S> seg = segments.get(i);
builder.add(seg.getStyle(), seg.length());
}
StyledText<S> endSeg = segments.get(endSegIdx);
builder.add(endSeg.getStyle(), end.getMinor());
}
return builder.create();
}
private String text = null;
/**
* Returns the string content of this paragraph,
* excluding the line terminator.
*/
@Override
public String toString() {
if(text == null) {
StringBuilder sb = new StringBuilder(length());
for(StyledText<S> seg: segments)
sb.append(seg);
text = sb.toString();
}
return text;
}
@Override
public boolean equals(Object other) {
if(other instanceof Paragraph) {
Paragraph<?, ?> that = (Paragraph<?, ?>) other;
return Objects.equals(this.paragraphStyle, that.paragraphStyle)
&& Objects.equals(this.segments, that.segments);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(paragraphStyle, segments);
}
}