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
|
/*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.internal.texteditor;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.Position;
import org.eclipse.ui.IEditorInput;
/**
* Data structure representing an edit position.
*
* @since 2.1
*/
public final class EditPosition {
/** The editor input */
private final IEditorInput fEditorInput;
/** The editor ID */
private final String fEditorId;
/** The position */
private final Position fPosition;
/**
* how many characters may come in between two edit positions for them to
* still be lumped into same bucket in position history (designed to prevent
* filling history with meaningless noise of very similar positions)
*/
public static final int PROXIMITY_THRESHOLD = 30;
/**
* Creates a new edit position.
* @param editorInput the editor input
* @param editorId the editor ID
* @param pos the position
*/
public EditPosition(IEditorInput editorInput, String editorId, Position pos) {
Assert.isNotNull(editorInput);
Assert.isNotNull(editorId);
fEditorId= editorId;
fEditorInput= editorInput;
fPosition= pos;
}
/**
* Returns the editor input for this edit position.
*
* @return the editor input of this edit position
*/
public IEditorInput getEditorInput() {
return fEditorInput;
}
/**
* Returns the editor id for this edit position.
*
* @return the editor input of this edit position
*/
public String getEditorId() {
return fEditorId;
}
/**
* Returns the position.
*
* @return the position
*/
public Position getPosition() {
return fPosition;
}
/**
* @param a Position to compare the other arg against
* @param b Another position to compare the first arg against
* @param threshold The maximum allowed distance between Position args for them
* to be considered co-located
* @return true if both Position args are colocated as defined by the threshold
* param
* @since 3.15
*/
public static boolean areCoLocated(Position a, Position b, int threshold) {
if (a == null || b == null) {
return false;
}
int center1 = a.offset + (a.length / 2);
int center2 = b.offset + (b.length / 2);
int centerDistance = Math.abs(center1 - center2);
int minWithoutOverlap = a.length / 2 + b.length / 2;
return centerDistance < (minWithoutOverlap + threshold);
}
/**
* @since 3.15
*/
public static boolean areCoLocated(Position a, Position b) {
return EditPosition.areCoLocated(a, b, EditPosition.PROXIMITY_THRESHOLD);
}
/**
* @since 3.15
*/
public static boolean areCoLocated(EditPosition a, EditPosition b, int threshold) {
return a != null && b != null && a.getEditorInput().getName()
.equals(b.getEditorInput().getName())
&& EditPosition.areCoLocated(a.getPosition(), b.getPosition(), threshold);
}
/**
* @since 3.15
*/
public static boolean areCoLocated(EditPosition a, EditPosition b) {
return EditPosition.areCoLocated(a, b, EditPosition.PROXIMITY_THRESHOLD);
}
}
|