KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > formatter > comment > CommentFormatterUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.formatter.comment;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.text.edits.TextEdit;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.BadPositionCategoryException;
20 import org.eclipse.jface.text.DefaultPositionUpdater;
21 import org.eclipse.jface.text.Document;
22 import org.eclipse.jface.text.Position;
23
24 import org.eclipse.jdt.core.ToolFactory;
25
26 import org.eclipse.jdt.internal.core.util.Util;
27
28 /**
29  * Comment formatting utils.
30  *
31  * @since 3.1
32  */

33 public class CommentFormatterUtil {
34
35     /**
36      * Evaluates the edit on the given string.
37      *
38      * @throws IllegalArgumentException if the positions are not inside the
39      * string
40      */

41     public static String JavaDoc evaluateFormatterEdit(String JavaDoc string, TextEdit edit, Position[] positions) {
42         try {
43             Document doc= createDocument(string, positions);
44             edit.apply(doc, 0);
45             if (positions != null) {
46                 for (int i= 0; i < positions.length; i++) {
47                     Assert.isTrue(!positions[i].isDeleted, "Position got deleted"); //$NON-NLS-1$
48
}
49             }
50             return doc.get();
51         } catch (BadLocationException e) {
52             log(e); // bug in the formatter
53
Assert.isTrue(false, "Formatter created edits with wrong positions: " + e.getMessage()); //$NON-NLS-1$
54
}
55         return null;
56     }
57     
58     /**
59      * Creates edits that describe how to format the given string. Returns
60      * <code>null</code> if the code could not be formatted for the given
61      * kind.
62      *
63      * @throws IllegalArgumentException if the offset and length are not
64      * inside the string
65      */

66     public static TextEdit format2(int kind, String JavaDoc string, int indentationLevel, String JavaDoc lineSeparator, Map JavaDoc options) {
67         int length= string.length();
68         if (0 < 0 || length < 0 || 0 + length > string.length()) {
69             throw new IllegalArgumentException JavaDoc("offset or length outside of string. offset: " + 0 + ", length: " + length + ", string size: " + string.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
70
}
71         return ToolFactory.createCodeFormatter(options).format(kind, string, 0, length, indentationLevel, lineSeparator);
72     }
73
74     /**
75      * Returns a document with the given content and the given positions
76      * registered with the {@link DefaultPositionUpdater}.
77      *
78      * @param content the content
79      * @param positions the positions
80      * @return the document
81      * @throws IllegalArgumentException
82      */

83     private static Document createDocument(String JavaDoc content, Position[] positions) throws IllegalArgumentException JavaDoc {
84         Document doc= new Document(content);
85         try {
86             if (positions != null) {
87                 final String JavaDoc POS_CATEGORY= "myCategory"; //$NON-NLS-1$
88

89                 doc.addPositionCategory(POS_CATEGORY);
90                 doc.addPositionUpdater(new DefaultPositionUpdater(POS_CATEGORY) {
91                     protected boolean notDeleted() {
92                         if (fOffset < fPosition.offset && (fPosition.offset + fPosition.length < fOffset + fLength)) {
93                             fPosition.offset= fOffset + fLength; // deleted positions: set to end of remove
94
return false;
95                         }
96                         return true;
97                     }
98                 });
99                 for (int i= 0; i < positions.length; i++) {
100                     try {
101                         doc.addPosition(POS_CATEGORY, positions[i]);
102                     } catch (BadLocationException e) {
103                         throw new IllegalArgumentException JavaDoc("Position outside of string. offset: " + positions[i].offset + ", length: " + positions[i].length + ", string size: " + content.length()); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
104
}
105                 }
106             }
107         } catch (BadPositionCategoryException cannotHappen) {
108             // can not happen: category is correctly set up
109
}
110         return doc;
111     }
112
113     /**
114      * Logs the given throwable.
115      *
116      * @param t the throwable
117      * @since 3.1
118      */

119     public static void log(Throwable JavaDoc t) {
120         Util.log(t, "Exception occured while formatting comments"); //$NON-NLS-1$
121
}
122 }
123
Popular Tags