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