Skip to main content
summaryrefslogtreecommitdiffstats
blob: c0419d7e555cd1564eaf66c078e6fd04f8118c63 (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
package org.eclipse.fx.code.editor.configuration.text;

import java.util.function.IntPredicate;

import org.eclipse.fx.code.editor.configuration.Check;
import org.eclipse.fx.code.editor.configuration.Equals;
import org.eclipse.fx.code.editor.configuration.Range;
import org.eclipse.fx.core.function.IntRelationOperation;
import org.eclipse.fx.text.rules.ColumnStartRule;
import org.eclipse.fx.text.rules.PredicateColumnStartRule;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IRule;

@SuppressWarnings("restriction")
public class Util {
	public static IRule wrap(Check check, IRule rule) {
		if( check == null ) {
			return rule;
		} else {
			IntPredicate p = null;
			if( check instanceof Equals ) {
				int c = ((Equals) check).getValue();
				p = v -> v == c;
			} else if( check instanceof Range ) {
				Range r = (Range) check;
				if( r.getMin() != -1 && r.getMax() != -1 ) {
					int min = r.getMin();
					int max = r.getMax();
					IntRelationOperation minOp = r.isMinIncl() ? IntRelationOperation::lt : IntRelationOperation::lte;
					IntRelationOperation maxOp = r.isMaxIncl() ? IntRelationOperation::lt : IntRelationOperation::lte;
					p = v -> minOp.apply(min, v) && maxOp.apply(v,max);
				} else if( r.getMin() != -1 ) {
					int min = r.getMin();
					IntRelationOperation minOp = r.isMinIncl() ? IntRelationOperation::lt : IntRelationOperation::lte;
					p = v -> minOp.apply(min, v);
				} else if( r.getMax() != -1 ) {
					int max = r.getMax();
					IntRelationOperation maxOp = r.isMaxIncl() ? IntRelationOperation::lt : IntRelationOperation::lte;
					p = v -> maxOp.apply(v,max);
				}
			}

			if( p != null ) {
				return rule instanceof IPredicateRule ? new PredicateColumnStartRule((IPredicateRule) rule, p) : new ColumnStartRule(rule, p);
			} else {
				return rule;
			}
		}
	}
}

Back to the top