1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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.corext.template.php;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import net.sourceforge.phpdt.internal.corext.util.CodeFormatterUtil;
18 import net.sourceforge.phpdt.internal.corext.util.Strings;
19 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
20 import net.sourceforge.phpdt.internal.ui.text.JavaHeuristicScanner;
21 import net.sourceforge.phpdt.internal.ui.text.JavaIndenter;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITypedRegion;
29 import org.eclipse.jface.text.templates.DocumentTemplateContext;
30 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
31 import org.eclipse.jface.text.templates.TemplateBuffer;
32 import org.eclipse.jface.text.templates.TemplateContext;
33 import org.eclipse.jface.text.templates.TemplateVariable;
34 import org.eclipse.text.edits.DeleteEdit;
35 import org.eclipse.text.edits.InsertEdit;
36 import org.eclipse.text.edits.MalformedTreeException;
37 import org.eclipse.text.edits.MultiTextEdit;
38 import org.eclipse.text.edits.RangeMarker;
39 import org.eclipse.text.edits.ReplaceEdit;
40 import org.eclipse.text.edits.TextEdit;
43 * A template editor using the Java formatter to format a template buffer.
45 public class JavaFormatter {
47 private static final String MARKER = "/*${" + GlobalTemplateVariables.Cursor.NAME + "}*/"; //$NON-NLS-1$ //$NON-NLS-2$
49 /** The line delimiter to use if code formatter is not used. */
50 private final String fLineDelimiter;
52 /** The initial indent level */
53 private final int fInitialIndentLevel;
55 /** The java partitioner */
56 private boolean fUseCodeFormatter;
59 * Creates a JavaFormatter with the target line delimiter.
61 * @param lineDelimiter
62 * the line delimiter to use
63 * @param initialIndentLevel
64 * the initial indentation level
65 * @param useCodeFormatter
66 * <code>true</code> if the core code formatter should be used
68 public JavaFormatter(String lineDelimiter, int initialIndentLevel, boolean useCodeFormatter) {
69 fLineDelimiter = lineDelimiter;
70 fUseCodeFormatter = useCodeFormatter;
71 fInitialIndentLevel = initialIndentLevel;
75 * Formats the template buffer.
79 * @throws BadLocationException
81 public void format(TemplateBuffer buffer, TemplateContext context) throws BadLocationException {
83 if (fUseCodeFormatter)
84 // try to format and fall back to indenting
86 format(buffer, (JavaContext) context);
87 } catch (BadLocationException e) {
89 } catch (MalformedTreeException e) {
95 // don't trim the buffer if the replacement area is empty
96 // case: surrounding empty lines with block
97 if (context instanceof DocumentTemplateContext) {
98 DocumentTemplateContext dtc = (DocumentTemplateContext) context;
99 if (dtc.getStart() == dtc.getCompletionOffset())
100 if (dtc.getDocument().get(dtc.getStart(), dtc.getEnd() - dtc.getEnd()).trim().length() == 0)
105 } catch (MalformedTreeException e) {
106 throw new BadLocationException();
110 private static int getCaretOffset(TemplateVariable[] variables) {
111 for (int i = 0; i != variables.length; i++) {
112 TemplateVariable variable = variables[i];
114 if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME))
115 return variable.getOffsets()[0];
121 private boolean isInsideCommentOrString(String string, int offset) {
123 IDocument document = new Document(string);
124 PHPeclipsePlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document);
127 ITypedRegion partition = document.getPartition(offset);
128 String partitionType = partition.getType();
130 return partitionType != null
131 && (partitionType.equals(IPHPPartitions.PHP_MULTILINE_COMMENT)
132 || partitionType.equals(IPHPPartitions.PHP_SINGLELINE_COMMENT) || partitionType.equals(IPHPPartitions.PHP_STRING_DQ)
133 || partitionType.equals(IPHPPartitions.PHP_STRING_SQ) || partitionType.equals(IPHPPartitions.PHP_PHPDOC_COMMENT));
135 } catch (BadLocationException e) {
140 private void format(TemplateBuffer templateBuffer, JavaContext context) throws BadLocationException {
142 // workaround for code formatter limitations
143 // handle a special case where cursor position is surrounded by whitespaces
145 String string = templateBuffer.getString();
146 TemplateVariable[] variables = templateBuffer.getVariables();
148 int caretOffset = getCaretOffset(variables);
149 if ((caretOffset > 0) && Character.isWhitespace(string.charAt(caretOffset - 1)) && (caretOffset < string.length())
150 && Character.isWhitespace(string.charAt(caretOffset)) && !isInsideCommentOrString(string, caretOffset)) {
151 List positions = variablesToPositions(variables);
153 TextEdit insert = new InsertEdit(caretOffset, MARKER);
154 string = edit(string, positions, insert);
155 positionsToVariables(positions, variables);
156 templateBuffer.setContent(string, variables);
158 plainFormat(templateBuffer, context);
160 string = templateBuffer.getString();
161 variables = templateBuffer.getVariables();
162 caretOffset = getCaretOffset(variables);
164 positions = variablesToPositions(variables);
165 TextEdit delete = new DeleteEdit(caretOffset, MARKER.length());
166 string = edit(string, positions, delete);
167 positionsToVariables(positions, variables);
168 templateBuffer.setContent(string, variables);
171 plainFormat(templateBuffer, context);
175 private void plainFormat(TemplateBuffer templateBuffer, JavaContext context) throws BadLocationException {
178 // private void plainFormat(TemplateBuffer templateBuffer, JavaContext context) throws BadLocationException {
180 // IDocument doc= new Document(templateBuffer.getString());
182 // TemplateVariable[] variables= templateBuffer.getVariables();
184 // List offsets= variablesToPositions(variables);
187 // if (context.getCompilationUnit() != null)
188 // options= context.getCompilationUnit().getJavaProject().getOptions(true);
190 // options= JavaCore.getOptions();
192 // TextEdit edit= CodeFormatterUtil.format2(CodeFormatter.K_UNKNOWN, doc.get(), fInitialIndentLevel, fLineDelimiter, options);
194 // throw new BadLocationException(); // fall back to indenting
196 // MultiTextEdit root;
197 // if (edit instanceof MultiTextEdit)
198 // root= (MultiTextEdit) edit;
200 // root= new MultiTextEdit(0, doc.getLength());
201 // root.addChild(edit);
203 // for (Iterator it= offsets.iterator(); it.hasNext();) {
204 // TextEdit position= (TextEdit) it.next();
206 // root.addChild(position);
207 // } catch (MalformedTreeException e) {
208 // // position conflicts with formatter edit
209 // // ignore this position
213 // root.apply(doc, TextEdit.UPDATE_REGIONS);
215 // positionsToVariables(offsets, variables);
217 // templateBuffer.setContent(doc.get(), variables);
220 private void indent(TemplateBuffer templateBuffer) throws BadLocationException, MalformedTreeException {
222 TemplateVariable[] variables = templateBuffer.getVariables();
223 List positions = variablesToPositions(variables);
225 IDocument document = new Document(templateBuffer.getString());
226 MultiTextEdit root = new MultiTextEdit(0, document.getLength());
227 root.addChildren((TextEdit[]) positions.toArray(new TextEdit[positions.size()]));
229 JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
230 JavaIndenter indenter = new JavaIndenter(document, scanner);
233 int offset = document.getLineOffset(0);
234 TextEdit edit = new InsertEdit(offset, CodeFormatterUtil.createIndentString(fInitialIndentLevel));
236 root.apply(document, TextEdit.UPDATE_REGIONS);
237 root.removeChild(edit);
239 formatDelimiter(document, root, 0);
242 int lineCount = document.getNumberOfLines();
244 for (int line = 1; line < lineCount; line++) {
245 IRegion region = document.getLineInformation(line);
246 offset = region.getOffset();
247 StringBuffer indent = indenter.computeIndentation(offset);
250 // axelcl delete start
251 // int nonWS = scanner.findNonWhitespaceForwardInAnyPartition(offset, offset + region.getLength());
252 // if (nonWS == JavaHeuristicScanner.NOT_FOUND)
254 // edit = new ReplaceEdit(offset, nonWS - offset, indent.toString());
256 // axelcl insert start
258 edit = new ReplaceEdit(offset, nonWS - offset, CodeFormatterUtil.createIndentString(fInitialIndentLevel));
261 root.apply(document, TextEdit.UPDATE_REGIONS);
262 root.removeChild(edit);
264 formatDelimiter(document, root, line);
267 positionsToVariables(positions, variables);
268 templateBuffer.setContent(document.get(), variables);
272 * Changes the delimiter to the configured line delimiter.
275 * the temporary document being edited
277 * the root edit containing all positions that will be updated along the way
280 * @throws BadLocationException
281 * if applying the changes fails
283 private void formatDelimiter(IDocument document, MultiTextEdit root, int line) throws BadLocationException {
284 IRegion region = document.getLineInformation(line);
285 String lineDelimiter = document.getLineDelimiter(line);
286 if (lineDelimiter != null) {
287 TextEdit edit = new ReplaceEdit(region.getOffset() + region.getLength(), lineDelimiter.length(), fLineDelimiter);
289 root.apply(document, TextEdit.UPDATE_REGIONS);
290 root.removeChild(edit);
294 private static void trimBegin(TemplateBuffer templateBuffer) throws BadLocationException {
295 String string = templateBuffer.getString();
296 TemplateVariable[] variables = templateBuffer.getVariables();
298 List positions = variablesToPositions(variables);
301 while ((i != string.length()) && Character.isWhitespace(string.charAt(i)))
304 string = edit(string, positions, new DeleteEdit(0, i));
305 positionsToVariables(positions, variables);
307 templateBuffer.setContent(string, variables);
310 private static String edit(String string, List positions, TextEdit edit) throws BadLocationException {
311 MultiTextEdit root = new MultiTextEdit(0, string.length());
312 root.addChildren((TextEdit[]) positions.toArray(new TextEdit[positions.size()]));
314 IDocument document = new Document(string);
315 root.apply(document);
317 return document.get();
320 private static List variablesToPositions(TemplateVariable[] variables) {
321 List positions = new ArrayList(5);
322 for (int i = 0; i != variables.length; i++) {
323 int[] offsets = variables[i].getOffsets();
325 // trim positions off whitespace
326 String value = variables[i].getDefaultValue();
328 while (wsStart < value.length() && Character.isWhitespace(value.charAt(wsStart))
329 && !Strings.isLineDelimiterChar(value.charAt(wsStart)))
332 variables[i].getValues()[0] = value.substring(wsStart);
334 for (int j = 0; j != offsets.length; j++) {
335 offsets[j] += wsStart;
336 positions.add(new RangeMarker(offsets[j], 0));
342 private static void positionsToVariables(List positions, TemplateVariable[] variables) {
343 Iterator iterator = positions.iterator();
345 for (int i = 0; i != variables.length; i++) {
346 TemplateVariable variable = variables[i];
348 int[] offsets = new int[variable.getOffsets().length];
349 for (int j = 0; j != offsets.length; j++)
350 offsets[j] = ((TextEdit) iterator.next()).getOffset();
352 variable.setOffsets(offsets);