fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / java / JavaStringAutoIndentStrategyDQ.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java;
12
13 import net.sourceforge.phpdt.ui.PreferenceConstants;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy;
19 import org.eclipse.jface.text.DocumentCommand;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITypedRegion;
23 import org.eclipse.jface.text.TextUtilities;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.texteditor.ITextEditorExtension3;
27
28 /**
29  * Auto indent strategy for java strings
30  */
31 public class JavaStringAutoIndentStrategyDQ extends
32                 DefaultIndentLineAutoEditStrategy {
33
34         private String fPartitioning;
35
36         /**
37          * The input string doesn't contain any line delimiter.
38          * 
39          * @param inputString
40          *            the given input string
41          * @return the displayable string.
42          */
43         private String displayString(String inputString, String indentation,
44                         String delimiter) {
45
46                 int length = inputString.length();
47                 StringBuffer buffer = new StringBuffer(length);
48                 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
49                                 inputString, "\n\r", true); //$NON-NLS-1$
50                 while (tokenizer.hasMoreTokens()) {
51
52                         String token = tokenizer.nextToken();
53                         if (token.equals("\r")) { //$NON-NLS-1$
54                                 buffer.append("\\r"); //$NON-NLS-1$
55                                 if (tokenizer.hasMoreTokens()) {
56                                         token = tokenizer.nextToken();
57                                         if (token.equals("\n")) { //$NON-NLS-1$
58                                                 buffer.append("\\n"); //$NON-NLS-1$
59                                                 buffer.append("\" . " + delimiter); //$NON-NLS-1$
60                                                 buffer.append(indentation);
61                                                 buffer.append("\""); //$NON-NLS-1$
62                                                 continue;
63                                         } else {
64                                                 buffer.append("\" . " + delimiter); //$NON-NLS-1$
65                                                 buffer.append(indentation);
66                                                 buffer.append("\""); //$NON-NLS-1$
67                                         }
68                                 } else {
69                                         continue;
70                                 }
71                         } else if (token.equals("\n")) { //$NON-NLS-1$
72                                 buffer.append("\\n"); //$NON-NLS-1$
73                                 buffer.append("\" . " + delimiter); //$NON-NLS-1$
74                                 buffer.append(indentation);
75                                 buffer.append("\""); //$NON-NLS-1$
76                                 continue;
77                         }
78
79                         StringBuffer tokenBuffer = new StringBuffer();
80                         for (int i = 0; i < token.length(); i++) {
81                                 char c = token.charAt(i);
82                                 switch (c) {
83                                 case '\r':
84                                         tokenBuffer.append("\\r"); //$NON-NLS-1$
85                                         break;
86                                 case '\n':
87                                         tokenBuffer.append("\\n"); //$NON-NLS-1$
88                                         break;
89                                 case '\b':
90                                         tokenBuffer.append("\\b"); //$NON-NLS-1$
91                                         break;
92                                 case '\t':
93                                         // keep tabs verbatim
94                                         tokenBuffer.append("\t"); //$NON-NLS-1$
95                                         break;
96                                 case '\f':
97                                         tokenBuffer.append("\\f"); //$NON-NLS-1$
98                                         break;
99                                 case '\"':
100                                         tokenBuffer.append("\\\""); //$NON-NLS-1$
101                                         break;
102                                 case '\'':
103                                         tokenBuffer.append("\\'"); //$NON-NLS-1$
104                                         break;
105                                 case '\\':
106                                         tokenBuffer.append("\\\\"); //$NON-NLS-1$
107                                         break;
108                                 default:
109                                         tokenBuffer.append(c);
110                                 }
111                         }
112                         buffer.append(tokenBuffer);
113                 }
114                 return buffer.toString();
115         }
116
117         /**
118          * Creates a new Java string auto indent strategy for the given document
119          * partitioning.
120          * 
121          * @param partitioning
122          *            the document partitioning
123          */
124         public JavaStringAutoIndentStrategyDQ(String partitioning) {
125                 super();
126                 fPartitioning = partitioning;
127         }
128
129         private boolean isLineDelimiter(IDocument document, String text) {
130                 String[] delimiters = document.getLegalLineDelimiters();
131                 if (delimiters != null)
132                         return TextUtilities.equals(delimiters, text) > -1;
133                 return false;
134         }
135
136         private String getLineIndentation(IDocument document, int offset)
137                         throws BadLocationException {
138
139                 // find start of line
140                 int adjustedOffset = (offset == document.getLength() ? offset - 1
141                                 : offset);
142                 IRegion line = document.getLineInformationOfOffset(adjustedOffset);
143                 int start = line.getOffset();
144
145                 // find white spaces
146                 int end = findEndOfWhiteSpace(document, start, offset);
147
148                 return document.get(start, end - start);
149         }
150
151         private String getModifiedText(String string, String indentation,
152                         String delimiter) throws BadLocationException {
153                 return displayString(string, indentation, delimiter);
154         }
155
156         private void javaStringIndentAfterNewLine(IDocument document,
157                         DocumentCommand command) throws BadLocationException {
158
159                 ITypedRegion partition = TextUtilities.getPartition(document,
160                                 fPartitioning, command.offset, false);
161                 int offset = partition.getOffset();
162                 int length = partition.getLength();
163
164                 if (command.offset == offset) {
165                         // we are really just before the string partition -> feet the event
166                         // through the java indenter
167                         // new
168                         // JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document,
169                         // command);
170                         return;
171                 }
172
173                 if (command.offset == offset + length
174                                 && document.getChar(offset + length - 1) == '\"')
175                         return;
176
177                 String indentation = getLineIndentation(document, command.offset);
178                 String delimiter = TextUtilities.getDefaultLineDelimiter(document);
179
180                 IRegion line = document.getLineInformationOfOffset(offset);
181                 String string = document.get(line.getOffset(), offset
182                                 - line.getOffset());
183                 if (string.trim().length() != 0)
184                         indentation += String.valueOf("\t\t"); //$NON-NLS-1$
185
186                 IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault()
187                                 .getPreferenceStore();
188                 if (isLineDelimiter(document, command.text))
189                         command.text = "\" ." + command.text + indentation + "\""; //$NON-NLS-1$//$NON-NLS-2$
190                 else if (command.text.length() > 1
191                                 && preferenceStore
192                                                 .getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS_DQ))
193                         command.text = getModifiedText(command.text, indentation, delimiter);
194         }
195
196         private boolean isSmartMode() {
197                 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
198                 if (page != null) {
199                         IEditorPart part = page.getActiveEditor();
200                         if (part instanceof ITextEditorExtension3) {
201                                 ITextEditorExtension3 extension = (ITextEditorExtension3) part;
202                                 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
203                         }
204                 }
205                 return false;
206         }
207
208         /*
209          * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument,
210          *      DocumentCommand)
211          */
212         public void customizeDocumentCommand(IDocument document,
213                         DocumentCommand command) {
214                 try {
215                         if (command.length != 0 || command.text == null)
216                                 return;
217
218                         IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault()
219                                         .getPreferenceStore();
220
221                         if (preferenceStore
222                                         .getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_DQ)
223                                         && isSmartMode())
224                                 javaStringIndentAfterNewLine(document, command);
225
226                 } catch (BadLocationException e) {
227                 }
228         }
229 }