comparison org.eclipse.text/src/org/eclipse/jface/text/projection/ProjectionTextStore.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children 5feec68b4556
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.jface.text.projection.ProjectionTextStore;
14
15 import org.eclipse.jface.text.projection.ProjectionMapping; // packageimport
16 import org.eclipse.jface.text.projection.ChildDocumentManager; // packageimport
17 import org.eclipse.jface.text.projection.SegmentUpdater; // packageimport
18 import org.eclipse.jface.text.projection.Segment; // packageimport
19 import org.eclipse.jface.text.projection.ProjectionDocument; // packageimport
20 import org.eclipse.jface.text.projection.FragmentUpdater; // packageimport
21 import org.eclipse.jface.text.projection.ProjectionDocumentEvent; // packageimport
22 import org.eclipse.jface.text.projection.ChildDocument; // packageimport
23 import org.eclipse.jface.text.projection.IMinimalMapping; // packageimport
24 import org.eclipse.jface.text.projection.Fragment; // packageimport
25 import org.eclipse.jface.text.projection.ProjectionDocumentManager; // packageimport
26
27
28 import java.lang.all;
29 import java.util.Set;
30
31
32 import org.eclipse.jface.text.BadLocationException;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.IRegion;
35 import org.eclipse.jface.text.ITextStore;
36 import org.eclipse.jface.text.Region;
37
38
39 /**
40 * A text store representing the projection defined by the given document
41 * information mapping.
42 *
43 * @since 3.0
44 */
45 class ProjectionTextStore : ITextStore {
46
47 /**
48 * Implementation of {@link IRegion} that can be reused
49 * by setting the offset and the length.
50 */
51 private static class ReusableRegion : IRegion {
52
53 private int fOffset;
54 private int fLength;
55
56 /*
57 * @see org.eclipse.jface.text.IRegion#getLength()
58 */
59 public int getLength() {
60 return fLength;
61 }
62
63 /*
64 * @see org.eclipse.jface.text.IRegion#getOffset()
65 */
66 public int getOffset() {
67 return fOffset;
68 }
69
70 /**
71 * Updates this region.
72 *
73 * @param offset the new offset
74 * @param length the new length
75 */
76 public void update(int offset, int length) {
77 fOffset= offset;
78 fLength= length;
79 }
80 }
81
82 /** The master document */
83 private IDocument fMasterDocument;
84 /** The document information mapping */
85 private IMinimalMapping fMapping;
86 /** Internal region used for querying the mapping. */
87 private ReusableRegion fReusableRegion;
88
89
90 /**
91 * Creates a new projection text store for the given master document and
92 * the given document information mapping.
93 *
94 * @param masterDocument the master document
95 * @param mapping the document information mapping
96 */
97 public this(IDocument masterDocument, IMinimalMapping mapping) {
98 fReusableRegion= new ReusableRegion();
99 fMasterDocument= masterDocument;
100 fMapping= mapping;
101 }
102
103 private void internalError() {
104 throw new IllegalStateException();
105 }
106
107 /*
108 * @see org.eclipse.jface.text.ITextStore#set(java.lang.String)
109 */
110 public void set(String contents) {
111
112 IRegion masterRegion= fMapping.getCoverage();
113 if (masterRegion is null)
114 internalError();
115
116 try {
117 fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), contents);
118 } catch (BadLocationException e) {
119 internalError();
120 }
121 }
122
123 /*
124 * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String)
125 */
126 public void replace(int offset, int length, String text) {
127 fReusableRegion.update(offset, length);
128 try {
129 IRegion masterRegion= fMapping.toOriginRegion(fReusableRegion);
130 fMasterDocument.replace(masterRegion.getOffset(), masterRegion.getLength(), text);
131 } catch (BadLocationException e) {
132 internalError();
133 }
134 }
135
136 /*
137 * @see org.eclipse.jface.text.ITextStore#getLength()
138 */
139 public int getLength() {
140 return fMapping.getImageLength();
141 }
142
143 /*
144 * @see org.eclipse.jface.text.ITextStore#get(int)
145 */
146 public char get(int offset) {
147 try {
148 int originOffset= fMapping.toOriginOffset(offset);
149 return fMasterDocument.getChar(originOffset);
150 } catch (BadLocationException e) {
151 internalError();
152 }
153
154 // unreachable
155 return cast(wchar) 0;
156 }
157
158 /*
159 * @see ITextStore#get(int, int)
160 */
161 public String get(int offset, int length) {
162 try {
163 IRegion[] fragments= fMapping.toExactOriginRegions(new Region(offset, length));
164 StringBuffer buffer= new StringBuffer();
165 for (int i= 0; i < fragments.length; i++) {
166 IRegion fragment= fragments[i];
167 buffer.append(fMasterDocument.get(fragment.getOffset(), fragment.getLength()));
168 }
169 return buffer.toString();
170 } catch (BadLocationException e) {
171 internalError();
172 }
173
174 // unreachable
175 return null;
176 }
177 }