Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77631a334d1e7392ea7945d36ef3095c9588d234 (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved.   This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* 
* Contributors:
*   IBM - Initial API and implementation
*   Jens Lukowski/Innoopract - initial renaming/restructuring
* 
*/
package org.eclipse.wst.sse.core.internal.text;



import org.eclipse.jface.text.BadLocationException;
import org.eclipse.wst.sse.core.events.RegionChangedEvent;
import org.eclipse.wst.sse.core.events.StructuredDocumentEvent;
import org.eclipse.wst.sse.core.exceptions.SourceEditingRuntimeException;
import org.eclipse.wst.sse.core.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.core.text.ITextRegionList;
import org.eclipse.wst.sse.core.util.Assert;
import org.eclipse.wst.sse.core.util.Debug;
import org.eclipse.wst.sse.core.util.Utilities;


public class BasicStructuredDocumentRegion extends Object implements IStructuredDocumentRegion {
	protected int start;
	protected int length;

	private ITextRegionList _regions;
	/**
	 * was this node terminated normally
	 */
	// 0 == false, 1 == true
	private byte hasEnd = 0;
	private IStructuredDocumentRegion previous = null;
	private IStructuredDocumentRegion next = null;
	//private String fType;
	/**
	 * allow a pointer back to this nodes model
	 */
	private IStructuredDocument fParentDocument;
	private static final String UNDEFINED = "org.eclipse.wst.sse.structuredDocument.UNDEFINED"; //$NON-NLS-1$
	private static final String TEXT_STORE_NOT_ASSIGNED = "text store not assigned yet"; //$NON-NLS-1$

	public BasicStructuredDocumentRegion() {
		super();
		_regions = new TextRegionListImpl();

	}

	public void addRegion(ITextRegion aRegion) {
		_getRegions().add(aRegion);
	}

	public void adjustLengthWith(int i) {
		length += i;
	}

	public void adjustStart(int i) {
		start += i;
	}

	/**
	 * getEnd and getEndOffset are the same only for IStructuredDocumentRegions
	 */
	public int getEnd() {
		return start + length;
	}

	/**
	 * getEnd and getEndOffset are the same only for IStructuredDocumentRegions
	 */
	public int getEndOffset() {
		return getEnd();
	}

	//	public IStructuredDocumentRegion getFirstNode() {
	//		IStructuredDocumentRegion aNode = (IStructuredDocumentRegion) fOwningDocument.getFirstStructuredDocumentRegion();
	//		return aNode;
	//	}
	public ITextRegion getFirstRegion() {
		if (_getRegions() == null)
			return null;
		return _getRegions().get(0);
	}

	public String getFullText(ITextRegion aRegion) {
		String result = null;
		try {
			int regionStart = aRegion.getStart();
			int regionLength = aRegion.getLength();
			result = fParentDocument.get(start + regionStart, regionLength);
		}
		catch (BadLocationException e) {
			throw new SourceEditingRuntimeException(e);
		}
		return result;
	}

	public String getFullText(String context) {
		// DMW: looping is faster than enumeration, 
		// so switched around 2/12/03
		//Enumeration e = getRegions().elements();
		ITextRegion region = null;
		String result = ""; //$NON-NLS-1$
		int length = getRegions().size();
		for (int i = 0; i < length; i++) {
			region = getRegions().get(i);
			if (region.getType() == context)
				result += getFullText(region);
		}
		return result;
	}

	public ITextRegion getLastRegion() {
		if (_getRegions() == null)
			return null;
		return _getRegions().get(_getRegions().size() - 1);
	}

	public int getLength() {
		return length;
	}

	public IStructuredDocumentRegion getNext() {
		return next;
	}

	public int getNumberOfRegions() {
		return _getRegions().size();
	}

	public IStructuredDocumentRegion getPrevious() {
		return previous;
	}

	/**
	 * The parameter offset refers to the overall offset in the document.
	 */
	public ITextRegion getRegionAtCharacterOffset(int offset) {
		ITextRegion result = null;
		if (_getRegions() != null) {
			// transform the requested offset to the "scale" that
			// regions are stored in, which are all relative to the
			// start point.
			//int transformedOffset = offset - getStartOffset();
			//
			int length = getRegions().size();
			for (int i = 0; i < length; i++) {
				ITextRegion region = getRegions().get(i);
				if (Debug.debugStructuredDocument) {
					System.out.println("region(s) in IStructuredDocumentRegion::getRegionAtCharacterOffset: " + region); //$NON-NLS-1$
					System.out.println("       requested offset: " + offset); //$NON-NLS-1$
					//System.out.println("       transformedOffset: " + transformedOffset); //$NON-NLS-1$
					System.out.println("       region start: " + region.getStart()); //$NON-NLS-1$
					System.out.println("       region end: " + region.getEnd()); //$NON-NLS-1$
					System.out.println("       region type: " + region.getType()); //$NON-NLS-1$
					System.out.println("       region class: " + region.getClass()); //$NON-NLS-1$

				}
				if ((getStartOffset(region) <= offset) && (offset < getEndOffset(region))) {
					result = region;
					break;
				}
			}
		}
		return result;
	}

	/** 
	 * getStart and getStartOffset are the same 
	 * only for IStrucutredDocumentRegions
	 */
	public int getStart() {
		return start;
	}

	/** 
	 * getStart and getStartOffset are the same 
	 * only for IStrucutredDocumentRegions
	 */
	public int getStartOffset() {
		return getStart();
	}

	public String getText() {
		String result = null;
		try {
			if (fParentDocument == null) {
				// likely to happen during inspecting
				result = TEXT_STORE_NOT_ASSIGNED;
			}
			else {
				result = fParentDocument.get(start, length);
			}
		}
		catch (BadLocationException e) {
			throw new SourceEditingRuntimeException(e);
		}
		return result;
	}

	public String getText(ITextRegion aRegion) {
		// assert: aRegion can not be null
		// (might be performance hit if literally put in assert call, 
		// but aRegion can not be null). Needs to be checked 
		// by calling code.
		try {
			return fParentDocument.get(this.getStartOffset(aRegion), aRegion.getTextLength());
		}
		catch (BadLocationException e) {
			Logger.logException(e);
		}
		return ""; //$NON-NLS-1$
	}

	/**
	 * Returns the text of the first region with the matching context type
	 */
	public String getText(String context) {
		// DMW: looping is faster than enumeration, 
		// so switched around 2/12/03
		//Enumeration e = getRegions().elements();
		ITextRegion region = null;
		String result = ""; //$NON-NLS-1$
		int length = getRegions().size();
		for (int i = 0; i < length; i++) {
			region = getRegions().get(i);
			if (region.getType() == context) {
				result = getText(region);
				break;
			}
		}
		return result;
	}

	/**
	 * @return int
	 */
	public int getTextEndOffset() {
		ITextRegion region = _getRegions().get(_getRegions().size() - 1);
		return getStartOffset() + region.getTextEnd();
	}

	/**
	 * Provides the type of IStructuredDocumentRegion ... not to be confused with type of XML node!
	 */
	// For now return a String.
	public String getType() {
		// for a "named" XML node (e.g. start tag), but type of
		// flatnode is "named xml node" (which is always the second region).
		// Therefore, if there's two or more regions, the type for the flatnode
		// will be the type of the second region. Otherwise it will be the type
		// of the first region.
		// Note: this can be refined as needed.
		String result = UNDEFINED; //"IStructuredDocumentRegion.UNDEFINED"; //$NON-NLS-1$
		// we are assume there is always at least one region in a flatnode!
		result = getRegions().get(0).getType();
		// typcially subclassed, if something other than first region is
		// desired
		return result;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean isEnded() {
		return (hasEnd == 1);
	}

	/**
	 * 
	 * @param newHasEnd boolean
	 */
	public void setEnded(boolean newHasEnd) {
		if (newHasEnd) {
			hasEnd = 1;
		}
		else {
			hasEnd = 0;
		}
	}

	public void setLength(int newLength) {
		//textLength = newLength;
		length = newLength;
	}

	public void setNext(IStructuredDocumentRegion newNext) {
		next = newNext;
	}

	public void setPrevious(IStructuredDocumentRegion newPrevious) {
		previous = newPrevious;
	}

	public void setStart(int newStart) {
		start = newStart;
	}

	public String toString() {
		// NOTE: if the document held by any region has been updated and the region offsets have not
		// yet been updated, the output from this method invalid.
		// Also note, this method can not be changed, without "breaking" 
		// unit tests, since some of them compare current results to previous
		// results. 
		String result = null;
		result = "[" + getStart() + ", " + getEnd() + "] (" + getText() + ")"; //$NON-NLS-4$//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
		return result;
	}

	private void updateDownStreamRegions(ITextRegion changedRegion, int lengthDifference) {
		int listLength = _getRegions().size();
		int startIndex = 0;
		// first, loop through to find index of where to start
		for (int i = 0; i < listLength; i++) {
			ITextRegion region = _getRegions().get(i);
			if (region == changedRegion) {
				startIndex = i;
				break;
			}
		}
		// now, beginning one past the one that was changed, loop
		// through to end of list, adjusting the start postions.
		startIndex++;
		for (int j = startIndex; j < listLength; j++) {
			ITextRegion region = _getRegions().get(j);
			region.adjustStart(lengthDifference);
		}
	}

	// This is a language specific method (e.g. HTML, Java, Prolog, etc.) so could/should be made configurable.
	/* (non-Javadoc)
	 */
	//	public StructuredDocumentEvent updateModel(Object requester, String changes, int start, int end, IStructuredDocumentRegion flatnode) {
	public StructuredDocumentEvent updateModel(Object requester, IStructuredDocumentRegion structuredDocumentRegion, String changes, int requestStart, int lengthToReplace) {
		StructuredDocumentEvent result = null;
		int lengthDifference = Utilities.calculateLengthDifference(changes, lengthToReplace);
		// Get the region pointed to by the requestStart postion, and give that region a chance to effect
		// the update.
		ITextRegion region = getRegionAtCharacterOffset(requestStart);
		// if there is no region, then the requested changes must come right after the
		// node (and right after the last region). This happens, for example, when someone
		// types something at the end of the document, or more commonly, when they are right
		// at the beginning of one node, and the dirty start is therefore calculated to be the
		// previous node.
		// So, in this case, we'll give the last region a chance to see if it wants to
		// swallow the requested changes --  but only for inserts -- deletes and "replaces"
		// should be reparsed if they are in these border regions, and only if the 
		if ((region == null) && (lengthToReplace == 0)) {
			region = _getRegions().get(_getRegions().size() - 1);
			// make sure the region is contiguous
			if (getEndOffset(region) == requestStart) {
				result = region.updateModel(requester, this, changes, requestStart, lengthToReplace);
			}
		}
		else {
			if (region != null) {
				//
				// If the requested change spans more than one region, then
				// we don't give the region a chance to update.
				if ((containsOffset(region, requestStart)) && (containsOffset(region, requestStart + lengthToReplace))) {
					result = region.updateModel(requester, this, changes, requestStart, lengthToReplace);
				}
			}
		}
		// if result is not null, then we need to update the start and end
		// postions of the regions that follow this one
		// if result is null, then apply the flatnode specific checks on what it can change
		// (i.e. more than one region, but no change to the node itself)
		if (result != null) {
			// That is, a region decided it could handle the change and created
			// a region changed event.
			Assert.isTrue(result instanceof RegionChangedEvent, "Program Error"); //$NON-NLS-1$
			updateDownStreamRegions(((RegionChangedEvent) result).getRegion(), lengthDifference);
			// PLUS, we need to update our own node end point (length)
			setLength(getLength() + lengthDifference);
		}

		return result;
	}

	/* (non-Javadoc)
	 */
	public IStructuredDocument getParentDocument() {

		return fParentDocument;
	}

	public void setParentDocument(IStructuredDocument document) {
		fParentDocument = document;

	}

	/* (non-Javadoc)
	 */
	public boolean containsOffset(ITextRegion containedRegion, int offset) {
		return getStartOffset(containedRegion) <= offset && offset < getEndOffset(containedRegion);
	}

	/* (non-Javadoc)
	 */
	public int getStartOffset(ITextRegion containedRegion) {
		// assert: containedRegion can not be null
		// (might be performance hit if literally put in assert call, 
		// but containedRegion can not be null). Needs to be checked 
		// by calling code.
		return getStartOffset() + containedRegion.getStart();
	}

	/* (non-Javadoc)
	 */
	public int getEndOffset(ITextRegion containedRegion) {
		return getStartOffset(containedRegion) + containedRegion.getLength();
	}

	/* (non-Javadoc)
	 */
	public int getTextEndOffset(ITextRegion containedRegion) {
		return getStartOffset(containedRegion) + containedRegion.getTextLength();
	}

	/* (non-Javadoc)
	 */
	public boolean containsOffset(int i) {

		return getStartOffset() <= i && i < getEndOffset();
	}

	/* (non-Javadoc)
	 */
	public String getFullText() {
		try {
			return getParentDocument().get(start, length);
		}
		catch (BadLocationException e) {
			// program error
			throw new SourceEditingRuntimeException(e);
		}
	}

	/* (non-Javadoc)
	 */
	public int getTextEnd() {

		//return start + textLength;
		return start + length;
	}

	/* (non-Javadoc)
	 */
	public int getTextLength() {
		//return textLength;
		return length;
	}

	/* (non-Javadoc)
	 */
	public void adjust(int i) {
		start += i;
	}

	/* (non-Javadoc)
	 */
	public void equatePositions(ITextRegion region) {
		start = region.getStart();
		length = region.getLength();
		//textLength = region.getTextLength();

	}

	public ITextRegionList getRegions() {
		return _getRegions();
	}

	/**
	 * Even inside-this class uses of 'regions' should use this method, 
	 * as this is where (soft) memory management/reparsing, etc., will be
	 * centralized. 
	 */
	private ITextRegionList _getRegions() {

		return _regions;
	}

	public void setRegions(ITextRegionList containedRegions) {
		_regions = containedRegions;
	}

	public boolean sameAs(ITextRegion oldRegion, IStructuredDocumentRegion newDocumentRegion, ITextRegion newRegion, int shift) {
		boolean result = false;
		// if any region is null, we return false (even if both are!)
		if ((oldRegion != null) && (newRegion != null)) {
			// if the regions are the same instance, they are equal
			if (oldRegion == newRegion) {
				result = true;
			}
			else {
				// this is the non-trivial part
				// note: we change for type first, then start offset and end offset,
				// since that would decide many cases right away and avoid the text comparison
				if (oldRegion.getType() == newRegion.getType()) {
					if (sameOffsetsAs(oldRegion, newDocumentRegion, newRegion, shift)) {
						if (sameTextAs(oldRegion, newDocumentRegion, newRegion, shift)) {
							result = true;
						}
					}
				}
			}

		}

		return result;
	}

	private boolean sameOffsetsAs(ITextRegion oldRegion, IStructuredDocumentRegion documentRegion, ITextRegion newRegion, int shift) {
		if (getStartOffset(oldRegion) == documentRegion.getStartOffset(newRegion) - shift) {
			if (getEndOffset(oldRegion) == documentRegion.getEndOffset(newRegion) - shift) {
				return true;
			}
		}
		return false;
	}

	private boolean sameTextAs(ITextRegion oldRegion, IStructuredDocumentRegion documentRegion, ITextRegion newRegion, int shift) {
		boolean result = false;
		try {
			if (getText(oldRegion).equals(documentRegion.getText(newRegion))) {
				result = true;
			}
		}
		catch (SourceEditingRuntimeException e) {
			if (e.getOriginalException() instanceof BadLocationException) {
				// this happens normally if document has been deleted from
				// and therefore regions are not equal
				result = false;
			}
			else {
				throw e;
			}
		}

		return result;
	}

	public boolean sameAs(IStructuredDocumentRegion region, int shift) {
		boolean result = false;
		// if region == null, we return false;
		if (region != null) {
			// if the regions are the same instance, they are equal
			if (this == region) {
				result = true;
			}
			else {
				// this is the non-trivial part
				// note: we change for type first, then start offset and end offset,
				// since that would decide many cases right away and avoid the text comparison
				if (getType() == region.getType()) {
					if (sameOffsetsAs(region, shift) && sameTextAs(region, shift)) {
						result = true;
					}
				}

			}
		}
		return result;
	}

	private boolean sameOffsetsAs(IStructuredDocumentRegion region, int shift) {
		if (getStartOffset() == region.getStartOffset() - shift) {
			if (getEndOffset() == region.getEndOffset() - shift) {
				return true;
			}
		}
		return false;
	}

	private boolean sameTextAs(IStructuredDocumentRegion region, int shift) {
		boolean result = false;
		try {
			if (getText().equals(region.getText())) {
				result = true;
			}
		}
		catch (SourceEditingRuntimeException e) {
			if (e.getOriginalException() instanceof BadLocationException || e.getOriginalException() instanceof StringIndexOutOfBoundsException) {
				// this happens normally if document has been deleted from
				// and therefore regions are not equal
				result = false;
			}
			else {
				throw e;
			}
		}

		return result;
	}

	/* (non-Javadoc)
	 */
	public void adjustTextLength(int i) {
		// not supported

	}

}

Back to the top