Skip to main content
summaryrefslogtreecommitdiffstats
blob: f80c0f95757188a9e8ab7226905a0cda5a66ad64 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*****************************************************************************
 * Copyright (c) 2004 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.ui.contentassist;



import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.wst.css.core.document.ICSSNode;
import org.eclipse.wst.css.core.document.ICSSPrimitiveValue;
import org.eclipse.wst.css.core.document.ICSSStyleDeclItem;
import org.eclipse.wst.css.core.metamodel.CSSMMDescriptor;
import org.eclipse.wst.css.core.metamodel.CSSMMFunction;
import org.eclipse.wst.css.core.metamodel.CSSMMNode;
import org.eclipse.wst.css.core.metamodel.CSSMMNumber;
import org.eclipse.wst.css.core.metamodel.CSSMMProperty;
import org.eclipse.wst.css.core.metamodel.CSSMMUnit;
import org.eclipse.wst.css.core.metamodel.util.CSSFunctionID;
import org.eclipse.wst.css.core.metamodel.util.CSSMetaModelUtil;
import org.eclipse.wst.css.core.parser.CSSRegionContexts;
import org.eclipse.wst.css.core.preferences.CSSPreferenceHelper;
import org.eclipse.wst.css.core.util.CSSUtil;
import org.eclipse.wst.css.core.util.RegionIterator;
import org.eclipse.wst.css.ui.image.CSSImageType;
import org.eclipse.wst.sse.core.IndexedRegion;
import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.text.ITextRegion;
import org.w3c.dom.css.CSSFontFaceRule;

class CSSProposalGeneratorForDeclarationValue extends CSSProposalGenerator {

	private static final String IMPORTANT = "!important"; //$NON-NLS-1$
	private boolean fUseUpperCase;
	private boolean fAppendSemiColon;

	/**
	 * CSSProposalGeneratorForDeclarationValue constructor comment.
	 * 
	 * @param context
	 *            com.ibm.sed.contentassist.old.css.CSSContentAssistContext
	 */
	CSSProposalGeneratorForDeclarationValue(CSSContentAssistContext context) {
		super(context);
		fUseUpperCase = CSSPreferenceHelper.getInstance().isPropValueUpperCase();
	}

	/**
	 *  
	 */
	private void addFunction(List candidates, CSSMMFunction prop) {
		String text = prop.toString();
		if (!isMatch(text)) {
			return;
		}

		int cursorPos = 0;
		StringBuffer buf = new StringBuffer();
		if (prop.getName().equals(CSSFunctionID.F_URI)) {
			StringAndOffset sao = generateURI();
			buf.append(sao.fString);
			cursorPos = sao.fOffset;
		} else {
			buf.append(prop.toString());
			cursorPos = buf.length();
			StringAndOffset sao = generateParenthesis();
			buf.append(sao.fString);
			cursorPos += sao.fOffset;
		}

		text = buf.toString();
		text = (fUseUpperCase) ? text.toUpperCase() : text.toLowerCase();

		CSSCACandidate item = new CSSCACandidate();
		item.setReplacementString(text);
		item.setCursorPosition(cursorPos);
		item.setDisplayString(text);
		item.setImageType(CSSImageType.VALUE_FUNCTION);
		appendSemiColon(item);
		candidates.add(item);
	}

	/**
	 *  
	 */
	private void addNumber(List candidates, CSSMMNumber prop) {
		String fullText = fContext.getTextToReplace();
		// skip number
		int unitIndex = -1;
		for (int i = 0; i < fullText.length(); i++) {
			if (Character.isDigit(fullText.charAt(i))) {
				unitIndex = i + 1;
			} else {
				break;
			}
		}

		String unitSubText = ""; //$NON-NLS-1$
		String numSubText = ""; //$NON-NLS-1$
		if (0 <= unitIndex) {
			numSubText = fullText.substring(0, unitIndex);
			if (unitIndex < fullText.length()) {
				unitSubText = fullText.substring(unitIndex);
			}
		} else {
			unitSubText = fullText;
		}

		Iterator i = prop.getDescendants();
		while (i.hasNext()) {
			CSSMMUnit unit = (CSSMMUnit) i.next();
			String unitString = unit.getUnitString();
			if ((0 < unitSubText.length() && unitString.indexOf(unitSubText) != 0) || (0 < numSubText.length() && unitString.equals("#"))) { //$NON-NLS-1$
				continue;
			}

			String text = numSubText + unitString;
			text = (fUseUpperCase) ? text.toUpperCase() : text.toLowerCase();
			CSSCACandidate item = new CSSCACandidate();
			item.setReplacementString(text);
			if (0 < numSubText.length() || text.equals("#")) { //$NON-NLS-1$
				item.setCursorPosition(text.length());
			} else {
				item.setCursorPosition(0);
			}
			item.setDisplayString(text);
			item.setImageType(CSSImageType.VALUE_NUMBER);
			appendSemiColon(item);
			candidates.add(item);
		}
	}

