Skip to main content
summaryrefslogtreecommitdiffstats
blob: ddb0840beaa6d9ca702267694744e2371365e1b0 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*****************************************************************************
 * 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.autoedit;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.ITextEditorExtension3;
import org.eclipse.wst.css.core.parser.CSSRegionContexts;
import org.eclipse.wst.css.core.preferences.CSSPreferenceHelper;
import org.eclipse.wst.css.core.util.RegionIterator;
import org.eclipse.wst.css.ui.internal.Logger;
import org.eclipse.wst.sse.core.text.IStructuredDocument;
import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.text.ITextRegion;
import org.eclipse.wst.sse.ui.internal.autoedit.BasicAutoEditStrategy;

public class StructuredAutoEditStrategyCSS extends BasicAutoEditStrategy {
	protected IStructuredDocument structuredDocument = null;

	class CompoundRegion {

		CompoundRegion(IStructuredDocumentRegion documentRegion, ITextRegion textRegion) {
			super();
			this.documentRegion = documentRegion;
			this.textRegion = textRegion;
		}

		IStructuredDocumentRegion getDocumentRegion() {
			return documentRegion;
		}

		ITextRegion getTextRegion() {
			return textRegion;
		}

		int getStart() {
			return textRegion.getStart();
		}

		int getEnd() {
			return textRegion.getEnd();
		}

		String getType() {
			return textRegion.getType();
		}

		String getText() {
			return documentRegion.getText(textRegion);
		}

		int getStartOffset() {
			return documentRegion.getStartOffset(textRegion);
		}

		int getEndOffset() {
			return documentRegion.getEndOffset(textRegion);
		}

		private IStructuredDocumentRegion documentRegion;
		private ITextRegion textRegion;

	}

	/**
	 */
	protected void autoIndentAfterClose(DocumentCommand command, String regionType) {
		if (!setRangeForClose(command))
			return;

		int position = command.offset + command.length;

		if (position == -1 || structuredDocument.getLength() == 0) {
			return;
		}

		// get open brace region
		CompoundRegion region = prevCorrespondence(position, regionType);

		// get indentation
		String str = getIndentFor(region, false);

		// append to input
		if (str != null)
			command.text = str + command.text;
	}

	/**
	 * Copies the indentation of the previous line.
	 */
	protected void autoIndentAfterNewLine(DocumentCommand command) {
		// select nearest white spaces to replace with new-line
		setRangeForNewLine(command);

		// get position
		int position = command.offset;

		if (position == -1 || structuredDocument.getLength() == 0) {
			return;
		}

		// get current region
		CompoundRegion currentRegion = getRegion(command.offset + command.length);

		// get key region
		CompoundRegion keyRegion = getPrevKeyRegion(position, currentRegion);

		// get indent string
		String str = getIndentFor(keyRegion, true);

		// check another indentation
		int shift = needShift(keyRegion, command.offset + command.length);

		// create text to replace
		StringBuffer buf = new StringBuffer(command.text);
		if (str != null)
			buf.append(str);
		while (shift-- != 0)
			buf.append(CSSPreferenceHelper.getInstance().getIndentString());
		command.text = buf.toString();

	}

	/**
	 */
	public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
		Object textEditor = getActiveTextEditor();
		if (!(textEditor instanceof ITextEditorExtension3 && ((ITextEditorExtension3) textEditor).getInsertMode() == ITextEditorExtension3.SMART_INSERT))
			return;

		//return;
		///*
		structuredDocument = (IStructuredDocument) document;

