Skip to content

Commit 4abf9df

Browse files
committed
Adds missing class from IntelliJ
1 parent ab2dac6 commit 4abf9df

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2000-2014 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.intellij.util.text;
17+
18+
import com.intellij.openapi.util.Segment;
19+
import com.intellij.openapi.util.TextRange;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import java.util.ArrayList;
23+
import java.util.Collections;
24+
import java.util.Comparator;
25+
import java.util.List;
26+
27+
/**
28+
* @author Rustam Vishnyakov
29+
*/
30+
public class TextRangeUtil {
31+
32+
public static final Comparator<TextRange> RANGE_COMPARATOR = new Comparator<TextRange>() {
33+
@Override
34+
public int compare(TextRange range1, TextRange range2) {
35+
int startOffsetDiff = range1.getStartOffset() - range2.getStartOffset();
36+
return startOffsetDiff != 0 ? startOffsetDiff : range1.getEndOffset() - range2.getEndOffset();
37+
}
38+
};
39+
40+
private TextRangeUtil() {
41+
}
42+
43+
/**
44+
* Excludes ranges from the original range. For example, if the original range is [30..100] and ranges to exclude are
45+
* [20..50] and [60..90], resulting ranges will be [50..60] and [90..100]. The ranges may overlap and follow in any order. In the latter
46+
* case the original list of excluded ranges is sorted by start/end offset.
47+
*
48+
* @param original The original range to exclude the ranges from.
49+
* @param excludedRanges The list of ranges to exclude.
50+
* @return A list of ranges after excluded ranges have been applied.
51+
*/
52+
public static Iterable<TextRange> excludeRanges(@NotNull TextRange original, @NotNull List<TextRange> excludedRanges) {
53+
if (!excludedRanges.isEmpty()) {
54+
if (excludedRanges.size() > 1) {
55+
Collections.sort(excludedRanges, RANGE_COMPARATOR);
56+
}
57+
int enabledRangeStart = original.getStartOffset();
58+
List<TextRange> enabledRanges = new ArrayList<TextRange>();
59+
for (TextRange excludedRange : excludedRanges) {
60+
if (excludedRange.getEndOffset() < enabledRangeStart) continue;
61+
int excludedRangeStart = excludedRange.getStartOffset();
62+
if (excludedRangeStart > original.getEndOffset()) break;
63+
if (excludedRangeStart > enabledRangeStart) {
64+
enabledRanges.add(new TextRange(enabledRangeStart, excludedRangeStart));
65+
}
66+
enabledRangeStart = excludedRange.getEndOffset();
67+
}
68+
if (enabledRangeStart < original.getEndOffset()) {
69+
enabledRanges.add(new TextRange(enabledRangeStart, original.getEndOffset()));
70+
}
71+
return enabledRanges;
72+
}
73+
return Collections.singletonList(original);
74+
}
75+
76+
/**
77+
* Return least text range that contains all of passed text ranges.
78+
* For example for {[0, 3],[3, 7],[10, 17]} this method will return [0, 17]
79+
* @param textRanges The list of ranges to process
80+
* @return least text range that contains all of passed text ranges
81+
*/
82+
@NotNull
83+
public static TextRange getEnclosingTextRange(@NotNull List<TextRange> textRanges) {
84+
if(textRanges.isEmpty())
85+
return TextRange.EMPTY_RANGE;
86+
int lowerBound = textRanges.get(0).getStartOffset();
87+
int upperBound = textRanges.get(0).getEndOffset();
88+
for(int i = 1; i < textRanges.size(); ++i) {
89+
TextRange textRange = textRanges.get(i);
90+
lowerBound = Math.min(lowerBound, textRange.getStartOffset());
91+
upperBound = Math.max(upperBound, textRange.getEndOffset());
92+
}
93+
return new TextRange(lowerBound, upperBound);
94+
}
95+
96+
public static int getDistance(@NotNull Segment r2, @NotNull Segment r1) {
97+
int s1 = r1.getStartOffset();
98+
int e1 = r1.getEndOffset();
99+
int s2 = r2.getStartOffset();
100+
int e2 = r2.getEndOffset();
101+
return Math.max(s1, s2) <= Math.min(e1, e2) ? 0 : Math.min(Math.abs(s1 - e2), Math.abs(s2 - e1));
102+
}
103+
}

0 commit comments

Comments
 (0)