	/**
	 *  
	 */
	private void checkSemiColon() {
		fAppendSemiColon = false;

		ITextRegion targetRegion = fContext.getTargetRegion();
		if (targetRegion != null && targetRegion.getType() != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
			// find trailing ":" or ";"
			// if ":" before ";" is found, add ";"
			RegionIterator iterator = fContext.getRegionIterator();
			IStructuredDocumentRegion container = iterator.getStructuredDocumentRegion();
			while (iterator.hasNext()) {
				ITextRegion region = iterator.next();
				if (iterator.getStructuredDocumentRegion() != container) {
					break;
				}
				if (region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
					fAppendSemiColon = true;
					break;
				}
			}
			if (!fAppendSemiColon) {
				// second chance:
				// leading IStructuredDocumentRegion is not ";"
				IStructuredDocumentRegion nextStructuredDocumentRegion = CSSUtil.findNextSignificantNode(container);
				if (CSSUtil.getStructuredDocumentRegionType(nextStructuredDocumentRegion) != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
					fAppendSemiColon = true;
				}
			}
		}
	}

	/**
	 *  
	 */
	private void appendSemiColon(CSSCACandidate item) {
		if (fAppendSemiColon) {
			String replacementString = item.getReplacementString();
			item.setReplacementString(replacementString + ";"); //$NON-NLS-1$
			int cursorPosition = item.getCursorPosition();
			if (replacementString.length() <= cursorPosition) {
				// cursorpos is tail of string
				cursorPosition++;
				item.setCursorPosition(cursorPosition);
			}
		}
	}

	/**
	 *  
	 */
	private void addSemiColon(List candidates) {
		ICSSNode targetNode = fContext.getTargetNode();
		if (targetNode instanceof ICSSStyleDeclItem) {
			ICSSNode firstChild = targetNode.getFirstChild();
			if (firstChild == null) {
				return;
			}
			if (firstChild instanceof IndexedRegion) {
				int startOffset = ((IndexedRegion) firstChild).getStartOffset();
				if (fContext.getCursorPos() <= startOffset) {
					return;
				}
			}
		}

		boolean bAddCloser = false;

		ITextRegion targetRegion = fContext.getTargetRegion();
		if (targetRegion != null && targetRegion.getType() != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
			// find trailing ":" or ";"
			// if ":" before ";" is found, add ";"
			RegionIterator iterator = fContext.getRegionIterator();
			IStructuredDocumentRegion container = iterator.getStructuredDocumentRegion();
			while (iterator.hasNext()) {
				ITextRegion region = iterator.next();
				if (iterator.getStructuredDocumentRegion() != container) {
					break;
				}
				if (region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
					bAddCloser = true;
					break;
				}
			}
			if (!bAddCloser) {
				// second chance:
				// leading IStructuredDocumentRegion is not ";"
				IStructuredDocumentRegion nextStructuredDocumentRegion = CSSUtil.findNextSignificantNode(container);
				if (CSSUtil.getStructuredDocumentRegionType(nextStructuredDocumentRegion) != CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
					bAddCloser = true;
				}
			}
		}

		if (bAddCloser) {
			CSSCACandidate item = new CSSCACandidate();
			String text = fContext.getTextToReplace() + ";";//$NON-NLS-1$
			item.setReplacementString(text);
			item.setCursorPosition(text.length());
			item.setDisplayString(";");//$NON-NLS-1$
			item.setImageType(null);
			candidates.add(item);
		}
	}

