Skip to content

Commit 26a5195

Browse files
committed
Allow custom merge/replace semantics for TextPresentation ranges eclipse-platform#2543
TextPresentation performs at least two tasks when applying new StyleRanges: - identify which style ranges need creating/matching with new styles - merge or replace an individual style range Decouple the two tasks by exposing methods for applying style range(s) that accept a BiConsumer to customise how style ranges should be merged
1 parent 6a3eb35 commit 26a5195

File tree

1 file changed

+65
-55
lines changed

1 file changed

+65
-55
lines changed

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/TextPresentation.java

+65-55
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.Iterator;
1818
import java.util.NoSuchElementException;
19+
import java.util.function.BiConsumer;
1920

2021
import org.eclipse.swt.SWT;
2122
import org.eclipse.swt.custom.StyleRange;
@@ -233,7 +234,7 @@ public void addStyleRange(StyleRange range) {
233234
* @since 3.0
234235
*/
235236
public void replaceStyleRange(StyleRange range) {
236-
applyStyleRange(range, false);
237+
applyStyleRange(range, this::replaceStyle);
237238
}
238239

239240
/**
@@ -244,18 +245,20 @@ public void replaceStyleRange(StyleRange range) {
244245
* @since 3.0
245246
*/
246247
public void mergeStyleRange(StyleRange range) {
247-
applyStyleRange(range, true);
248+
applyStyleRange(range, this::mergeStyle);
248249
}
249250

250251
/**
251252
* Applies the given range to this presentation. The range must be a
252253
* subrange of the presentation's default range.
253254
*
254255
* @param range the range to be added
255-
* @param merge <code>true</code> if the style should be merged instead of replaced
256+
* @param styleRangeApplier method accepting a StyleRange template to be applied and a
257+
* StyleRange target, the style being applied to. The target may be modified by the
258+
* method
256259
* @since 3.0
257260
*/
258-
private void applyStyleRange(StyleRange range, boolean merge) {
261+
public void applyStyleRange(StyleRange range, BiConsumer<StyleRange, StyleRange> styleRangeApplier) {
259262
if (range.length == 0)
260263
return;
261264

@@ -272,7 +275,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
272275

273276
defaultRange.start= start;
274277
defaultRange.length= length;
275-
applyStyle(range, defaultRange, merge);
278+
styleRangeApplier.accept(range, defaultRange);
276279
fRanges.add(defaultRange);
277280
} else {
278281
IRegion rangeRegion= new Region(start, length);
@@ -284,7 +287,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
284287
defaultRange= range;
285288
defaultRange.start= start;
286289
defaultRange.length= length;
287-
applyStyle(range, defaultRange, merge);
290+
styleRangeApplier.accept(range, defaultRange);
288291
fRanges.add(defaultRange);
289292
return;
290293
}
@@ -313,14 +316,14 @@ private void applyStyleRange(StyleRange range, boolean merge) {
313316

314317
defaultRange.start= start;
315318
defaultRange.length= currentStart - start;
316-
applyStyle(range, defaultRange, merge);
319+
styleRangeApplier.accept(range, defaultRange);
317320
fRanges.add(i, defaultRange);
318321
i++; last++;
319322

320323

321324
// Apply style to first part of current range
322325
current.length= Math.min(end, currentEnd) - currentStart;
323-
applyStyle(range, current, merge);
326+
styleRangeApplier.accept(range, current);
324327
}
325328

326329
if (start >= currentStart) {
@@ -333,7 +336,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
333336
i++; last++;
334337
fRanges.add(i, current);
335338
}
336-
applyStyle(range, current, merge);
339+
styleRangeApplier.accept(range, current);
337340
current.start= start;
338341
current.length= Math.min(end, currentEnd) - start;
339342
}
@@ -359,7 +362,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
359362
defaultRange= range;
360363
defaultRange.start= start;
361364
defaultRange.length= end - start;
362-
applyStyle(range, defaultRange, merge);
365+
styleRangeApplier.accept(range, defaultRange);
363366
fRanges.add(last, defaultRange);
364367
}
365368
}
@@ -374,7 +377,7 @@ private void applyStyleRange(StyleRange range, boolean merge) {
374377
* @since 3.0
375378
*/
376379
public void replaceStyleRanges(StyleRange[] ranges) {
377-
applyStyleRanges(ranges, false);
380+
applyStyleRanges(ranges, this::replaceStyle);
378381
}
379382

380383
/**
@@ -386,7 +389,7 @@ public void replaceStyleRanges(StyleRange[] ranges) {
386389
* @since 3.0
387390
*/
388391
public void mergeStyleRanges(StyleRange[] ranges) {
389-
applyStyleRanges(ranges, true);
392+
applyStyleRanges(ranges, this::mergeStyle);
390393
}
391394

392395
/**
@@ -395,10 +398,12 @@ public void mergeStyleRanges(StyleRange[] ranges) {
395398
* by increasing offset and must not overlap (but may be adjacent).
396399
*
397400
* @param ranges the ranges to be added
398-
* @param merge <code>true</code> if the style should be merged instead of replaced
401+
* @param styleRangeApplier method accepting a StyleRange template to be applied and a
402+
* StyleRange target, the style being applied to. The target may be modified by the
403+
* method
399404
* @since 3.0
400405
*/
401-
private void applyStyleRanges(StyleRange[] ranges, boolean merge) {
406+
public void applyStyleRanges(StyleRange[] ranges, BiConsumer<StyleRange, StyleRange> styleRangeApplier) {
402407
int j= 0;
403408
ArrayList<StyleRange> oldRanges= fRanges;
404409
ArrayList<StyleRange> newRanges= new ArrayList<>(2*ranges.length + oldRanges.size());
@@ -407,67 +412,72 @@ private void applyStyleRanges(StyleRange[] ranges, boolean merge) {
407412
for (int m= getFirstIndexAfterWindow(new Region(range.start, range.length)); j < m; j++)
408413
newRanges.add(oldRanges.get(j));
409414
fRanges= newRanges; // for mergeStyleRange(...)
410-
applyStyleRange(range, merge);
415+
applyStyleRange(range, styleRangeApplier);
411416
}
412417
for (int m= oldRanges.size(); j < m; j++)
413418
newRanges.add(oldRanges.get(j));
414419
fRanges= newRanges;
415420
}
416421

417422
/**
418-
* Applies the template's style to the target.
423+
* Merges the template's style to the target.
419424
*
420425
* @param template the style range to be used as template
421426
* @param target the style range to which to apply the template
422-
* @param merge <code>true</code> if the style should be merged instead of replaced
423427
* @since 3.0
424428
*/
425-
private void applyStyle(StyleRange template, StyleRange target, boolean merge) {
426-
if (merge) {
427-
if (template.font != null)
428-
target.font= template.font;
429-
target.fontStyle|= template.fontStyle;
430-
431-
if (template.metrics != null)
432-
target.metrics= template.metrics;
429+
private void mergeStyle(StyleRange template, StyleRange target) {
430+
if (template.font != null)
431+
target.font= template.font;
432+
target.fontStyle|= template.fontStyle;
433433

434-
if (template.foreground != null || template.underlineStyle == SWT.UNDERLINE_LINK)
435-
target.foreground= template.foreground;
436-
if (template.background != null)
437-
target.background= template.background;
434+
if (template.metrics != null)
435+
target.metrics= template.metrics;
438436

439-
target.strikeout|= template.strikeout;
440-
if (template.strikeoutColor != null)
441-
target.strikeoutColor= template.strikeoutColor;
437+
if (template.foreground != null || template.underlineStyle == SWT.UNDERLINE_LINK)
438+
target.foreground= template.foreground;
439+
if (template.background != null)
440+
target.background= template.background;
442441

443-
target.underline|= template.underline;
444-
if (template.underlineStyle != SWT.NONE && target.underlineStyle != SWT.UNDERLINE_LINK)
445-
target.underlineStyle= template.underlineStyle;
442+
target.strikeout|= template.strikeout;
443+
if (template.strikeoutColor != null)
444+
target.strikeoutColor= template.strikeoutColor;
446445

447-
if (template.underlineColor != null)
448-
target.underlineColor= template.underlineColor;
446+
target.underline|= template.underline;
447+
if (template.underlineStyle != SWT.NONE && target.underlineStyle != SWT.UNDERLINE_LINK)
448+
target.underlineStyle= template.underlineStyle;
449449

450-
if (template.borderStyle != SWT.NONE)
451-
target.borderStyle= template.borderStyle;
452-
if (template.borderColor != null)
453-
target.borderColor= template.borderColor;
450+
if (template.underlineColor != null)
451+
target.underlineColor= template.underlineColor;
454452

455-
} else {
456-
target.font= template.font;
457-
target.fontStyle= template.fontStyle;
458-
target.metrics= template.metrics;
459-
target.foreground= template.foreground;
460-
target.background= template.background;
461-
target.strikeout= template.strikeout;
462-
target.strikeoutColor= template.strikeoutColor;
463-
target.underline= template.underline;
464-
target.underlineStyle= template.underlineStyle;
465-
target.underlineColor= template.underlineColor;
466-
target.borderStyle= template.borderStyle;
467-
target.borderColor= template.borderColor;
468-
}
453+
if (template.borderStyle != SWT.NONE)
454+
target.borderStyle= template.borderStyle;
455+
if (template.borderColor != null)
456+
target.borderColor= template.borderColor;
469457
}
470458

459+
/**
460+
* Replaces the target's style with the template.
461+
*
462+
* @param template the style range to be used as template
463+
* @param target the style range to which to apply the template
464+
* @since 3.0
465+
*/
466+
private void replaceStyle(StyleRange template, StyleRange target) {
467+
target.font= template.font;
468+
target.fontStyle= template.fontStyle;
469+
target.metrics= template.metrics;
470+
target.foreground= template.foreground;
471+
target.background= template.background;
472+
target.strikeout= template.strikeout;
473+
target.strikeoutColor= template.strikeoutColor;
474+
target.underline= template.underline;
475+
target.underlineStyle= template.underlineStyle;
476+
target.underlineColor= template.underlineColor;
477+
target.borderStyle= template.borderStyle;
478+
target.borderColor= template.borderColor;
479+
}
480+
471481
/**
472482
* Checks whether the given range is a subrange of the presentation's
473483
* default style range.

0 commit comments

Comments
 (0)