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