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