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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.java;
14 import java.util.StringTokenizer;
16 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;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.texteditor.ITextEditorExtension3;
31 import org.eclipse.jdt.ui.PreferenceConstants;
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 * Auto indent strategy for java strings
39 public class JavaStringAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
41 private String fPartitioning;
44 * The input string doesn't contain any line delimiter.
46 * @param inputString the given input string
47 * @param indentation the indentation
48 * @param delimiter the line delimiter
49 * @return the display string
51 private String displayString(String inputString, String indentation, String delimiter) {
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()){
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$
70 buffer.append("\" + " + delimiter); //$NON-NLS-1$
71 buffer.append(indentation);
72 buffer.append("\""); //$NON-NLS-1$
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$
85 StringBuffer tokenBuffer = new StringBuffer();
86 for (int i = 0; i < token.length(); i++){
87 char c = token.charAt(i);
90 tokenBuffer.append("\\r"); //$NON-NLS-1$
93 tokenBuffer.append("\\n"); //$NON-NLS-1$
96 tokenBuffer.append("\\b"); //$NON-NLS-1$
100 tokenBuffer.append("\t"); //$NON-NLS-1$
103 tokenBuffer.append("\\f"); //$NON-NLS-1$
106 tokenBuffer.append("\\\""); //$NON-NLS-1$
109 tokenBuffer.append("\\\\"); //$NON-NLS-1$
112 tokenBuffer.append(c);
115 buffer.append(tokenBuffer);
117 return buffer.toString();
121 * Creates a new Java string auto indent strategy for the given document partitioning.
123 * @param partitioning the document partitioning
125 public JavaStringAutoIndentStrategy(String partitioning) {
127 fPartitioning= partitioning;
130 private boolean isLineDelimiter(IDocument document, String text) {
131 String[] delimiters= document.getLegalLineDelimiters();
132 if (delimiters != null)
133 return TextUtilities.equals(delimiters, text) > -1;
137 private String getLineIndentation(IDocument document, int offset) throws BadLocationException {
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();
145 int end= findEndOfWhiteSpace(document, start, offset);
147 return document.get(start, end - start);
150 private String getModifiedText(String string, String indentation, String delimiter) {
151 return displayString(string, indentation, delimiter);
154 private void javaStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException {
156 ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, true);
157 int offset= partition.getOffset();
158 int length= partition.getLength();
160 if (command.offset == offset + length && document.getChar(offset + length - 1) == '\"')
163 String indentation= getLineIndentation(document, command.offset);
164 String delimiter= TextUtilities.getDefaultLineDelimiter(document);
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$
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);
179 private boolean isSmartMode() {
180 IWorkbenchPage page= JavaPlugin.getActivePage();
182 IEditorPart part= page.getActiveEditor();
183 if (part instanceof ITextEditorExtension3) {
184 ITextEditorExtension3 extension= (ITextEditorExtension3) part;
185 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
192 * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand)
195 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
197 if (command.text == null)
200 javaStringIndentAfterNewLine(document, command);
201 } catch (BadLocationException e) {