comparison dwtx/jface/text/reconciler/DirtyRegionQueue.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.text.reconciler.DirtyRegionQueue;
14
15 import dwt.dwthelper.utils;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20
21 /**
22 * Queue used by {@link dwtx.jface.text.reconciler.AbstractReconciler} to manage
23 * dirty regions. When a dirty region is inserted into the queue, the queue tries
24 * to fold it into the neighboring dirty region.
25 *
26 * @see dwtx.jface.text.reconciler.AbstractReconciler
27 * @see dwtx.jface.text.reconciler.DirtyRegion
28 */
29 class DirtyRegionQueue {
30
31 /** The list of dirty regions. */
32 private List fDirtyRegions= new ArrayList();
33
34 /**
35 * Creates a new empty dirty region.
36 */
37 public DirtyRegionQueue() {
38 super();
39 }
40
41 /**
42 * Adds a dirty region to the end of the dirty-region queue.
43 *
44 * @param dr the dirty region to add
45 */
46 public void addDirtyRegion(DirtyRegion dr) {
47 // If the dirty region being added is directly after the last dirty
48 // region on the queue then merge the two dirty regions together.
49 DirtyRegion lastDR= getLastDirtyRegion();
50 bool wasMerged= false;
51 if (lastDR !is null)
52 if (lastDR.getType() is dr.getType())
53 if (lastDR.getType() is DirtyRegion.INSERT) {
54 if (lastDR.getOffset() + lastDR.getLength() is dr.getOffset()) {
55 lastDR.mergeWith(dr);
56 wasMerged= true;
57 }
58 } else if (lastDR.getType() is DirtyRegion.REMOVE) {
59 if (dr.getOffset() + dr.getLength() is lastDR.getOffset()) {
60 lastDR.mergeWith(dr);
61 wasMerged= true;
62 }
63 }
64
65 if (!wasMerged)
66 // Don't merge- just add the new one onto the queue.
67 fDirtyRegions.add(dr);
68 }
69
70 /**
71 * Returns the last dirty region that was added to the queue.
72 *
73 * @return the last DirtyRegion on the queue
74 */
75 private DirtyRegion getLastDirtyRegion() {
76 int size= fDirtyRegions.size();
77 return (size is 0 ? null : (DirtyRegion) fDirtyRegions.get(size - 1));
78 }
79
80 /**
81 * Returns the number of regions in the queue.
82 *
83 * @return the dirty-region queue-size
84 */
85 public int getSize() {
86 return fDirtyRegions.size();
87 }
88
89 /**
90 * Throws away all entries in the queue.
91 */
92 public void purgeQueue() {
93 fDirtyRegions.clear();
94 }
95
96 /**
97 * Removes and returns the first dirty region in the queue
98 *
99 * @return the next dirty region on the queue
100 */
101 public DirtyRegion removeNextDirtyRegion() {
102 if (fDirtyRegions.size() is 0)
103 return null;
104 DirtyRegion dr= (DirtyRegion) fDirtyRegions.get(0);
105 fDirtyRegions.remove(0);
106 return dr;
107 }
108 }