129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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.Document;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18 /**
|
|
19 * Default document implementation. Uses a {@link dwtx.jface.text.GapTextStore} wrapped
|
|
20 * inside a {@link dwtx.jface.text.CopyOnWriteTextStore} as text store.
|
|
21 * <p>
|
|
22 * The used line tracker considers the following strings as line delimiters: "\n", "\r", "\r\n".
|
|
23 * </p>
|
|
24 * <p>
|
|
25 * The document is ready to use. It has a default position category for which a default position
|
|
26 * updater is installed.
|
|
27 * </p>
|
|
28 * <p>
|
|
29 * <strong>Performance:</strong> The implementation should perform reasonably well for typical
|
|
30 * source code documents. It is not designed for very large documents of a size of several
|
|
31 * megabytes. Space-saving implementations are initially used for both the text store and the line
|
|
32 * tracker; the first modification after a {@link #set(String) set} incurs the cost to transform the
|
|
33 * document structures to efficiently handle updates.
|
|
34 * </p>
|
|
35 * <p>
|
|
36 * See {@link GapTextStore} and <code>TreeLineTracker</code> for algorithmic behavior of the used
|
|
37 * document structures.
|
|
38 * </p>
|
|
39 *
|
|
40 * @see dwtx.jface.text.GapTextStore
|
|
41 * @see dwtx.jface.text.CopyOnWriteTextStore
|
|
42 */
|
|
43 public class Document : AbstractDocument {
|
|
44 /**
|
|
45 * Creates a new empty document.
|
|
46 */
|
|
47 public Document() {
|
|
48 super();
|
|
49 setTextStore(new CopyOnWriteTextStore(new GapTextStore()));
|
|
50 setLineTracker(new DefaultLineTracker());
|
|
51 completeInitialization();
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * Creates a new document with the given initial content.
|
|
56 *
|
|
57 * @param initialContent the document's initial content
|
|
58 */
|
|
59 public Document(String initialContent) {
|
|
60 super();
|
|
61 setTextStore(new CopyOnWriteTextStore(new GapTextStore()));
|
|
62 setLineTracker(new DefaultLineTracker());
|
|
63 getStore().set(initialContent);
|
|
64 getTracker().set(initialContent);
|
|
65 completeInitialization();
|
|
66 }
|
|
67
|
|
68 /*
|
|
69 * @see dwtx.jface.text.IRepairableDocumentExtension#isLineInformationRepairNeeded(int, int, java.lang.String)
|
|
70 * @since 3.4
|
|
71 */
|
|
72 public bool isLineInformationRepairNeeded(int offset, int length, String text) throws BadLocationException {
|
|
73 if ((0 > offset) || (0 > length) || (offset + length > getLength()))
|
|
74 throw new BadLocationException();
|
|
75
|
|
76 return isLineInformationRepairNeeded(text) || isLineInformationRepairNeeded(get(offset, length));
|
|
77 }
|
|
78
|
|
79 /**
|
|
80 * Checks whether the line information needs to be repaired.
|
|
81 *
|
|
82 * @param text the text to check
|
|
83 * @return <code>true</code> if the line information must be repaired
|
|
84 * @since 3.4
|
|
85 */
|
|
86 private bool isLineInformationRepairNeeded(String text) {
|
|
87 if (text is null)
|
|
88 return false;
|
|
89
|
|
90 int length= text.length();
|
|
91 if (length is 0)
|
|
92 return false;
|
|
93
|
|
94 int rIndex= text.indexOf('\r');
|
|
95 int nIndex= text.indexOf('\n');
|
|
96 if (rIndex is -1 && nIndex is -1)
|
|
97 return false;
|
|
98
|
|
99 if (rIndex > 0 && rIndex < length-1 && nIndex > 1 && rIndex < length-2)
|
|
100 return false;
|
|
101
|
|
102 String defaultLD= null;
|
|
103 try {
|
|
104 defaultLD= getLineDelimiter(0);
|
|
105 } catch (BadLocationException x) {
|
|
106 return true;
|
|
107 }
|
|
108
|
|
109 if (defaultLD is null)
|
|
110 return false;
|
|
111
|
|
112 defaultLD= getDefaultLineDelimiter();
|
|
113
|
|
114 if (defaultLD.length() is 1) {
|
|
115 if (rIndex !is -1 && !"\r".equals(defaultLD)) //$NON-NLS-1$
|
|
116 return true;
|
|
117 if (nIndex !is -1 && !"\n".equals(defaultLD)) //$NON-NLS-1$
|
|
118 return true;
|
|
119 } else if (defaultLD.length() is 2)
|
|
120 return rIndex is -1 || nIndex - rIndex !is 1;
|
|
121
|
|
122 return false;
|
|
123 }
|
|
124
|
|
125 }
|