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;
15 import net.sourceforge.phpeclipse.ui.WebUI;
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;
30 * Auto indent strategy for java strings
32 public class JavaStringAutoIndentStrategySQ extends
33 DefaultIndentLineAutoEditStrategy {
35 private String fPartitioning;
38 * The input string doesn't contain any line delimiter.
41 * the given input string
42 * @return the displayable string.
44 private String displayString(String inputString, String indentation,
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()) {
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$
65 buffer.append("\' . " + delimiter); //$NON-NLS-1$
66 buffer.append(indentation);
67 buffer.append("\'"); //$NON-NLS-1$
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$
80 StringBuffer tokenBuffer = new StringBuffer();
81 for (int i = 0; i < token.length(); i++) {
82 char c = token.charAt(i);
85 tokenBuffer.append("\\r"); //$NON-NLS-1$
88 tokenBuffer.append("\\n"); //$NON-NLS-1$
91 tokenBuffer.append("\\b"); //$NON-NLS-1$
95 tokenBuffer.append("\t"); //$NON-NLS-1$
98 tokenBuffer.append("\\f"); //$NON-NLS-1$
101 tokenBuffer.append("\\\""); //$NON-NLS-1$
104 tokenBuffer.append("\\'"); //$NON-NLS-1$
107 tokenBuffer.append("\\\\"); //$NON-NLS-1$
110 tokenBuffer.append(c);
113 buffer.append(tokenBuffer);
115 return buffer.toString();
119 * Creates a new Java string auto indent strategy for the given document
122 * @param partitioning
123 * the document partitioning
125 public JavaStringAutoIndentStrategySQ(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)
138 throws BadLocationException {
140 // find start of line
141 int adjustedOffset = (offset == document.getLength() ? offset - 1
143 IRegion line = document.getLineInformationOfOffset(adjustedOffset);
144 int start = line.getOffset();
147 int end = findEndOfWhiteSpace(document, start, offset);
149 return document.get(start, end - start);
152 private String getModifiedText(String string, String indentation,
153 String delimiter) throws BadLocationException {
154 return displayString(string, indentation, delimiter);
157 private void javaStringIndentAfterNewLine(IDocument document,
158 DocumentCommand command) throws BadLocationException {
160 ITypedRegion partition = TextUtilities.getPartition(document,
161 fPartitioning, command.offset, false);
162 int offset = partition.getOffset();
163 int length = partition.getLength();
165 if (command.offset == offset) {
166 // we are really just before the string partition -> feet the event
167 // through the java indenter
169 // JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document,
174 if (command.offset == offset + length
175 && document.getChar(offset + length - 1) == '\'')
178 String indentation = getLineIndentation(document, command.offset);
179 String delimiter = TextUtilities.getDefaultLineDelimiter(document);
181 IRegion line = document.getLineInformationOfOffset(offset);
182 String string = document.get(line.getOffset(), offset
184 if (string.trim().length() != 0)
185 indentation += String.valueOf("\t\t"); //$NON-NLS-1$
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
193 .getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ))
194 command.text = getModifiedText(command.text, indentation, delimiter);
197 private boolean isSmartMode() {
198 IWorkbenchPage page = WebUI.getActivePage();
200 IEditorPart part = page.getActiveEditor();
201 if (part instanceof ITextEditorExtension3) {
202 ITextEditorExtension3 extension = (ITextEditorExtension3) part;
203 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
210 * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument,
213 public void customizeDocumentCommand(IDocument document,
214 DocumentCommand command) {
216 if (command.length != 0 || command.text == null)
219 IPreferenceStore preferenceStore = WebUI.getDefault()
220 .getPreferenceStore();
223 .getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_SQ)
225 javaStringIndentAfterNewLine(document, command);
227 } catch (BadLocationException e) {