Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1442dbe00ae75dd9d1b86cc6c222360a17230834 (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
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
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.text.edits;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.IRegion;


/**
 * A text edit group combines a list of {@link TextEdit}s
 * and a name into a single object. The name must be a human
 * readable string use to present the text edit group in the
 * user interface.
 * <p>
 * Clients may extend this class to add extra information to
 * a text edit group.
 * </p>
 *
 * @since 3.0
 */
public class TextEditGroup {

	private String fDescription;
	private List<TextEdit> fEdits;

	/**
	 * Creates a new text edit group with the given name.
	 *
	 * @param name the name of the text edit group. Must be
	 *  a human readable string
	 */
	public TextEditGroup(String name) {
		super();
		Assert.isNotNull(name);
		fDescription= name;
		fEdits= new ArrayList<>(3);
	}

	/**
	 * Creates a new text edit group with a name and a single
	 * {@link TextEdit}.
	 *
	 * @param name the name of the text edit group. Must be
	 *  a human readable string
	 * @param edit the edit to manage
	 */
	public TextEditGroup(String name, TextEdit edit) {
		Assert.isNotNull(name);
		Assert.isNotNull(edit);
		fDescription= name;
		fEdits= new ArrayList<>(1);
		fEdits.add(edit);
	}

	/**
	 * Creates a new text edit group with the given name and
	 * array of edits.
	 *
	 * @param name the name of the text edit group. Must be
	 *  a human readable string
	 * @param edits the array of edits
	 */
	public TextEditGroup(String name, TextEdit[] edits) {
		super();
		Assert.isNotNull(name);
		Assert.isNotNull(edits);
		fDescription= name;
		fEdits= new ArrayList<>(Arrays.asList(edits));
	}

	/**
	 * Returns the edit group's name.
	 *
	 * @return the edit group's name
	 */
	public String getName() {
		return fDescription;
	}

	/**
	 * Adds the given {@link TextEdit} to this group.
	 *
	 * @param edit the edit to add
	 */
	public void addTextEdit(TextEdit edit) {
		fEdits.add(edit);
	}

	/**
	 * Removes the given {@link TextEdit} from this group.
	 *
	 * @param edit the edit to remove
	 * @return <code>true</code> if this group contained the specified edit.
	 * @since 3.3
	 */
	public boolean removeTextEdit(TextEdit edit) {
	  return fEdits.remove(edit);
	}

	/**
	 * Removes all text edits from this group.
	 *
	 * @since 3.3
	 */
	public void clearTextEdits() {
	  fEdits.clear();
	}



	/**
	 * Returns <code>true</code> if the list of managed
	 * {@link TextEdit}s is empty; otherwise <code>false
	 * </code> is returned.
	 *
	 * @return whether the list of managed text edits is
	 *  empty or not
	 */
	public boolean isEmpty() {
		return fEdits.isEmpty();
	}

	/**
	 * Returns an array of {@link TextEdit}s containing
	 * the edits managed by this group.
	 *
	 * @return the managed text edits
	 */
	public TextEdit[] getTextEdits() {
		return fEdits.toArray(new TextEdit[fEdits.size()]);
	}

	/**
	 * Returns the text region covered by the edits managed via this
	 * edit group. If the group doesn't manage any edits <code>null
	 * </code> is returned.
	 *
	 * @return the text region covered by this edit group or <code>
	 *  null</code> if no edits are managed
	 */
	public IRegion getRegion() {
		int size= fEdits.size();
		if (size == 0) {
			return null;
		} else if (size == 1) {
			return fEdits.get(0).getRegion();
		} else {
			return TextEdit.getCoverage(fEdits.toArray(new TextEdit[fEdits.size()]));
		}
	}
}

Back to the top