Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7857b1d2f78eeee851ce83d1652132223d2d3ec2 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*******************************************************************************
 * Copyright (c) 2013 Google, Inc 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:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.includes;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.eclipse.cdt.internal.ui.preferences.PreferencesMessages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;

/**
 * Style preferences for a category of include statements.
 */
public class IncludeGroupStyle implements Comparable<IncludeGroupStyle> {

	public enum IncludeKind {
		RELATED(PreferencesMessages.IncludeCategoriesBlock_related_headers_node,
				PreferencesMessages.IncludeCategoriesBlock_related_headers_node_description, null),
		PARTNER(PreferencesMessages.IncludeCategoriesBlock_partner_header_node,
				PreferencesMessages.IncludeCategoriesBlock_partner_header_node_description, RELATED),
		IN_SAME_FOLDER(PreferencesMessages.IncludeCategoriesBlock_same_folder_header_node,
				PreferencesMessages.IncludeCategoriesBlock_same_folder_header_node_description, RELATED),
		IN_SUBFOLDER(PreferencesMessages.IncludeCategoriesBlock_subfolder_header_node,
				PreferencesMessages.IncludeCategoriesBlock_subfolder_header_node_description, RELATED),
		SYSTEM(PreferencesMessages.IncludeCategoriesBlock_system_headers_node,
				PreferencesMessages.IncludeCategoriesBlock_system_headers_node_description, null),
		SYSTEM_WITH_EXTENSION(PreferencesMessages.IncludeCategoriesBlock_system_headers_with_extension_node,
				PreferencesMessages.IncludeCategoriesBlock_system_headers_with_extension_node_description, SYSTEM),
		SYSTEM_WITHOUT_EXTENSION(PreferencesMessages.IncludeCategoriesBlock_system_headers_without_extension_node,
				PreferencesMessages.IncludeCategoriesBlock_system_headers_without_extension_node_description, SYSTEM),
		OTHER(PreferencesMessages.IncludeCategoriesBlock_unrelated_headers_node,
				PreferencesMessages.IncludeCategoriesBlock_unrelated_headers_node_description, null),
		IN_SAME_PROJECT(PreferencesMessages.IncludeCategoriesBlock_same_project_headers_node,
				PreferencesMessages.IncludeCategoriesBlock_same_project_headers_node_description, OTHER),
		IN_OTHER_PROJECT(PreferencesMessages.IncludeCategoriesBlock_other_project_headers_node,
				PreferencesMessages.IncludeCategoriesBlock_other_project_headers_node_description, OTHER),
		EXTERNAL(PreferencesMessages.IncludeCategoriesBlock_external_headers_node,
				PreferencesMessages.IncludeCategoriesBlock_external_headers_node_description, OTHER),
		MATCHING_PATTERN(PreferencesMessages.IncludeCategoriesBlock_user_defined_categories_node,
				PreferencesMessages.IncludeCategoriesBlock_user_defined_categories_node_description, null);

		public final String name;
		public final String description;
		public final IncludeKind parent;
		public final List<IncludeKind> children = new ArrayList<>();

		private IncludeKind(String name, String description, IncludeKind parent) {
			this.name = name;
			this.description = description;
			this.parent = parent;
			if (parent != null)
				parent.children.add(this);
		}

		public boolean hasChildren() {
			return !children.isEmpty();
		}
	}

	private static final String TAG_STYLE = "style"; //$NON-NLS-1$
	private static final String TAG_NAME = "name"; //$NON-NLS-1$
	private static final String TAG_PATTERN = "pattern"; //$NON-NLS-1$
	private static final String TAG_KEEP_TOGETHER = "keep_together"; //$NON-NLS-1$
	private static final String TAG_BLANK_LINE_BEFORE = "blank_line_before"; //$NON-NLS-1$
	private static final String TAG_RELATIVE_PATH = "relative_path"; //$NON-NLS-1$
	private static final String TAG_ANGLE_BRACKETS = "angle_brackets"; //$NON-NLS-1$
	private static final String TAG_ORDER = "order"; //$NON-NLS-1$

	private final IncludeKind includeKind;
	private boolean keepTogether;
	private boolean blankLineBefore;
	private boolean relativePath;
	private boolean angleBrackets;
	private Pattern headerNamePattern;
	private String name;
	private int order; // Relative position of the include group. Ignored if keepTogether is false.

	public IncludeGroupStyle(IncludeKind includeKind) {
		if (includeKind == null || includeKind == IncludeKind.MATCHING_PATTERN)
			throw new IllegalArgumentException();
		this.includeKind = includeKind;
	}

	public IncludeGroupStyle(String name, Pattern headerNamePattern) {
		if (name == null)
			throw new IllegalArgumentException();
		if (headerNamePattern == null)
			throw new IllegalArgumentException();
		this.includeKind = IncludeKind.MATCHING_PATTERN;
		this.name = name;
		this.headerNamePattern = headerNamePattern;
	}

	public boolean isKeepTogether() {
		return keepTogether;
	}

	public void setKeepTogether(boolean value) {
		this.keepTogether = value;
	}

	public boolean isBlankLineBefore() {
		return blankLineBefore;
	}

	public void setBlankLineBefore(boolean value) {
		this.blankLineBefore = value;
	}

	public boolean isRelativePath() {
		return relativePath;
	}