	/**
	 *  
	 */
	private void addString(List candidates, String text) {
		if (!isMatch(text)) {
			return;
		}

		text = (fUseUpperCase) ? text.toUpperCase() : text.toLowerCase();

		CSSCACandidate item = new CSSCACandidate();
		item.setReplacementString(text);
		item.setCursorPosition(text.length());
		item.setDisplayString(text);
		item.setImageType(CSSImageType.VALUE_STRING);
		appendSemiColon(item);
		candidates.add(item);
	}

	private void addImportant(List candidates) {
		ICSSNode targetNode = fContext.getTargetNode();
		while (targetNode instanceof ICSSPrimitiveValue) {
			targetNode = targetNode.getParentNode();
		}
		if (!(targetNode instanceof ICSSStyleDeclItem)) {
			return;
		}
		// 1. has no priority region
		// 2. cursor position is after of last child
		// 3. normal isMatch method
		String priority = ((ICSSStyleDeclItem) targetNode).getPriority();
		if (priority == null || priority.length() == 0) {
			ICSSNode lastChild = targetNode.getLastChild();
			if (lastChild instanceof IndexedRegion) {
				int startOffset = ((IndexedRegion) lastChild).getStartOffset();
				//	int endOffset = ((IndexedRegion)lastChild).getEndOffset();
				if (startOffset < fContext.getCursorPos() && isMatch(IMPORTANT)) {
					CSSCACandidate item = new CSSCACandidate();
					item.setReplacementString(IMPORTANT);
					item.setCursorPosition(IMPORTANT.length());
					item.setDisplayString(IMPORTANT);
					item.setImageType(CSSImageType.VALUE_STRING);
					appendSemiColon(item);
					candidates.add(item);
				}
			}
		}
	}

	/**
	 * getCandidates method comment.
	 */
	protected Iterator getCandidates() {
		List candidates = new ArrayList();

		checkSemiColon(); // check should add semi-colon or not

		String name = getPropertyName();
		if (name != null) {
			CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
			Iterator i = Collections.EMPTY_LIST.iterator();
			if (isFontFaceRule()) {
				CSSMMDescriptor desc = util.getDescriptor(name);
				if (desc != null) {
					i = desc.getValues();
				}
			} else {
				CSSMMProperty prop = util.getProperty(name);
				if (prop != null) {
					i = prop.getValues();
				}
			}
			while (i.hasNext()) {
				CSSMMNode val = (CSSMMNode) i.next();
				String valueType = val.getType();
				if (valueType == CSSMMNode.TYPE_KEYWORD) {
					addString(candidates, val.toString());
				} else if (valueType == CSSMMNode.TYPE_NUMBER) {
					addNumber(candidates, (CSSMMNumber) val);
				} else if (valueType == CSSMMNode.TYPE_FUNCTION) {
					addFunction(candidates, (CSSMMFunction) val);
				}
			}
		}

		addImportant(candidates);
		addSemiColon(candidates);

		return candidates.iterator();
	}

	/**
	 * @return java.lang.String
	 */
	private String getPropertyName() {
		ICSSNode targetNode = fContext.getTargetNode();
		while (targetNode instanceof ICSSPrimitiveValue) {
			targetNode = targetNode.getParentNode();
		}
		if (targetNode instanceof ICSSStyleDeclItem) {
			return ((ICSSStyleDeclItem) targetNode).getPropertyName();
		} else {
			return null;
		}
	}

	/**
	 *  
	 */
	private boolean isFontFaceRule() {
		ICSSNode targetNode = fContext.getTargetNode();
		while (targetNode instanceof ICSSPrimitiveValue) {
			targetNode = targetNode.getParentNode();
		}
		if (targetNode instanceof ICSSStyleDeclItem) {
			targetNode = targetNode.getParentNode(); // get Declaration
			if (targetNode != null) {
				// inline style has no rule node
				targetNode = targetNode.getParentNode(); // get rule
			}
		}
		return (targetNode instanceof CSSFontFaceRule);
	}
}

Back to the top