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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.java;
13 import net.sourceforge.phpdt.ui.PreferenceConstants;
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
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;
30 * Auto indent strategy for java strings
32 public class JavaStringAutoIndentStrategy extends DefaultAutoIndentStrategy {
34 private String fPartitioning;
37 * The input string doesn't contain any line delimiter.
39 * @param inputString the given input string
40 * @return the displayable string.
42 private String displayString(String inputString, String indentation, String delimiter) {
44 int length = inputString.length();
45 StringBuffer buffer = new StringBuffer(length);
46 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(inputString, "\n\r", true); //$NON-NLS-1$
47 while (tokenizer.hasMoreTokens()){
49 String token = tokenizer.nextToken();
50 if (token.equals("\r")) { //$NON-NLS-1$
51 buffer.append("\\r"); //$NON-NLS-1$
52 if (tokenizer.hasMoreTokens()) {
53 token = tokenizer.nextToken();
54 if (token.equals("\n")) { //$NON-NLS-1$
55 buffer.append("\\n"); //$NON-NLS-1$
56 buffer.append("\" + " + delimiter); //$NON-NLS-1$
57 buffer.append(indentation);
58 buffer.append("\""); //$NON-NLS-1$
61 buffer.append("\" + " + delimiter); //$NON-NLS-1$
62 buffer.append(indentation);
63 buffer.append("\""); //$NON-NLS-1$
68 } else if (token.equals("\n")) { //$NON-NLS-1$
69 buffer.append("\\n"); //$NON-NLS-1$
70 buffer.append("\" + " + delimiter); //$NON-NLS-1$
71 buffer.append(indentation);
72 buffer.append("\""); //$NON-NLS-1$
76 StringBuffer tokenBuffer = new StringBuffer();
77 for (int i = 0; i < token.length(); i++){
78 char c = token.charAt(i);
81 tokenBuffer.append("\\r"); //$NON-NLS-1$
84 tokenBuffer.append("\\n"); //$NON-NLS-1$
87 tokenBuffer.append("\\b"); //$NON-NLS-1$
91 tokenBuffer.append("\t"); //$NON-NLS-1$
94 tokenBuffer.append("\\f"); //$NON-NLS-1$
97 tokenBuffer.append("\\\""); //$NON-NLS-1$
100 tokenBuffer.append("\\'"); //$NON-NLS-1$
103 tokenBuffer.append("\\\\"); //$NON-NLS-1$
106 tokenBuffer.append(c);
109 buffer.append(tokenBuffer);
111 return buffer.toString();
115 * Creates a new Java string auto indent strategy for the given document partitioning.
117 * @param partitioning the document partitioning
119 public JavaStringAutoIndentStrategy(String partitioning) {
121 fPartitioning= partitioning;
124 private boolean isLineDelimiter(IDocument document, String text) {
125 String[] delimiters= document.getLegalLineDelimiters();
126 if (delimiters != null)
127 return TextUtilities.equals(delimiters, text) > -1;
131 private String getLineIndentation(IDocument document, int offset) throws BadLocationException {
133 // find start of line
134 int adjustedOffset= (offset == document.getLength() ? offset - 1 : offset);
135 IRegion line= document.getLineInformationOfOffset(adjustedOffset);
136 int start= line.getOffset();
139 int end= findEndOfWhiteSpace(document, start, offset);
141 return document.get(start, end - start);
144 private String getModifiedText(String string, String indentation, String delimiter) throws BadLocationException {
145 return displayString(string, indentation, delimiter);
148 private void javaStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException {
150 ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset);
151 int offset= partition.getOffset();
152 int length= partition.getLength();
154 if (command.offset == offset) {
155 // we are really just before the string partition -> feet the event through the java indenter
156 // new JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document, command);
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());
168 if (string.trim().length() != 0)
169 indentation += String.valueOf("\t\t"); //$NON-NLS-1$
171 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
172 if (isLineDelimiter(document, command.text))
173 command.text= "\" +" + command.text + indentation + "\""; //$NON-NLS-1$//$NON-NLS-2$
174 else if (command.text.length() > 1 && preferenceStore.getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS))
175 command.text= getModifiedText(command.text, indentation, delimiter);
178 private boolean isSmartMode() {
179 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
181 IEditorPart part= page.getActiveEditor();
182 if (part instanceof ITextEditorExtension3) {
183 ITextEditorExtension3 extension= (ITextEditorExtension3) part;
184 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
191 * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand)
193 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
195 if (command.length != 0 || command.text == null)
198 IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
200 if (preferenceStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS) && isSmartMode())
201 javaStringIndentAfterNewLine(document, command);
203 } catch (BadLocationException e) {