		if (command.length == 0 && command.text != null) {
			if (endsWith(document.getLegalLineDelimiters(), command.text) != -1) {
				autoIndentAfterNewLine(command);
			} else if (command.text.equals("}")) {//$NON-NLS-1$
				autoIndentAfterClose(command, CSSRegionContexts.CSS_RBRACE);
			} else if (command.text.equals("]")) {//$NON-NLS-1$
				autoIndentAfterClose(command, CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END);
			} else if (command.text.equals(")")) {//$NON-NLS-1$
				autoIndentAfterClose(command, CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE);
			}
		}
		//*/
	}

	/**
	 */
	protected String getIndentFor(CompoundRegion region, boolean indentForNextRegion) {
		if (region == null)
			return null;
		IStructuredDocumentRegion flatNode = region.getDocumentRegion();
		if (flatNode == null)
			return null;

		try {
			if (region.getType() == CSSRegionContexts.CSS_LBRACE || region.getType() == CSSRegionContexts.CSS_DELIMITER || region.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
				// get meanful flat node
				RegionIterator it = new RegionIterator(flatNode, region.getTextRegion());
				it.prev();
				while (it.hasPrev()) {
					ITextRegion r = it.prev();
					region = new CompoundRegion(it.getStructuredDocumentRegion(), r);
					if (region.getType() != CSSRegionContexts.CSS_S)
						break;
				}
				flatNode = region.getDocumentRegion();
				// get indent string
				int position = flatNode.getStart();
				int line = structuredDocument.getLineOfOffset(position);
				int start = structuredDocument.getLineOffset(line);
				int end = findEndOfWhiteSpace(structuredDocument, start, position);
				return structuredDocument.get(start, end - start);
			} else if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START ||
			//region.getType() == CSSRegionContexts.CSS_PARENTHESIS_OPEN ||
						region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION || region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
				int position = flatNode.getStart() + region.getStart();
				int line = structuredDocument.getLineOfOffset(position);
				int start = structuredDocument.getLineOffset(line);
				int end = findEndOfWhiteSpace(structuredDocument, start, position);
				StringBuffer buf = new StringBuffer(structuredDocument.get(start, end - start));
				position += region.getText().length();
				if (indentForNextRegion) {
					int tokenStart = findEndOfWhiteSpace(structuredDocument, position, structuredDocument.getLineOffset(line) + structuredDocument.getLineLength(line) - 1);
					if (tokenStart < structuredDocument.getLineOffset(line) + structuredDocument.getLineLength(line) - 1) {
						position = tokenStart;
					}
				}
				while (position - end > 0) {
					buf.append(" ");//$NON-NLS-1$
					end++;
				}
				return buf.toString();
			} else
				return "";//$NON-NLS-1$
		} catch (BadLocationException excp) {
			Logger.logException(excp);
		}
		return null;
	}

	/**
	 */
	protected CompoundRegion getPrevKeyRegion(int position, CompoundRegion currentRegion) {
		if (currentRegion == null) {
			if (structuredDocument.getLastStructuredDocumentRegion() == null)
				return null;
		}

		if (currentRegion != null && (currentRegion.getType() == CSSRegionContexts.CSS_RBRACE || currentRegion.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END || currentRegion.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE)) {
			return prevCorrespondence(currentRegion);
		}

		RegionIterator it = new RegionIterator(structuredDocument, position - 1);
		while (it.hasPrev()) {
			ITextRegion r = it.prev();
			CompoundRegion region = new CompoundRegion(it.getStructuredDocumentRegion(), r);
			if (region.getType() == CSSRegionContexts.CSS_LBRACE ||
			//          region.getType() == CSSRegionContexts.CSS_RBRACE ||
						region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START ||
						//          region.getType() ==
						// CSSRegionContexts.CSS_BRACKET_CLOSE ||
						//// region.getType() ==
						// CSSRegionContexts.CSS_PARENTHESIS_OPEN ||
						//          region.getType() ==
						// CSSRegionContexts.CSS_PARENTHESIS_CLOSE ||
						region.getType() == CSSRegionContexts.CSS_DELIMITER || region.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER ||
						//          region.getType() == CSSRegionContexts.CSS_COLON ||
						//          region.getType() == CSSRegionContexts.CSS_COMMENT
						// ||
						region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION) {
				return region;
			} else if (region.getType() == CSSRegionContexts.CSS_RBRACE || region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END || region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
				// skip to LBRACE
				CompoundRegion pc = prevCorrespondence(region);
				it.reset(pc.getDocumentRegion(), pc.getTextRegion());
				it.prev();
			} else if (region.getType() == CSSRegionContexts.CSS_STRING) {
				RegionIterator itTmp = new RegionIterator(structuredDocument, position);
				if (region == itTmp.prev())
					return region; // position is inside of string
			} else if (region.getType() == CSSRegionContexts.CSS_COMMENT) {
				RegionIterator itTmp = new RegionIterator(structuredDocument, position);
				if (region == itTmp.prev())
					return region; // position is inside of comment
			} else if (region.getType() == CSSRegionContexts.CSS_UNKNOWN) {
				String str = region.getText();
				if (str.charAt(str.length() - 1) == '\\')
					return region;
			} else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) {
				RegionIterator itPrev = new RegionIterator(region.getDocumentRegion(), region.getTextRegion());
				while (itPrev.hasPrev()) {
					ITextRegion regionPrev = itPrev.prev();
					if (regionPrev.getType() == CSSRegionContexts.CSS_RBRACE) {
						break;
					} else if (regionPrev.getType() == CSSRegionContexts.CSS_DELIMITER || regionPrev.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
						return region;
					} else if (regionPrev.getType() == CSSRegionContexts.CSS_LBRACE) {
						while (itPrev.hasPrev()) {
							regionPrev = itPrev.prev();
							if (regionPrev.getType() == CSSRegionContexts.CSS_MEDIA)
								break;
							if (regionPrev.getType() == CSSRegionContexts.CSS_LBRACE || regionPrev.getType() == CSSRegionContexts.CSS_RBRACE || regionPrev.getType() == CSSRegionContexts.CSS_DELIMITER || regionPrev.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER)
								return region;
						}
						if (regionPrev.getType() == CSSRegionContexts.CSS_MEDIA)
							break;
						else
							return region;
					}
				}
			}
		}
		return null;
	}

	/**
	 */
	protected CompoundRegion getRegion(int position) {
		IStructuredDocumentRegion flatNode = structuredDocument.getRegionAtCharacterOffset(position);
		if (flatNode != null)
			return new CompoundRegion(flatNode, flatNode.getRegionAtCharacterOffset(position));
		return null;
	}

	/**
	 */
	protected int needShift(CompoundRegion region, int position) {
		int shift = 0;
		if (region == null || region.getType() == CSSRegionContexts.CSS_DELIMITER || region.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER || region.getType() == CSSRegionContexts.CSS_LBRACE) {
			// get non space region
			CompoundRegion cr = getRegion(position - 1);
			RegionIterator it = new RegionIterator(cr.getDocumentRegion(), cr.getTextRegion());
			ITextRegion nearestRegion = null;
			while (it.hasPrev()) {
				nearestRegion = it.prev();
				if (nearestRegion.getType() != CSSRegionContexts.CSS_S && nearestRegion.getType() != CSSRegionContexts.CSS_COMMENT)
					break;
			}
			if (nearestRegion != null && (nearestRegion.getType() == CSSRegionContexts.CSS_LBRACE || nearestRegion.getType() == CSSRegionContexts.CSS_RBRACE || nearestRegion.getType() == CSSRegionContexts.CSS_DELIMITER || nearestRegion.getType() == CSSRegionContexts.CSS_DECLARATION_DELIMITER))
				shift--;
			else if (region == null)
				shift--;
			shift++;
		}
		if (region != null && region.getType() == CSSRegionContexts.CSS_LBRACE) {
			RegionIterator it = new RegionIterator(structuredDocument, position);
			if (!it.hasPrev() || it.prev().getType() != CSSRegionContexts.CSS_RBRACE)
				shift++;
			else
				shift = 0;
		}
		return shift;
	}

	/**
	 */
	protected CompoundRegion prevCorrespondence(int position, String regionType) {
		RegionIterator it = new RegionIterator(structuredDocument, position - 1);
		ITextRegion region = null;
		int nest = 1;
		if (regionType == CSSRegionContexts.CSS_RBRACE) {
			// skip to LBRACE
			while (it.hasPrev()) {
				region = it.prev();
				if (region.getType() == CSSRegionContexts.CSS_LBRACE)
					nest--;
				else if (region.getType() == CSSRegionContexts.CSS_RBRACE)
					nest++;
				if (nest <= 0)
					break;
			}
			if (nest == 0)
				return new CompoundRegion(it.getStructuredDocumentRegion(), region);
		}
		if (regionType == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END) {
			// skip to BRACKET_OPEN
			while (it.hasPrev()) {
				region = it.prev();
				if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START)
					nest--;
				else if (region.getType() == CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END)
					nest++;
				if (nest <= 0)
					break;
			}
			if (nest == 0)
				return new CompoundRegion(it.getStructuredDocumentRegion(), region);
		}
		if (regionType == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) {
			// skip to PARENTHESIS_OPEN
			while (it.hasPrev()) {
				region = it.prev();
				if (//region.getType() ==
					// CSSRegionContexts.CSS_PARENTHESIS_OPEN ||
				region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION)
					nest--;
				else if (region.getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE)
					nest++;
				if (nest <= 0)
					break;
			}
			if (nest == 0)
				return new CompoundRegion(it.getStructuredDocumentRegion(), region);
		}
		return null;
	}

	/**
	 */
	protected CompoundRegion prevCorrespondence(CompoundRegion region) {
		if (region == null)
			return null;

		IStructuredDocumentRegion flatNode = region.getDocumentRegion();
		int position = flatNode.getStart() + region.getStart();
		return prevCorrespondence(position, region.getType());
	}

	/**
	 * Insert the method's description here.
	 * 
	 * @return boolean
	 * @param command
	 *            org.eclipse.jface.text.DocumentCommand
	 */
	protected boolean setRangeForClose(DocumentCommand command) {
		int position = command.offset;

		if (position == -1 || structuredDocument.getLength() == 0) {
			return false;
		}

		try {
			// find start of line
			int p = (position == structuredDocument.getLength() ? position - 1 : position);

			int line = structuredDocument.getLineOfOffset(p);
			int start = structuredDocument.getLineOffset(line);
			RegionIterator it = new RegionIterator(structuredDocument, start);
			boolean allWhiteSpace = false;
			// check whether the text from lStart to position is white space
			// or not
			while (it.hasNext()) {
				ITextRegion region = it.next();
				if (region.getType() != CSSRegionContexts.CSS_S)
					break;
				if (it.getStructuredDocumentRegion().getEndOffset(region) > p) {
					allWhiteSpace = true;
					break;
				}
			}
			if (allWhiteSpace) {
				command.length = command.length - (start - command.offset);
				command.offset = start;
				return true;
			}
		} catch (BadLocationException excp) {
			Logger.logException(excp);
		}
		return false;
	}

	/**
	 */
	protected void setRangeForNewLine(DocumentCommand command) {
		int position = command.offset;

		if (position == -1 || structuredDocument.getLength() == 0) {
			return;
		}

		try {
			// add pre-nearest white spaces to replace target
			if (position > 0) {
				IStructuredDocumentRegion flatNode = structuredDocument.getRegionAtCharacterOffset(position - 1);
				if (flatNode != null) {
					ITextRegion region = flatNode.getRegionAtCharacterOffset(position - 1);
					if (region.getType() == CSSRegionContexts.CSS_S) {
						int end = command.offset + command.length;
						int nLine = structuredDocument.getLineOfOffset(position);
						int nStartPos = structuredDocument.getLineOffset(nLine);
						if (nStartPos < flatNode.getStartOffset(region))
							nStartPos = flatNode.getStartOffset(region);
						command.offset = nStartPos;
						command.length = end - command.offset;
					}
				}
			}

			// add post-nearest white spaces to replace target
			if (position < structuredDocument.getLength()) {
				IStructuredDocumentRegion flatNode = structuredDocument.getRegionAtCharacterOffset(position);
				if (flatNode != null) {
					ITextRegion region = flatNode.getRegionAtCharacterOffset(position);
					if (region.getType() == CSSRegionContexts.CSS_S) {
						int nLine = structuredDocument.getLineOfOffset(position);
						String currentLineDelim = structuredDocument.getLineDelimiter(nLine);
						int nEndPos = structuredDocument.getLineOffset(nLine) + structuredDocument.getLineLength(nLine) - ((currentLineDelim != null) ? currentLineDelim.length() : 0);
						if (nEndPos > flatNode.getEndOffset(region))
							nEndPos = flatNode.getEndOffset(region);
						command.length = nEndPos - command.offset;
					}
				}
			}
		} catch (BadLocationException e) {
			// do not customize command
		}

	}

	private static int endsWith(String[] searchStrings, String text) {
		for (int i = 0; i < searchStrings.length; i++) {
			if (text.endsWith(searchStrings[i]))
				return i;
		}
		return -1;
	}

	private static int findEndOfWhiteSpace(IDocument document, int offset, int end) throws BadLocationException {
		while (offset < end) {
			char c = document.getChar(offset);
			if (c != ' ' && c != '\t') {
				return offset;
			}
			offset++;
		}
		return end;
	}

}

Back to the top