129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 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.DirtyRegion;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import dwtx.jface.text.ITypedRegion;
|
|
18
|
|
19
|
|
20 /**
|
|
21 * A dirty region describes a document range which has been changed.
|
|
22 */
|
|
23 public class DirtyRegion : ITypedRegion {
|
|
24
|
|
25 /**
|
|
26 * Identifies an insert operation.
|
|
27 */
|
|
28 final static public String INSERT= "__insert"; //$NON-NLS-1$
|
|
29 /**
|
|
30 * Identifies a remove operation.
|
|
31 */
|
|
32 final static public String REMOVE= "__remove"; //$NON-NLS-1$
|
|
33
|
|
34 /** The region's offset. */
|
|
35 private int fOffset;
|
|
36 /** The region's length. */
|
|
37 private int fLength;
|
|
38 /** Indicates the type of the applied change. */
|
|
39 private String fType;
|
|
40 /** The text which has been inserted. */
|
|
41 private String fText;
|
|
42
|
|
43 /**
|
|
44 * Creates a new dirty region.
|
|
45 *
|
|
46 * @param offset the offset within the document where the change occurred
|
|
47 * @param length the length of the text within the document that changed
|
|
48 * @param type the type of change that this region represents: {@link #INSERT} {@link #REMOVE}
|
|
49 * @param text the substitution text
|
|
50 */
|
|
51 public DirtyRegion(int offset, int length, String type, String text) {
|
|
52 fOffset= offset;
|
|
53 fLength= length;
|
|
54 fType= normalizeTypeValue(type);
|
|
55 fText= text;
|
|
56 }
|
|
57
|
|
58 /**
|
|
59 * Computes the normalized type value to ensure that the implementation can use object identity rather
|
|
60 * than equality.
|
|
61 *
|
|
62 * @param type the type value
|
|
63 * @return the normalized type value or <code>null</code>
|
|
64 * @since 3.1
|
|
65 */
|
|
66 private String normalizeTypeValue(String type) {
|
|
67 if (INSERT.equals(type))
|
|
68 return INSERT;
|
|
69 if (REMOVE.equals(type))
|
|
70 return REMOVE;
|
|
71 return null;
|
|
72 }
|
|
73
|
|
74 /*
|
|
75 * @see ITypedRegion#getOffset()
|
|
76 */
|
|
77 public int getOffset() {
|
|
78 return fOffset;
|
|
79 }
|
|
80
|
|
81 /*
|
|
82 * @see ITypedRegion#getLength()
|
|
83 */
|
|
84 public int getLength() {
|
|
85 return fLength;
|
|
86 }
|
|
87
|
|
88 /*
|
|
89 * @see ITypedRegion#getType
|
|
90 */
|
|
91 public String getType() {
|
|
92 return fType;
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * Returns the text that changed as part of the region change.
|
|
97 *
|
|
98 * @return the changed text
|
|
99 */
|
|
100 public String getText() {
|
|
101 return fText;
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Modify the receiver so that it encompasses the region specified by the dirty region.
|
|
106 *
|
|
107 * @param dr the dirty region with which to merge
|
|
108 */
|
|
109 void mergeWith(DirtyRegion dr) {
|
|
110 int start= Math.min(fOffset, dr.fOffset);
|
|
111 int end= Math.max(fOffset + fLength, dr.fOffset + dr.fLength);
|
|
112 fOffset= start;
|
|
113 fLength= end - start;
|
|
114 fText= (dr.fText is null ? fText : (fText is null) ? dr.fText : fText + dr.fText);
|
|
115 }
|
|
116 }
|