Skip to main content
summaryrefslogtreecommitdiffstats
blob: d419fcc353ac1e0c3b94c98f864be01184709c84 (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
/*******************************************************************************
 * Copyright (c) 2000, 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.jface.text.reconciler;

import java.util.List;
import java.util.ArrayList;


/**
 * Queue used by {@link org.eclipse.jface.text.reconciler.Reconciler} to manage
 * dirty regions. When a dirty region is inserted into the queue, the queue tries
 * to fold it into the neighboring dirty region.
 *
 * @see org.eclipse.jface.text.reconciler.Reconciler
 * @see org.eclipse.jface.text.reconciler.DirtyRegion
 */
class DirtyRegionQueue {

	/** The list of dirty regions. */
	private List fDirtyRegions= new ArrayList();

	/**
	 * Creates a new empty dirty region.
	 */
	public DirtyRegionQueue() {
		super();
	}

	/**
	 * Adds a dirty region to the end of the dirty-region queue.
	 *
	 * @param dr the dirty region to add
	 */
	public void addDirtyRegion(DirtyRegion dr) {
		// If the dirty region being added is directly after the last dirty
		// region on the queue then merge the two dirty regions together.
		DirtyRegion lastDR= getLastDirtyRegion();
		boolean wasMerged= false;
		if (lastDR != null)
			if (lastDR.getType() == dr.getType())
				if (lastDR.getType() == DirtyRegion.INSERT) {
					if (lastDR.getOffset() + lastDR.getLength() == dr.getOffset()) {
						lastDR.mergeWith(dr);
						wasMerged= true;
					}
				} else if (lastDR.getType() == DirtyRegion.REMOVE) {
					if (dr.getOffset() + dr.getLength() == lastDR.getOffset()) {
						lastDR.mergeWith(dr);
						wasMerged= true;
					}
				}

		if (!wasMerged)
			// Don't merge- just add the new one onto the queue.
			fDirtyRegions.add(dr);
	}

	/**
	 * Returns the last dirty region that was added to the queue.
	 *
	 * @return the last DirtyRegion on the queue
	 */
	private DirtyRegion getLastDirtyRegion() {
		int size= fDirtyRegions.size();
		return (size == 0 ? null : (DirtyRegion) fDirtyRegions.get(size - 1));
	}

	/**
	 * Returns the number of regions in the queue.
	 *
	 * @return the dirty-region queue-size
	 */
	public int getSize() {
		return fDirtyRegions.size();
	}

	/**
	 * Throws away all entries in the queue.
	 */
	public void purgeQueue() {
		fDirtyRegions.clear();
	}

	/**
	 * Removes and returns the first dirty region in the queue
	 *
	 * @return the next dirty region on the queue
	 */
	public DirtyRegion removeNextDirtyRegion() {
		if (fDirtyRegions.size() == 0)
			return null;
		DirtyRegion dr= (DirtyRegion) fDirtyRegions.get(0);
		fDirtyRegions.remove(0);
		return dr;
	}
}

Back to the top