25
|
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 org.eclipse.swt.custom.DefaultContent;
|
|
14
|
|
15 import org.eclipse.swt.SWT;
|
|
16 import org.eclipse.swt.SWTException;
|
|
17 import org.eclipse.swt.internal.Compatibility;
|
|
18 import org.eclipse.swt.widgets.TypedListener;
|
|
19 import org.eclipse.swt.custom.StyledTextContent;
|
|
20 import org.eclipse.swt.custom.TextChangeListener;
|
|
21 import org.eclipse.swt.custom.StyledTextEvent;
|
|
22 import org.eclipse.swt.custom.StyledTextListener;
|
|
23 import org.eclipse.swt.custom.StyledText;
|
|
24 import java.lang.all;
|
|
25
|
48
|
26 version(Tango){
|
25
|
27 static import tango.io.model.IFile;
|
48
|
28 } else { // Phobos
|
|
29 }
|
25
|
30
|
|
31
|
|
32 class DefaultContent : StyledTextContent {
|
|
33 private final static String LineDelimiter = tango.io.model.IFile.FileConst.NewlineString;
|
|
34
|
|
35 StyledTextListener[] textListeners; // stores text listeners for event sending
|
|
36 char[] textStore; // stores the actual text
|
|
37 int gapStart = -1; // the character position start of the gap
|
|
38 int gapEnd = -1; // the character position after the end of the gap
|
|
39 int gapLine = -1; // the line on which the gap exists, the gap will always be associated with one line
|
|
40 int highWatermark = 300;
|
|
41 int lowWatermark = 50;
|
|
42
|
|
43 int[][] lines; // array of character positions and lengths representing the lines of text
|
|
44 int lineCount_ = 0; // the number of lines of text
|
|
45 int expandExp = 1; // the expansion exponent, used to increase the lines array exponentially
|
|
46 int replaceExpandExp = 1; // the expansion exponent, used to increase the lines array exponentially
|
|
47
|
|
48 /**
|
|
49 * Creates a new DefaultContent and initializes it. A <code>StyledTextContent</> will always have
|
|
50 * at least one empty line.
|
|
51 */
|
|
52 this() {
|
|
53 lines = new int[][]( 50, 2 );
|
|
54 setText("");
|
|
55 }
|
|
56 /**
|
|
57 * Adds a line to the end of the line indexes array. Increases the size of the array if necessary.
|
|
58 * <code>lineCount</code> is updated to reflect the new entry.
|
|
59 * <p>
|
|
60 *
|
|
61 * @param start the start of the line
|
|
62 * @param length the length of the line
|
|
63 */
|
|
64 void addLineIndex(int start, int length) {
|
|
65 int size = lines.length;
|
|
66 if (lineCount_ is size) {
|
|
67 // expand the lines by powers of 2
|
|
68 int[][] newLines = new int[][]( size+Compatibility.pow2(expandExp), 2 );
|
|
69 System.arraycopy(lines, 0, newLines, 0, size);
|
|
70 lines = newLines;
|
|
71 expandExp++;
|
|
72 }
|
|
73 int[] range = [start, length];
|
|
74 lines[lineCount_] = range;
|
|
75 lineCount_++;
|
|
76 }
|
|
77 /**
|
|
78 * Adds a line index to the end of <code>linesArray</code>. Increases the
|
|
79 * size of the array if necessary and returns a new array.
|
|
80 * <p>
|
|
81 *
|
|
82 * @param start the start of the line
|
|
83 * @param length the length of the line
|
|
84 * @param linesArray the array to which to add the line index
|
|
85 * @param count the position at which to add the line
|
|
86 * @return a new array of line indexes
|
|
87 */
|
|
88 int[][] addLineIndex(int start, int length, int[][] linesArray, int count) {
|
|
89 int size = linesArray.length;
|
|
90 int[][] newLines = linesArray;
|
|
91 if (count is size) {
|
|
92 newLines = new int[][]( size+Compatibility.pow2(replaceExpandExp), 2 );
|
|
93 replaceExpandExp++;
|
|
94 System.arraycopy(linesArray, 0, newLines, 0, size);
|
|
95 }
|
|
96 int[] range = [start, length];
|
|
97 newLines[count] = range;
|
|
98 return newLines;
|
|
99 }
|
|
100 /**
|
|
101 * Adds a <code>TextChangeListener</code> listening for
|
|
102 * <code>TextChangingEvent</code> and <code>TextChangedEvent</code>. A
|
|
103 * <code>TextChangingEvent</code> is sent before changes to the text occur.
|
|
104 * A <code>TextChangedEvent</code> is sent after changes to the text
|
|
105 * occurred.
|
|
106 * <p>
|
|
107 *
|
|
108 * @param listener the listener
|
|
109 * @exception IllegalArgumentException <ul>
|
|
110 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
|
|
111 * </ul>
|
|
112 */
|
|
113 public void addTextChangeListener(TextChangeListener listener) {
|
|
114 if (listener is null) error(SWT.ERROR_NULL_ARGUMENT);
|
|
115 StyledTextListener typedListener = new StyledTextListener(listener);
|
|
116 textListeners ~= typedListener;
|
|
117 }
|
|
118 /**
|
|
119 * Adjusts the gap to accommodate a text change that is occurring.
|
|
120 * <p>
|
|
121 *
|
|
122 * @param position the position at which a change is occurring
|
|
123 * @param sizeHint the size of the change
|
|
124 * @param line the line where the gap will go
|
|
125 */
|
|
126 void adjustGap(int position, int sizeHint, int line) {
|
|
127 if (position is gapStart) {
|
|
128 // text is being inserted at the gap position
|
|
129 int size = (gapEnd - gapStart) - sizeHint;
|
|
130 if (lowWatermark <= size && size <= highWatermark)
|
|
131 return;
|
|
132 } else if ((position + sizeHint is gapStart) && (sizeHint < 0)) {
|
|
133 // text is being deleted at the gap position
|
|
134 int size = (gapEnd - gapStart) - sizeHint;
|
|
135 if (lowWatermark <= size && size <= highWatermark)
|
|
136 return;
|
|
137 }
|
|
138 moveAndResizeGap(position, sizeHint, line);
|
|
139 }
|
|
140 /**
|
|
141 * Calculates the indexes of each line in the text store. Assumes no gap exists.
|
|
142 * Optimized to do less checking.
|
|
143 */
|
|
144 void indexLines(){
|
|
145 int start = 0;
|
|
146 lineCount_ = 0;
|
|
147 int textLength = textStore.length;
|
|
148 int i;
|
|
149 for (i = start; i < textLength; i++) {
|
|
150 char ch = textStore[i];
|
|
151 if (ch is SWT.CR) {
|
|
152 // see if the next character is a LF
|
|
153 if (i + 1 < textLength) {
|
|
154 ch = textStore[i+1];
|
|
155 if (ch is SWT.LF) {
|
|
156 i++;
|
|
157 }
|
|
158 }
|
|
159 addLineIndex(start, i - start + 1);
|
|
160 start = i + 1;
|
|
161 } else if (ch is SWT.LF) {
|
|
162 addLineIndex(start, i - start + 1);
|
|
163 start = i + 1;
|
|
164 }
|
|
165 }
|
|
166 addLineIndex(start, i - start);
|
|
167 }
|
|
168 /**
|
|
169 * Returns whether or not the given character is a line delimiter. Both CR and LF
|
|
170 * are valid line delimiters.
|
|
171 * <p>
|
|
172 *
|
|
173 * @param ch the character to test
|
|
174 * @return true if ch is a delimiter, false otherwise
|
|
175 */
|
|
176 bool isDelimiter(char ch) {
|
|
177 if (ch is SWT.CR) return true;
|
|
178 if (ch is SWT.LF) return true;
|
|
179 return false;
|
|
180 }
|
|
181 /**
|
|
182 * Determine whether or not the replace operation is valid. DefaultContent will not allow
|
|
183 * the /r/n line delimiter to be split or partially deleted.
|
|
184 * <p>
|
|
185 *
|
|
186 * @param start start offset of text to replace
|
|
187 * @param replaceLength start offset of text to replace
|
|
188 * @param newText start offset of text to replace
|
|
189 * @return a bool specifying whether or not the replace operation is valid
|
|
190 */
|
|
191 protected bool isValidReplace(int start, int replaceLength, String newText){
|
|
192 if (replaceLength is 0) {
|
|
193 // inserting text, see if the \r\n line delimiter is being split
|
|
194 if (start is 0) return true;
|
|
195 if (start is getCharCount()) return true;
|
|
196 char before = getTextRange(start - 1, 1)[0];
|
|
197 if (before is '\r') {
|
|
198 char after = getTextRange(start, 1)[0];
|
|
199 if (after is '\n') return false;
|
|
200 }
|
|
201 } else {
|
|
202 // deleting text, see if part of a \r\n line delimiter is being deleted
|
|
203 char startChar = getTextRange(start, 1)[0];
|
|
204 if (startChar is '\n') {
|
|
205 // see if char before delete position is \r
|
|
206 if (start !is 0) {
|
|
207 char before = getTextRange(start - 1, 1)[0];
|
|
208 if (before is '\r') return false;
|
|
209 }
|
|
210 }
|
|
211 char endChar = getTextRange(start + replaceLength - 1, 1)[0];
|
|
212 if (endChar is '\r') {
|
|
213 // see if char after delete position is \n
|
|
214 if (start + replaceLength !is getCharCount()) {
|
|
215 char after = getTextRange(start + replaceLength, 1)[0];
|
|
216 if (after is '\n') return false;
|
|
217 }
|
|
218 }
|
|
219 }
|
|
220 return true;
|
|
221 }
|
|
222 /**
|
|
223 * Calculates the indexes of each line of text in the given range.
|
|
224 * <p>
|
|
225 *
|
|
226 * @param offset the logical start offset of the text lineate
|
|
227 * @param length the length of the text to lineate, includes gap
|
|
228 * @param numLines the number of lines to initially allocate for the line index array,
|
|
229 * passed in for efficiency (the exact number of lines may be known)
|
|
230 * @return a line indexes array where each line is identified by a start offset and
|
|
231 * a length
|
|
232 */
|
|
233 int[][] indexLines(int offset, int length, int numLines){
|
|
234 int[][] indexedLines = new int[][]( numLines, 2 );
|
|
235 int start = 0;
|
|
236 int lineCount_ = 0;
|
|
237 int i;
|
|
238 replaceExpandExp = 1;
|
|
239 for (i = start; i < length; i++) {
|
|
240 int location = i + offset;
|
|
241 if ((location >= gapStart) && (location < gapEnd)) {
|
|
242 // ignore the gap
|
|
243 } else {
|
|
244 char ch = textStore[location];
|
|
245 if (ch is SWT.CR) {
|
|
246 // see if the next character is a LF
|
|
247 if (location+1 < textStore.length) {
|
|
248 ch = textStore[location+1];
|
|
249 if (ch is SWT.LF) {
|
|
250 i++;
|
|
251 }
|
|
252 }
|
|
253 indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount_);
|
|
254 lineCount_++;
|
|
255 start = i + 1;
|
|
256 } else if (ch is SWT.LF) {
|
|
257 indexedLines = addLineIndex(start, i - start + 1, indexedLines, lineCount_);
|
|
258 lineCount_++;
|
|
259 start = i + 1;
|
|
260 }
|
|
261 }
|
|
262 }
|
|
263 int[][] newLines = new int[][]( lineCount_+1, 2 );
|
|
264 System.arraycopy(indexedLines, 0, newLines, 0, lineCount_);
|
|
265 int[] range = [start, i - start];
|
|
266 newLines[lineCount_] = range;
|
|
267 return newLines;
|
|
268 }
|
|
269 /**
|
|
270 * Inserts text.
|
|
271 * <p>
|
|
272 *
|
|
273 * @param position the position at which to insert the text
|
|
274 * @param text the text to insert
|
|
275 */
|
|
276 void insert(int position, String text) {
|
|
277 if (text.length is 0) return;
|
|
278
|
|
279 int startLine = getLineAtOffset(position);
|
|
280 int change = text.length;
|
|
281 bool endInsert = position is getCharCount();
|
|
282 adjustGap(position, change, startLine);
|
|
283
|
|
284 // during an insert the gap will be adjusted to start at
|
|
285 // position and it will be associated with startline, the
|
|
286 // inserted text will be placed in the gap
|
|
287 int startLineOffset = getOffsetAtLine(startLine);
|
|
288 // at this point, startLineLength will include the start line
|
|
289 // and all of the newly inserted text
|
|
290 int startLineLength = getPhysicalLine(startLine).length;
|
|
291
|
|
292 if (change > 0) {
|
|
293 // shrink gap
|
|
294 gapStart += (change);
|
|
295 for (int i = 0; i < text.length; i++) {
|
|
296 textStore[position + i]= text[i];
|
|
297 }
|
|
298 }
|
|
299
|
|
300 // figure out the number of new lines that have been inserted
|
|
301 int [][] newLines = indexLines(startLineOffset, startLineLength, 10);
|
|
302 // only insert an empty line if it is the last line in the text
|
|
303 int numNewLines = newLines.length - 1;
|
|
304 if (newLines[numNewLines][1] is 0) {
|
|
305 // last inserted line is a new line
|
|
306 if (endInsert) {
|
|
307 // insert happening at end of the text, leave numNewLines as
|
|
308 // is since the last new line will not be concatenated with another
|
|
309 // line
|
|
310 numNewLines += 1;
|
|
311 } else {
|
|
312 numNewLines -= 1;
|
|
313 }
|
|
314 }
|
|
315
|
|
316 // make room for the new lines
|
|
317 expandLinesBy(numNewLines);
|
|
318 // shift down the lines after the replace line
|
|
319 for (int i = lineCount_ - 1; i > startLine; i--) {
|
|
320 lines[i + numNewLines]=lines[i];
|
|
321 }
|
|
322 // insert the new lines
|
|
323 for (int i = 0; i < numNewLines; i++) {
|
|
324 newLines[i][0] += startLineOffset;
|
|
325 lines[startLine + i]=newLines[i];
|
|
326 }
|
|
327 // update the last inserted line
|
|
328 if (numNewLines < newLines.length) {
|
|
329 newLines[numNewLines][0] += startLineOffset;
|
|
330 lines[startLine + numNewLines] = newLines[numNewLines];
|
|
331 }
|
|
332
|
|
333 lineCount_ += numNewLines;
|
|
334 gapLine = getLineAtPhysicalOffset(gapStart);
|
|
335 }
|
|
336 /**
|
|
337 * Moves the gap and adjusts its size in anticipation of a text change.
|
|
338 * The gap is resized to actual size + the specified size and moved to the given
|
|
339 * position.
|
|
340 * <p>
|
|
341 *
|
|
342 * @param position the position at which a change is occurring
|
|
343 * @param size the size of the change
|
|
344 * @param newGapLine the line where the gap should be put
|
|
345 */
|
|
346 void moveAndResizeGap(int position, int size, int newGapLine) {
|
|
347 char[] content = null;
|
|
348 int oldSize = gapEnd - gapStart;
|
|
349 int newSize;
|
|
350 if (size > 0) {
|
|
351 newSize = highWatermark + size;
|
|
352 } else {
|
|
353 newSize = lowWatermark - size;
|
|
354 }
|
|
355 // remove the old gap from the lines information
|
|
356 if (gapExists()) {
|
|
357 // adjust the line length
|
|
358 lines[gapLine][1] = lines[gapLine][1] - oldSize;
|
|
359 // adjust the offsets of the lines after the gapLine
|
|
360 for (int i = gapLine + 1; i < lineCount_; i++) {
|
|
361 lines[i][0] = lines[i][0] - oldSize;
|
|
362 }
|
|
363 }
|
|
364
|
|
365 if (newSize < 0) {
|
|
366 if (oldSize > 0) {
|
|
367 // removing the gap
|
|
368 content = new char[textStore.length - oldSize];
|
|
369 System.arraycopy(textStore, 0, content, 0, gapStart);
|
|
370 System.arraycopy(textStore, gapEnd, content, gapStart, content.length - gapStart);
|
|
371 textStore = content;
|
|
372 }
|
|
373 gapStart = gapEnd = position;
|
|
374 return;
|
|
375 }
|
|
376 content = new char[textStore.length + (newSize - oldSize)];
|
|
377 int newGapStart = position;
|
|
378 int newGapEnd = newGapStart + newSize;
|
|
379 if (oldSize is 0) {
|
|
380 System.arraycopy(textStore, 0, content, 0, newGapStart);
|
|
381 System.arraycopy(textStore, newGapStart, content, newGapEnd, content.length - newGapEnd);
|
|
382 } else if (newGapStart < gapStart) {
|
|
383 int delta = gapStart - newGapStart;
|
|
384 System.arraycopy(textStore, 0, content, 0, newGapStart);
|
|
385 System.arraycopy(textStore, newGapStart, content, newGapEnd, delta);
|
|
386 System.arraycopy(textStore, gapEnd, content, newGapEnd + delta, textStore.length - gapEnd);
|
|
387 } else {
|
|
388 int delta = newGapStart - gapStart;
|
|
389 System.arraycopy(textStore, 0, content, 0, gapStart);
|
|
390 System.arraycopy(textStore, gapEnd, content, gapStart, delta);
|
|
391 System.arraycopy(textStore, gapEnd + delta, content, newGapEnd, content.length - newGapEnd);
|
|
392 }
|
|
393 textStore = content;
|
|
394 gapStart = newGapStart;
|
|
395 gapEnd = newGapEnd;
|
|
396
|
|
397 // add the new gap to the lines information
|
|
398 if (gapExists()) {
|
|
399 gapLine = newGapLine;
|
|
400 // adjust the line length
|
|
401 int gapLength = gapEnd - gapStart;
|
|
402 lines[gapLine][1] = lines[gapLine][1] + (gapLength);
|
|
403 // adjust the offsets of the lines after the gapLine
|
|
404 for (int i = gapLine + 1; i < lineCount_; i++) {
|
|
405 lines[i][0] = lines[i][0] + gapLength;
|
|
406 }
|
|
407 }
|
|
408 }
|
|
409 /**
|
|
410 * Returns the number of lines that are in the specified text.
|
|
411 * <p>
|
|
412 *
|
|
413 * @param startOffset the start of the text to lineate
|
|
414 * @param length the length of the text to lineate
|
|
415 * @return number of lines
|
|
416 */
|
|
417 int lineCount(int startOffset, int length){
|
|
418 if (length is 0) {
|
|
419 return 0;
|
|
420 }
|
|
421 int lineCount_ = 0;
|
|
422 int count = 0;
|
|
423 int i = startOffset;
|
|
424 if (i >= gapStart) {
|
|
425 i += gapEnd - gapStart;
|
|
426 }
|
|
427 while (count < length) {
|
|
428 if ((i >= gapStart) && (i < gapEnd)) {
|
|
429 // ignore the gap
|
|
430 } else {
|
|
431 char ch = textStore[i];
|
|
432 if (ch is SWT.CR) {
|
|
433 // see if the next character is a LF
|
|
434 if (i + 1 < textStore.length) {
|
|
435 ch = textStore[i+1];
|
|
436 if (ch is SWT.LF) {
|
|
437 i++;
|
|
438 count++;
|
|
439 }
|
|
440 }
|
|
441 lineCount_++;
|
|
442 } else if (ch is SWT.LF) {
|
|
443 lineCount_++;
|
|
444 }
|
|
445 count++;
|
|
446 }
|
|
447 i++;
|
|
448 }
|
|
449 return lineCount_;
|
|
450 }
|
|
451 /**
|
|
452 * Returns the number of lines that are in the specified text.
|
|
453 * <p>
|
|
454 *
|
|
455 * @param text the text to lineate
|
|
456 * @return number of lines in the text
|
|
457 */
|
|
458 int lineCount(String text){
|
|
459 int lineCount_ = 0;
|
|
460 int length = text.length;
|
|
461 for (int i = 0; i < length; i++) {
|
|
462 char ch = text[i];
|
|
463 if (ch is SWT.CR) {
|
|
464 if (i + 1 < length && text[i + 1] is SWT.LF) {
|
|
465 i++;
|
|
466 }
|
|
467 lineCount_++;
|
|
468 } else if (ch is SWT.LF) {
|
|
469 lineCount_++;
|
|
470 }
|
|
471 }
|
|
472 return lineCount_;
|
|
473 }
|
|
474 /**
|
|
475 * @return the logical length of the text store
|
|
476 */
|
|
477 public int getCharCount() {
|
|
478 int length = gapEnd - gapStart;
|
|
479 return (textStore.length - length);
|
|
480 }
|
|
481 /**
|
|
482 * Returns the line at <code>index</code> without delimiters.
|
|
483 * <p>
|
|
484 *
|
|
485 * @param index the index of the line to return
|
|
486 * @return the logical line text (i.e., without the gap)
|
|
487 * @exception IllegalArgumentException <ul>
|
|
488 * <li>ERROR_INVALID_ARGUMENT when index is out of range</li>
|
|
489 * </ul>
|
|
490 */
|
|
491 public String getLine(int index) {
|
|
492 if ((index >= lineCount_) || (index < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
|
|
493 int start = lines[index][0];
|
|
494 int length_ = lines[index][1];
|
|
495 int end = start + length_ - 1;
|
|
496 if (!gapExists() || (end < gapStart) || (start >= gapEnd)) {
|
|
497 // line is before or after the gap
|
|
498 while ((length_ - 1 >= 0) && isDelimiter(textStore[start+length_-1])) {
|
|
499 length_--;
|
|
500 }
|
|
501 return textStore[ start .. start + length_].dup;
|
|
502 } else {
|
|
503 // gap is in the specified range, strip out the gap
|
|
504 StringBuffer buf = new StringBuffer();
|
|
505 int gapLength = gapEnd - gapStart;
|
|
506 buf.append(textStore[ start .. gapStart ] );
|
|
507 buf.append(textStore[ gapEnd .. gapEnd + length_ - gapLength - (gapStart - start) ]);
|
|
508 length_ = buf.length;
|
|
509 while ((length_ - 1 >=0) && isDelimiter(buf.slice[length_ - 1])) {
|
|
510 length_--;
|
|
511 }
|
|
512 return buf.toString()[ 0 .. length_ ].dup;
|
|
513 }
|
|
514 }
|
|
515 /**
|
|
516 * Returns the line delimiter that should be used by the StyledText
|
|
517 * widget when inserting new lines. This delimiter may be different than the
|
|
518 * delimiter that is used by the <code>StyledTextContent</code> interface.
|
|
519 * <p>
|
|
520 *
|
|
521 * @return the platform line delimiter as specified in the line.separator
|
|
522 * system property.
|
|
523 */
|
|
524 public String getLineDelimiter() {
|
|
525 return LineDelimiter;
|
|
526 }
|
|
527 /**
|
|
528 * Returns the line at the given index with delimiters.
|
|
529 * <p>
|
|
530 * @param index the index of the line to return
|
|
531 * @return the logical line text (i.e., without the gap) with delimiters
|
|
532 */
|
|
533 String getFullLine(int index) {
|
|
534 int start = lines[index][0];
|
|
535 int length_ = lines[index][1];
|
|
536 int end = start + length_ - 1;
|
|
537 if (!gapExists() || (end < gapStart) || (start >= gapEnd)) {
|
|
538 // line is before or after the gap
|
|
539 return textStore[ start .. start + length_ ].dup;
|
|
540 } else {
|
|
541 // gap is in the specified range, strip out the gap
|
|
542 StringBuffer buffer = new StringBuffer();
|
|
543 int gapLength = gapEnd - gapStart;
|
|
544 buffer.append(textStore[ start .. gapStart ]);
|
|
545 buffer.append(textStore[ gapEnd .. gapEnd + length_ - gapLength - (gapStart - start) ]);
|
|
546 return buffer.toString().dup;
|
|
547 }
|
|
548 }
|
|
549 /**
|
|
550 * Returns the physical line at the given index (i.e., with delimiters and the gap).
|
|
551 * <p>
|
|
552 *
|
|
553 * @param index the line index
|
|
554 * @return the physical line
|
|
555 */
|
|
556 String getPhysicalLine(int index) {
|
|
557 int start = lines[index][0];
|
|
558 int length_ = lines[index][1];
|
|
559 return getPhysicalText(start, length_);
|
|
560 }
|
|
561 /**
|
|
562 * @return the number of lines in the text store
|
|
563 */
|
|
564 public int getLineCount(){
|
|
565 return lineCount_;
|
|
566 }
|
|
567 /**
|
|
568 * Returns the line at the given offset.
|
|
569 * <p>
|
|
570 *
|
|
571 * @param charPosition logical character offset (i.e., does not include gap)
|
|
572 * @return the line index
|
|
573 * @exception IllegalArgumentException <ul>
|
|
574 * <li>ERROR_INVALID_ARGUMENT when charPosition is out of range</li>
|
|
575 * </ul>
|
|
576 */
|
|
577 public int getLineAtOffset(int charPosition){
|
|
578 if ((charPosition > getCharCount()) || (charPosition < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
|
|
579 int position;
|
|
580 if (charPosition < gapStart) {
|
|
581 // position is before the gap
|
|
582 position = charPosition;
|
|
583 } else {
|
|
584 // position includes the gap
|
|
585 position = charPosition + (gapEnd - gapStart);
|
|
586 }
|
|
587
|
|
588 // if last line and the line is not empty you can ask for
|
|
589 // a position that doesn't exist (the one to the right of the
|
|
590 // last character) - for inserting
|
|
591 if (lineCount_ > 0) {
|
|
592 int lastLine = lineCount_ - 1;
|
|
593 if (position is lines[lastLine][0] + lines[lastLine][1])
|
|
594 return lastLine;
|
|
595 }
|
|
596
|
|
597 int high = lineCount_;
|
|
598 int low = -1;
|
|
599 int index = lineCount_;
|
|
600 while (high - low > 1) {
|
|
601 index = (high + low) / 2;
|
|
602 int lineStart = lines[index][0];
|
|
603 int lineEnd = lineStart + lines[index][1] - 1;
|
|
604 if (position <= lineStart) {
|
|
605 high = index;
|
|
606 } else if (position <= lineEnd) {
|
|
607 high = index;
|
|
608 break;
|
|
609 } else {
|
|
610 low = index;
|
|
611 }
|
|
612 }
|
|
613 return high;
|
|
614 }
|
|
615 /**
|
|
616 * Returns the line index at the given physical offset.
|
|
617 * <p>
|
|
618 *
|
|
619 * @param position physical character offset (i.e., includes gap)
|
|
620 * @return the line index
|
|
621 */
|
|
622 int getLineAtPhysicalOffset(int position){
|
|
623 int high = lineCount_;
|
|
624 int low = -1;
|
|
625 int index = lineCount_;
|
|
626 while (high - low > 1) {
|
|
627 index = (high + low) / 2;
|
|
628 int lineStart = lines[index][0];
|
|
629 int lineEnd = lineStart + lines[index][1] - 1;
|
|
630 if (position <= lineStart) {
|
|
631 high = index;
|
|
632 } else if (position <= lineEnd) {
|
|
633 high = index;
|
|
634 break;
|
|
635 } else {
|
|
636 low = index;
|
|
637 }
|
|
638 }
|
|
639 return high;
|
|
640 }
|
|
641 /**
|
|
642 * Returns the logical offset of the given line.
|
|
643 * <p>
|
|
644 *
|
|
645 * @param lineIndex index of line
|
|
646 * @return the logical starting offset of the line. When there are not any lines,
|
|
647 * getOffsetAtLine(0) is a valid call that should answer 0.
|
|
648 * @exception IllegalArgumentException <ul>
|
|
649 * <li>ERROR_INVALID_ARGUMENT when lineIndex is out of range</li>
|
|
650 * </ul>
|
|
651 */
|
|
652 public int getOffsetAtLine(int lineIndex) {
|
|
653 if (lineIndex is 0) return 0;
|
|
654 if ((lineIndex >= lineCount_) || (lineIndex < 0)) error(SWT.ERROR_INVALID_ARGUMENT);
|
|
655 int start = lines[lineIndex][0];
|
|
656 if (start > gapEnd) {
|
|
657 return start - (gapEnd - gapStart);
|
|
658 } else {
|
|
659 return start;
|
|
660 }
|
|
661 }
|
|
662 /**
|
|
663 * Increases the line indexes array to accommodate more lines.
|
|
664 * <p>
|
|
665 *
|
|
666 * @param numLines the number to increase the array by
|
|
667 */
|
|
668 void expandLinesBy(int numLines) {
|
|
669 int size = lines.length;
|
|
670 if (size - lineCount_ >= numLines) {
|
|
671 return;
|
|
672 }
|
|
673 int[][] newLines = new int[][]( size+Math.max(10, numLines), 2 );
|
|
674 System.arraycopy(lines, 0, newLines, 0, size);
|
|
675 lines = newLines;
|
|
676 }
|
|
677 /**
|
|
678 * Reports an SWT error.
|
|
679 * <p>
|
|
680 *
|
|
681 * @param code the error code
|
|
682 */
|
|
683 void error (int code) {
|
|
684 SWT.error(code);
|
|
685 }
|
|
686 /**
|
|
687 * Returns whether or not a gap exists in the text store.
|
|
688 * <p>
|
|
689 *
|
|
690 * @return true if gap exists, false otherwise
|
|
691 */
|
|
692 bool gapExists() {
|
|
693 return gapStart !is gapEnd;
|
|
694 }
|
|
695 /**
|
|
696 * Returns a string representing the continuous content of
|
|
697 * the text store.
|
|
698 * <p>
|
|
699 *
|
|
700 * @param start the physical start offset of the text to return
|
|
701 * @param length the physical length of the text to return
|
|
702 * @return the text
|
|
703 */
|
|
704 String getPhysicalText(int start, int length_) {
|
|
705 return textStore[ start .. start + length_ ].dup;
|
|
706 }
|
|
707 /**
|
|
708 * Returns a string representing the logical content of
|
|
709 * the text store (i.e., gap stripped out).
|
|
710 * <p>
|
|
711 *
|
|
712 * @param start the logical start offset of the text to return
|
|
713 * @param length the logical length of the text to return
|
|
714 * @return the text
|
|
715 */
|
|
716 public String getTextRange(int start, int length_) {
|
|
717 if (textStore is null)
|
|
718 return "";
|
|
719 if (length_ is 0)
|
|
720 return "";
|
|
721 int end= start + length_;
|
|
722 if (!gapExists() || (end < gapStart))
|
|
723 return textStore[ start .. start + length_].dup;
|
|
724 if (gapStart < start) {
|
|
725 int gapLength= gapEnd - gapStart;
|
|
726 return textStore[ start + gapLength .. start + gapLength + length_ ].dup;
|
|
727 }
|
|
728 StringBuffer buf = new StringBuffer();
|
|
729 buf.append(textStore[ start .. start + gapStart - start ] );
|
|
730 buf.append(textStore[ gapEnd .. gapEnd + end - gapStart ] );
|
|
731 return buf.toString().dup;
|
|
732 }
|
|
733 /**
|
|
734 * Removes the specified <code>TextChangeListener</code>.
|
|
735 * <p>
|
|
736 *
|
|
737 * @param listener the listener which should no longer be notified
|
|
738 *
|
|
739 * @exception IllegalArgumentException <ul>
|
|
740 * <li>ERROR_NULL_ARGUMENT when listener is null</li>
|
|
741 * </ul>
|
|
742 */
|
|
743 public void removeTextChangeListener(TextChangeListener listener){
|
|
744 if (listener is null) error(SWT.ERROR_NULL_ARGUMENT);
|
|
745 for (int i = 0; i < textListeners.length; i++) {
|
|
746 TypedListener typedListener = cast(TypedListener) textListeners[i];
|
|
747 if (typedListener.getEventListener () is listener) {
|
|
748 textListeners = textListeners[ 0 .. i ] ~ textListeners[ i+1 .. $ ];
|
|
749 break;
|
|
750 }
|
|
751 }
|
|
752 }
|
|
753 /**
|
|
754 * Replaces the text with <code>newText</code> starting at position <code>start</code>
|
|
755 * for a length of <code>replaceLength</code>. Notifies the appropriate listeners.
|
|
756 * <p>
|
|
757 *
|
|
758 * When sending the TextChangingEvent, <code>newLineCount</code> is the number of
|
|
759 * lines that are going to be inserted and <code>replaceLineCount</code> is
|
|
760 * the number of lines that are going to be deleted, based on the change
|
|
761 * that occurs visually. For example:
|
|
762 * <ul>
|
|
763 * <li>(replaceText,newText) is> (replaceLineCount,newLineCount)
|
|
764 * <li>("","\n") is> (0,1)
|
|
765 * <li>("\n\n","a") is> (2,0)
|
|
766 * </ul>
|
|
767 * </p>
|
|
768 *
|
|
769 * @param start start offset of text to replace
|
|
770 * @param replaceLength start offset of text to replace
|
|
771 * @param newText start offset of text to replace
|
|
772 *
|
|
773 * @exception SWTException <ul>
|
|
774 * <li>ERROR_INVALID_ARGUMENT when the text change results in a multi byte
|
|
775 * line delimiter being split or partially deleted. Splitting a line
|
|
776 * delimiter by inserting text between the CR and LF characters of the
|
|
777 * \r\n delimiter or deleting part of this line delimiter is not supported</li>
|
|
778 * </ul>
|
|
779 */
|
|
780 public void replaceTextRange(int start, int replaceLength, String newText){
|
|
781 // check for invalid replace operations
|
|
782 if (!isValidReplace(start, replaceLength, newText)) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
783
|
|
784 // inform listeners
|
|
785 StyledTextEvent event = new StyledTextEvent(this);
|
|
786 event.type = StyledText.TextChanging;
|
|
787 event.start = start;
|
|
788 event.replaceLineCount = lineCount(start, replaceLength);
|
|
789 event.text = newText;
|
|
790 event.newLineCount = lineCount(newText);
|
|
791 event.replaceCharCount = replaceLength;
|
|
792 event.newCharCount = newText.length;
|
|
793 sendTextEvent(event);
|
|
794
|
|
795 // first delete the text to be replaced
|
|
796 delete_(start, replaceLength, event.replaceLineCount + 1);
|
|
797 // then insert the new text
|
|
798 insert(start, newText);
|
|
799 // inform listeners
|
|
800 event = new StyledTextEvent(this);
|
|
801 event.type = StyledText.TextChanged;
|
|
802 sendTextEvent(event);
|
|
803 }
|
|
804 /**
|
|
805 * Sends the text listeners the TextChanged event.
|
|
806 */
|
|
807 void sendTextEvent(StyledTextEvent event) {
|
|
808 for (int i = 0; i < textListeners.length; i++) {
|
|
809 (cast(StyledTextListener)textListeners[i]).handleEvent(event);
|
|
810 }
|
|
811 }
|
|
812 /**
|
|
813 * Sets the content to text and removes the gap since there are no sensible predictions
|
|
814 * about where the next change will occur.
|
|
815 * <p>
|
|
816 *
|
|
817 * @param text the text
|
|
818 */
|
|
819 public void setText (String text){
|
|
820 textStore = text.dup;
|
|
821 gapStart = -1;
|
|
822 gapEnd = -1;
|
|
823 expandExp = 1;
|
|
824 indexLines();
|
|
825 StyledTextEvent event = new StyledTextEvent(this);
|
|
826 event.type = StyledText.TextSet;
|
|
827 event.text = "";
|
|
828 sendTextEvent(event);
|
|
829 }
|
|
830 /**
|
|
831 * Deletes text.
|
|
832 * <p>
|
|
833 * @param position the position at which the text to delete starts
|
|
834 * @param length the length of the text to delete
|
|
835 * @param numLines the number of lines that are being deleted
|
|
836 */
|
|
837 void delete_(int position, int length_, int numLines) {
|
|
838 if (length_ is 0) return;
|
|
839
|
|
840 int startLine = getLineAtOffset(position);
|
|
841 int startLineOffset = getOffsetAtLine(startLine);
|
|
842 int endLine = getLineAtOffset(position + length_);
|
|
843
|
|
844 String endText = "";
|
|
845 bool splittingDelimiter = false;
|
|
846 if (position + length_ < getCharCount()) {
|
|
847 endText = getTextRange(position + length_ - 1, 2);
|
|
848 if ((endText[0] is SWT.CR) && (endText[1] is SWT.LF)) {
|
|
849 splittingDelimiter = true;
|
|
850 }
|
|
851 }
|
|
852
|
|
853 adjustGap(position + length_, -length_, startLine);
|
|
854 int [][] oldLines = indexLines(position, length_ + (gapEnd - gapStart), numLines);
|
|
855
|
|
856 // enlarge the gap - the gap can be enlarged either to the
|
|
857 // right or left
|
|
858 if (position + length_ is gapStart) {
|
|
859 gapStart -= length_;
|
|
860 } else {
|
|
861 gapEnd += length_;
|
|
862 }
|
|
863
|
|
864 // figure out the length of the new concatenated line, do so by
|
|
865 // finding the first line delimiter after position
|
|
866 int j = position;
|
|
867 bool eol = false;
|
|
868 while (j < textStore.length && !eol) {
|
|
869 if (j < gapStart || j >= gapEnd) {
|
|
870 char ch = textStore[j];
|
|
871 if (isDelimiter(ch)) {
|
|
872 if (j + 1 < textStore.length) {
|
|
873 if (ch is SWT.CR && (textStore[j+1] is SWT.LF)) {
|
|
874 j++;
|
|
875 }
|
|
876 }
|
|
877 eol = true;
|
|
878 }
|
|
879 }
|
|
880 j++;
|
|
881 }
|
|
882 // update the line where the deletion started
|
|
883 lines[startLine][1] = (position - startLineOffset) + (j - position);
|
|
884 // figure out the number of lines that have been deleted
|
|
885 int numOldLines = oldLines.length - 1;
|
|
886 if (splittingDelimiter) numOldLines -= 1;
|
|
887 // shift up the lines after the last deleted line, no need to update
|
|
888 // the offset or length of the lines
|
|
889 for (int i = endLine + 1; i < lineCount_; i++) {
|
|
890 lines[i - numOldLines] = lines[i];
|
|
891 }
|
|
892 lineCount_ -= numOldLines;
|
|
893 gapLine = getLineAtPhysicalOffset(gapStart);
|
|
894 }
|
|
895
|
|
896 /++
|
|
897 + SWT extension
|
|
898 +/
|
|
899 int utf8AdjustOffset( int offset ){
|
|
900 if (textStore is null)
|
|
901 return offset;
|
|
902 if (offset is 0)
|
|
903 return offset;
|
|
904 if( offset >= textStore.length ){
|
|
905 return offset;
|
|
906 }
|
|
907 if (!gapExists() || (offset < gapStart)){
|
|
908 while( textStore[offset] & 0xC0 is 0x80 ){
|
|
909 offset--;
|
|
910 }
|
|
911 return offset;
|
|
912 }
|
|
913 int gapLength= gapEnd - gapStart;
|
|
914 if( offset+gapLength >= textStore.length ){
|
|
915 return offset;
|
|
916 }
|
|
917 while( textStore[offset+gapLength] & 0xC0 is 0x80 ){
|
|
918 offset--;
|
|
919 }
|
|
920 return offset;
|
|
921 }
|
|
922
|
|
923
|
|
924 }
|