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.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;
29 * Auto indent strategy for java strings
31 public class JavaStringAutoIndentStrategySQ extends
32 DefaultIndentLineAutoEditStrategy {
34 private String fPartitioning;
37 * The input string doesn't contain any line delimiter.
40 * the given input string
41 * @return the displayable string.
43 private String displayString(String inputString, String indentation,
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()) {
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$
64 buffer.append("\' . " + delimiter); //$NON-NLS-1$
65 buffer.append(indentation);
66 buffer.append("\'"); //$NON-NLS-1$
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$
79 StringBuffer tokenBuffer = new StringBuffer();
80 for (int i = 0; i < token.length(); i++) {
81 char c = token.charAt(i);
84 tokenBuffer.append("\\r"); //$NON-NLS-1$
87 tokenBuffer.append("\\n"); //$NON-NLS-1$
90 tokenBuffer.append("\\b"); //$NON-NLS-1$
94 tokenBuffer.append("\t"); //$NON-NLS-1$
97 tokenBuffer.append("\\f"); //$NON-NLS-1$
100 tokenBuffer.append("\\\""); //$NON-NLS-1$
103 tokenBuffer.append("\\'"); //$NON-NLS-1$
106 tokenBuffer.append("\\\\"); //$NON-NLS-1$
109 tokenBuffer.append(c);
112 buffer.append(tokenBuffer);
114 return buffer.toString();
118 * Creates a new Java string auto indent strategy for the given document
121 * @param partitioning
122 * the document partitioning
124 public JavaStringAutoIndentStrategySQ(String partitioning) {
126 fPartitioning = partitioning;
129 private boolean isLineDelimiter(IDocument document, String text) {
130 String[] delimiters = document.getLegalLineDelimiters();
131 if (delimiters != null)
132 return TextUtilities.equals(delimiters, text) > -1;
136 private String getLineIndentation(IDocument document, int offset)
137 throws BadLocationException {
139 // find start of line
140 int adjustedOffset = (offset == document.getLength() ? offset - 1
142 IRegion line = document.getLineInformationOfOffset(adjustedOffset);
143 int start = line.getOffset();
146 int end = findEndOfWhiteSpace(document, start, offset);
148 return document.get(start, end - start);
151 private String getModifiedText(String string, String indentation,
152 String delimiter) throws BadLocationException {
153 return displayString(string, indentation, delimiter);
156 private void javaStringIndentAfterNewLine(IDocument document,
157 DocumentCommand command) throws BadLocationException {
159 ITypedRegion partition = TextUtilities.getPartition(document,
160 fPartitioning, command.offset, false);
161 int offset = partition.getOffset();
162 int length = partition.getLength();
164 if (command.offset == offset) {
165 // we are really just before the string partition -> feet the event
166 // through the java indenter
168 // JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document,
173 if (command.offset == offset + length
174 && document.getChar(offset + length - 1) == '\'')
177 String indentation = getLineIndentation(document, command.offset);
178 String delimiter = TextUtilities.getDefaultLineDelimiter(document);
180 IRegion line = document.getLineInformationOfOffset(offset);
181 String string = document.get(line.getOffset(), offset
183 if (string.trim().length() != 0)
184 indentation += String.valueOf("\t\t"); //$NON-NLS-1$
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
192 .getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ))
193 command.text = getModifiedText(command.text, indentation, delimiter);
196 private boolean isSmartMode() {
197 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
199 IEditorPart part = page.getActiveEditor();
200 if (part instanceof ITextEditorExtension3) {
201 ITextEditorExtension3 extension = (ITextEditorExtension3) part;
202 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
209 * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument,
212 public void customizeDocumentCommand(IDocument document,
213 DocumentCommand command) {
215 if (command.length != 0 || command.text == null)
218 IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault()
219 .getPreferenceStore();
222 .getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_SQ)
224 javaStringIndentAfterNewLine(document, command);
226 } catch (BadLocationException e) {