	public void setRelativePath(boolean value) {
		assert !includeKind.hasChildren();
		this.relativePath = value;
	}

	public boolean isAngleBrackets() {
		return angleBrackets;
	}

	public void setAngleBrackets(boolean value) {
		assert !includeKind.hasChildren();
		this.angleBrackets = value;
	}

	public Pattern getHeaderNamePattern() {
		return headerNamePattern;
	}

	public void setHeaderNamePattern(Pattern headerNamePattern) {
		this.headerNamePattern = headerNamePattern;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public IncludeKind getIncludeKind() {
		return includeKind;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public static IncludeGroupStyle fromMemento(IMemento memento, IncludeKind includeKind) {
		IncludeGroupStyle style;
		if (includeKind == IncludeKind.MATCHING_PATTERN) {
			String name = nullToEmpty(memento.getString(TAG_NAME));
			String pattern = nullToEmpty(memento.getString(TAG_PATTERN));
			style = new IncludeGroupStyle(name, Pattern.compile(pattern));
		} else {
			style = new IncludeGroupStyle(includeKind);
		}
		style.setKeepTogether(nullToFalse(memento.getBoolean(TAG_KEEP_TOGETHER)));
		style.setBlankLineBefore(nullToFalse(memento.getBoolean(TAG_BLANK_LINE_BEFORE)));
		if (!includeKind.hasChildren()) {
			style.setRelativePath(nullToFalse(memento.getBoolean(TAG_RELATIVE_PATH)));
			style.setAngleBrackets(nullToFalse(memento.getBoolean(TAG_ANGLE_BRACKETS)));
		}
		Integer order = memento.getInteger(TAG_ORDER);
		if (order != null)
			style.setOrder(order.intValue());
		return style;
	}

	private static boolean nullToFalse(Boolean val) {
		return val != null && val.booleanValue();
	}

	private static String nullToEmpty(String val) {
		return val != null ? val : ""; //$NON-NLS-1$
	}

	public void saveToMemento(IMemento memento) {
		if (includeKind == IncludeKind.MATCHING_PATTERN) {
			memento.putString(TAG_NAME, name);
			memento.putString(TAG_PATTERN, headerNamePattern.toString());
		}
		memento.putBoolean(TAG_KEEP_TOGETHER, keepTogether);
		memento.putBoolean(TAG_BLANK_LINE_BEFORE, blankLineBefore);
		if (!includeKind.hasChildren()) {
			memento.putBoolean(TAG_RELATIVE_PATH, relativePath);
			memento.putBoolean(TAG_ANGLE_BRACKETS, angleBrackets);
		}
		if (keepTogether)
			memento.putInteger(TAG_ORDER, order);
	}

	public String toXmlString() {
		XMLMemento memento = XMLMemento.createWriteRoot(TAG_STYLE);
		saveToMemento(memento);
		StringWriter writer = new StringWriter();
		try {
			memento.save(writer);
		} catch (IOException e) {
			CUIPlugin.log(e);
		}
		return writer.toString();
	}

	public static IncludeGroupStyle fromXmlString(String str, IncludeKind includeKind) {
		StringReader reader = new StringReader(str);
		XMLMemento memento;
		try {
			memento = XMLMemento.createReadRoot(reader);
		} catch (WorkbenchException e) {
			return null;
		}
		return fromMemento(memento, includeKind);
	}

	/** For debugging only */
	@Override
	public String toString() {
		return includeKind.toString();
	}

	/**
	 * Compares styles according to their sorting order.
	 */
	@Override
	public int compareTo(IncludeGroupStyle other) {
		if (keepTogether != other.keepTogether)
			return keepTogether ? -1 : 1;
		int c = order - other.order;
		if (c != 0)
			return c;
		return includeKind.ordinal() - other.includeKind.ordinal();
	}

	public IncludeGroupStyle getGroupingStyle(Map<IncludeKind, IncludeGroupStyle> stylesMap) {
		if (keepTogether)
			return this;
		IncludeGroupStyle parent = getParentStyle(stylesMap);
		if (parent != null && (parent.keepTogether || parent.includeKind == IncludeKind.OTHER))
			return parent;
		return stylesMap.get(IncludeKind.OTHER);
	}

	private IncludeGroupStyle getParentStyle(Map<IncludeKind, IncludeGroupStyle> stylesMap) {
		IncludeKind kind = includeKind.parent;
		if (kind == null)
			return null;
		return stylesMap.get(kind);
	}

	public boolean isBlankLineNeededAfter(IncludeGroupStyle previousIncludeStyle,
			Map<IncludeKind, IncludeGroupStyle> stylesMap) {
		if (previousIncludeStyle == null)
			return false;
		IncludeGroupStyle groupingStyle = getGroupingStyle(stylesMap);
		IncludeGroupStyle previousGroupingStyle = previousIncludeStyle.getGroupingStyle(stylesMap);
		if (groupingStyle != previousGroupingStyle && groupingStyle.isBlankLineBefore())
			return true;
		IncludeGroupStyle parentStyle = groupingStyle.getParentStyle(stylesMap);
		IncludeGroupStyle previousParentStyle = previousGroupingStyle.getParentStyle(stylesMap);
		return parentStyle != null && previousParentStyle != null && parentStyle != previousParentStyle
				&& parentStyle.isKeepTogether() && parentStyle.isBlankLineBefore();
	}
}

Back to the top