Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 56c928769cd90cff501561c84c900d691e8c9b91 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
 * Copyright (c) 2004, 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.wst.css.core.internal.formatter;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.text.IRegion;
import org.eclipse.wst.css.core.internal.CSSCorePlugin;
import org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy;
import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts;
import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;

/**
 * 
 */
public class StyleDeclarationFormatter extends DefaultCSSSourceFormatter {

	private static StyleDeclarationFormatter instance;

	/**
	 * 
	 */
	StyleDeclarationFormatter() {
		super();
	}

	/**
	 * 
	 */
	protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) {
		CSSCleanupStrategy stgy = getCleanupStrategy(node);

		ICSSNode prev = (child != null) ? child.getPreviousSibling() : node.getLastChild();
		int start = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0;
		int end = (child != null) ? ((IndexedRegion) child).getStartOffset() : 0;

		// check no child
		if (child == null && prev == null)
			return;

		if (start > 0 && start < end) { // format source
			ICSSModel cssModel = node.getOwnerDocument().getModel();
			// BUG202615 - it is possible to have a style declaration with no
			// model associated with it
			if (cssModel != null) {
				IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
				if (structuredDocument != null) {
					// get meaning regions
					CompoundRegion[] regions = null;
					if (exceptFor == null)
						regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
					else {
						String pickupType = CSSRegionContexts.CSS_DECLARATION_DELIMITER;
						if (prev == null || child == null)
							pickupType = null;
						regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType);
					}
					// extract source
					for (int i = 0; i < regions.length; i++) {
						appendSpaceBefore(node, regions[i], source);
						source.append(decoratedRegion(regions[i], 0, stgy)); // must
						// be comments
					}
				}
			}
		} else if (prev != null && child != null) { // generate source :
			// between two declarations
			// BUG93037-properties view adds extra ; when add new property
			boolean semicolonFound = false;

			ICSSModel cssModel = node.getOwnerDocument().getModel();
			// BUG202615 - it is possible to have a style declaration with no
			// model associated with it
			if (cssModel != null) {
				IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
				if (structuredDocument != null) {
					int prevStart = (prev != null) ? ((IndexedRegion) prev).getStartOffset() : 0;
					int prevEnd = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0;
					CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(prevStart, prevEnd - prevStart), stgy);
					int i = regions.length - 1;
					while (i >= 0 && !semicolonFound) {
						if (regions[i].getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER)
							semicolonFound = true;
						--i;
					}
				}
			}
			if (!semicolonFound)
				source.append(";");//$NON-NLS-1$
		} else if (prev == null) { // generate source : before the first
			// declaration
			org.eclipse.wst.css.core.internal.util.RegionIterator it = null;
			if (end > 0) {
				ICSSModel cssModel = node.getOwnerDocument().getModel();
				// BUG202615 - it is possible to have a style declaration with
				// no model associated with it
				if (cssModel != null) {
					IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
					if (structuredDocument != null) {
						it = new org.eclipse.wst.css.core.internal.util.RegionIterator(structuredDocument, end - 1);
					}
				}
			} else {
				int pos = getChildInsertPos(node);
				if (pos >= 0) {
					ICSSModel cssModel = node.getOwnerDocument().getModel();
					// BUG202615 - it is possible to have a style declaration
					// with no model associated with it
					if (cssModel != null) {
						IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
						if (structuredDocument != null) {
							it = new org.eclipse.wst.css.core.internal.util.RegionIterator(structuredDocument, pos - 1);
						}
					}
				}
			}
			if (it != null) {
				int limit = ((IndexedRegion) ((node.getParentNode() != null) ? node.getParentNode() : node)).getStartOffset();
				while (it.hasPrev()) {
					ITextRegion curReg = it.prev();
					if (curReg.getType() == CSSRegionContexts.CSS_LBRACE || curReg.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER)
						break;
					if (curReg.getType() != CSSRegionContexts.CSS_S && curReg.getType() != CSSRegionContexts.CSS_COMMENT) {
						source.append(";");//$NON-NLS-1$
						break;
					}
					if (it.getStructuredDocumentRegion().getStartOffset(curReg) <= limit)
						break;
				}
			}
		} else if (child == null) { // generate source : after the last
			// declaration
			org.eclipse.wst.css.core.internal.util.RegionIterator it = null;
			if (start > 0) {
				ICSSModel cssModel = node.getOwnerDocument().getModel();
				// BUG202615 - it is possible to have a style declaration with
				// no model associated with it
				if (cssModel != null) {
					IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
					if (structuredDocument != null) {
						it = new org.eclipse.wst.css.core.internal.util.RegionIterator(structuredDocument, start);
					}
				}
			} else {
				int pos = getChildInsertPos(node);
				if (pos >= 0) {
					ICSSModel cssModel = node.getOwnerDocument().getModel();
					// BUG202615 - it is possible to have a style declaration
					// with no model associated with it
					if (cssModel != null) {
						IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
						if (structuredDocument != null) {
							it = new org.eclipse.wst.css.core.internal.util.RegionIterator(structuredDocument, pos);
						}
					}
				}
			}
			if (it != null) {
				int limit = ((IndexedRegion) ((node.getParentNode() != null) ? node.getParentNode() : node)).getEndOffset();
				while (it.hasNext()) {
					ITextRegion curReg = it.next();
					if (curReg.getType() == CSSRegionContexts.CSS_RBRACE || curReg.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER)
						break;
					if (curReg.getType() != CSSRegionContexts.CSS_S && curReg.getType() != CSSRegionContexts.CSS_COMMENT) {
						// Bug 219004 - Before appending a ;, make sure that there
						// isn't one already
						boolean semicolonFound = false;
						while(it.hasNext() && !semicolonFound) {
							if(it.next().getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER)
								semicolonFound = true;
						}
						
						if(!semicolonFound)
							source.append(";");//$NON-NLS-1$
						break;
					}
					if (limit <= it.getStructuredDocumentRegion().getEndOffset(curReg))
						break;
				}
			}
		}
		if (child == null) {
			if (((IndexedRegion) node).getEndOffset() <= 0) {
				// get next region
				int pos = getChildInsertPos(node);
				CompoundRegion toAppendRegion = null;
				if (pos >= 0) {
					ICSSModel cssModel = node.getOwnerDocument().getModel();
					// BUG202615 - it is possible to have a style declaration
					// with no model associated with it
					if (cssModel != null) {
						IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
						if (structuredDocument != null) {
							IStructuredDocumentRegion flatNode = structuredDocument.getRegionAtCharacterOffset(pos);
							toAppendRegion = new CompoundRegion(flatNode, flatNode.getRegionAtCharacterOffset(pos));
						}
					}
				}
				appendDelimBefore(node.getParentNode(), toAppendRegion, source);
			}
		} else if ((prev != null || ((IndexedRegion) node).getEndOffset() <= 0)) {
			Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();

			if (preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_ONE_PER_LINE) && (node.getOwnerDocument() != node || !preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR)))
				appendDelimBefore(node, null, source);
			else if (prev != null || node.getOwnerDocument() != node)
				appendSpaceBefore(node, toAppend, source);
		}
	}

	/**
	 * 
	 */
	protected void formatBefore(ICSSNode node, ICSSNode child, IRegion region, String toAppend, StringBuffer source) {
		CSSCleanupStrategy stgy = getCleanupStrategy(node);

		ICSSModel cssModel = node.getOwnerDocument().getModel();
		// BUG202615 - it is possible to have a style declaration
		// with no model associated with it
		if (cssModel != null) {
			IStructuredDocument structuredDocument = cssModel.getStructuredDocument();
			if (structuredDocument != null) {
				CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy);
				CompoundRegion[] outside = getOutsideRegions(structuredDocument, region);

				for (int i = 0; i < regions.length; i++) {
					if (i != 0 || needS(outside[0]))
						appendSpaceBefore(node, regions[i], source);
					source.append(decoratedRegion(regions[i], 0, stgy)); // must
																			// be
																			// comments
				}
				Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences();
				if (needS(outside[1])) {
					if (((IndexedRegion) child).getStartOffset() == region.getOffset() + region.getLength() && preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_ONE_PER_LINE) && (node.getOwnerDocument() != node || !preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR))) {
						appendDelimBefore(node, null, source);
					} else
						appendSpaceBefore(node, toAppend, source);
				}
			}
		}
	}

	/**
	 * Insert the method's description here.
	 */
	public int getChildInsertPos(ICSSNode node) {
		if (node == null)
			return -1;
		int pos = super.getChildInsertPos(node);
		if (pos < 0) {
			CSSSourceGenerator formatter = getParentFormatter(node);
			return (formatter != null) ? formatter.getChildInsertPos(node.getParentNode()) : -1;
		}
		return pos;
	}

	/**
	 * 
	 */
	public synchronized static StyleDeclarationFormatter getInstance() {
		if (instance == null)
			instance = new StyleDeclarationFormatter();
		return instance;
	}

	/**
	 * 
	 * @return int
	 * @param node
	 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
	 * @param insertPos
	 *            int
	 */
	public int getLengthToReformatAfter(ICSSNode node, int insertPos) {
		if (node == null)
			return 0;
		IndexedRegion nnode = (IndexedRegion) node;
		if (insertPos < 0 || !nnode.contains(insertPos)) {
			if (node.getParentNode() != null && nnode.getEndOffset() <= 0) {
				CSSSourceGenerator pntFormatter = getParentFormatter(node);
				if (pntFormatter != null)
					return pntFormatter.getLengthToReformatAfter(node.getParentNode(), insertPos);
			}
			return 0;
		}
		return super.getLengthToReformatAfter(node, insertPos);
	}

	/**
	 * 
	 * @return int
	 * @param node
	 *            org.eclipse.wst.css.core.model.interfaces.ICSSNode
	 * @param insertPos
	 *            int
	 */
	public int getLengthToReformatBefore(ICSSNode node, int insertPos) {
		if (node == null)
			return 0;
		IndexedRegion nnode = (IndexedRegion) node;
		if (insertPos <= 0 || !nnode.contains(insertPos - 1)) {
			if (node.getParentNode() != null && nnode.getEndOffset() <= 0) {
				CSSSourceGenerator pntFormatter = getParentFormatter(node);
				if (pntFormatter != null)
					return pntFormatter.getLengthToReformatBefore(node.getParentNode(), insertPos);
			}
			return 0;
		}
		return super.getLengthToReformatBefore(node, insertPos);
	}
}

Back to the top