]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/java/JavaStringAutoIndentStrategy.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / java / JavaStringAutoIndentStrategy.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.java;
12
13
14 import java.util.StringTokenizer;
15
16 import org.eclipse.jface.preference.IPreferenceStore;
17
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
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IWorkbenchPage;
28
29 import org.eclipse.ui.texteditor.ITextEditorExtension3;
30
31 import org.eclipse.jdt.ui.PreferenceConstants;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35
36 /**
37  * Auto indent strategy for java strings
38  */
39 public class JavaStringAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
40
41         private String fPartitioning;
42
43         /**
44          * The input string doesn't contain any line delimiter.
45          *
46          * @param inputString the given input string
47          * @param indentation the indentation
48          * @param delimiter the line delimiter
49          * @return the display string
50          */
51         private String displayString(String inputString, String indentation, String delimiter) {
52
53                 int length = inputString.length();
54                 StringBuffer buffer = new StringBuffer(length);
55                 StringTokenizer tokenizer= new StringTokenizer(inputString, "\n\r", true); //$NON-NLS-1$
56                 while (tokenizer.hasMoreTokens()){
57
58                         String token = tokenizer.nextToken();
59                         if (token.equals("\r")) { //$NON-NLS-1$
60                                 buffer.append("\\r"); //$NON-NLS-1$
61                                 if (tokenizer.hasMoreTokens()) {
62                                         token = tokenizer.nextToken();
63                                         if (token.equals("\n")) { //$NON-NLS-1$
64                                                 buffer.append("\\n"); //$NON-NLS-1$
65                                                 buffer.append("\" + " + delimiter); //$NON-NLS-1$
66                                                 buffer.append(indentation);
67                                                 buffer.append("\""); //$NON-NLS-1$
68                                                 continue;
69                                         } else {
70                                                 buffer.append("\" + " + delimiter); //$NON-NLS-1$
71                                                 buffer.append(indentation);
72                                                 buffer.append("\""); //$NON-NLS-1$
73                                         }
74                                 } else {
75                                         continue;
76                                 }
77                         } else if (token.equals("\n")) { //$NON-NLS-1$
78                                 buffer.append("\\n"); //$NON-NLS-1$
79                                 buffer.append("\" + " + delimiter); //$NON-NLS-1$
80                                 buffer.append(indentation);
81                                 buffer.append("\""); //$NON-NLS-1$
82                                 continue;
83                         }
84
85                         StringBuffer tokenBuffer = new StringBuffer();
86                         for (int i = 0; i < token.length(); i++){
87                                 char c = token.charAt(i);
88                                 switch (c) {
89                                         case '\r' :
90                                                 tokenBuffer.append("\\r"); //$NON-NLS-1$
91                                                 break;
92                                         case '\n' :
93                                                 tokenBuffer.append("\\n"); //$NON-NLS-1$
94                                                 break;
95                                         case '\b' :
96                                                 tokenBuffer.append("\\b"); //$NON-NLS-1$
97                                                 break;
98                                         case '\t' :
99                                                 // keep tabs verbatim
100                                                 tokenBuffer.append("\t"); //$NON-NLS-1$
101                                                 break;
102                                         case '\f' :
103                                                 tokenBuffer.append("\\f"); //$NON-NLS-1$
104                                                 break;
105                                         case '\"' :
106                                                 tokenBuffer.append("\\\""); //$NON-NLS-1$
107                                                 break;
108                                         case '\\' :
109                                                 tokenBuffer.append("\\\\"); //$NON-NLS-1$
110                                                 break;
111                                         default :
112                                                 tokenBuffer.append(c);
113                                 }
114                         }
115                         buffer.append(tokenBuffer);
116                 }
117                 return buffer.toString();
118         }
119
120         /**
121          * Creates a new Java string auto indent strategy for the given document partitioning.
122          *
123          * @param partitioning the document partitioning
124          */
125         public JavaStringAutoIndentStrategy(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) throws BadLocationException {
138
139                 // find start of line
140                 int adjustedOffset= (offset == document.getLength() ? offset  - 1 : offset);
141                 IRegion line= document.getLineInformationOfOffset(adjustedOffset);
142                 int start= line.getOffset();
143
144                 // find white spaces
145                 int end= findEndOfWhiteSpace(document, start, offset);
146
147                 return document.get(start, end - start);
148         }
149
150         private String getModifiedText(String string, String indentation, String delimiter) {
151                 return displayString(string, indentation, delimiter);
152         }
153
154         private void javaStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException {
155
156                 ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, true);
157                 int offset= partition.getOffset();
158                 int length= partition.getLength();
159
160                 if (command.offset == offset + length && document.getChar(offset + length - 1) == '\"')
161                         return;
162
163                 String indentation= getLineIndentation(document, command.offset);
164                 String delimiter= TextUtilities.getDefaultLineDelimiter(document);
165
166                 IRegion line= document.getLineInformationOfOffset(offset);
167                 String string= document.get(line.getOffset(), offset - line.getOffset()).trim();
168                 if (string.length() != 0 && !string.equals("+")) //$NON-NLS-1$
169                         indentation += String.valueOf("\t\t"); //$NON-NLS-1$
170
171                 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore();
172                 boolean isLineDelimiter= isLineDelimiter(document, command.text);
173                 if (preferenceStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS) && isLineDelimiter)
174                         command.text= "\" +" + command.text + indentation + "\"";  //$NON-NLS-1$//$NON-NLS-2$
175                 else if (command.text.length() > 1 && !isLineDelimiter && preferenceStore.getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS))
176                         command.text= getModifiedText(command.text, indentation, delimiter);
177         }
178
179         private boolean isSmartMode() {
180                 IWorkbenchPage page= JavaPlugin.getActivePage();
181                 if (page != null)  {
182                         IEditorPart part= page.getActiveEditor();
183                         if (part instanceof ITextEditorExtension3) {
184                                 ITextEditorExtension3 extension= (ITextEditorExtension3) part;
185                                 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
186                         }
187                 }
188                 return false;
189         }
190
191         /*
192          * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand)
193          */
194         @Override
195         public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
196                 try {
197                         if (command.text == null)
198                                 return;
199                         if (isSmartMode())
200                                 javaStringIndentAfterNewLine(document, command);
201                 } catch (BadLocationException e) {
202                 }
203         }
204 }