1
/*******************************************************************************
2
* Copyright (c) 2008 Symbian Software Systems 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
9
* Andrew Ferguson (Symbian) - Initial implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.ui.text.doctools;
13
import java.io.BufferedReader;
14
import java.io.IOException;
15
import java.io.StringReader;
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.jface.text.BadLocationException;
19
import org.eclipse.jface.text.DocumentCommand;
20
import org.eclipse.jface.text.DocumentRewriteSession;
21
import org.eclipse.jface.text.DocumentRewriteSessionType;
22
import org.eclipse.jface.text.IAutoEditStrategy;
23
import org.eclipse.jface.text.IDocument;
24
import org.eclipse.jface.text.IDocumentExtension4;
25
import org.eclipse.jface.text.IRegion;
26
import org.eclipse.jface.text.ITypedRegion;
27
import org.eclipse.jface.text.Region;
28
import org.eclipse.jface.text.TextUtilities;
29
import org.eclipse.ui.IEditorPart;
30
import org.eclipse.ui.IWorkbenchPage;
31
import org.eclipse.ui.IWorkbenchWindow;
32
import org.eclipse.ui.PlatformUI;
34
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
35
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
36
import org.eclipse.cdt.core.dom.ast.IASTNode;
37
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
38
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
39
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
40
import org.eclipse.cdt.core.model.CModelException;
41
import org.eclipse.cdt.core.model.ITranslationUnit;
42
import org.eclipse.cdt.ui.CUIPlugin;
43
import org.eclipse.cdt.ui.IWorkingCopyManager;
44
import org.eclipse.cdt.ui.text.ICPartitions;
47
* This class provides default behaviors for multi-line comment auto-editing.
49
* This class is intended to be sub-classed.
53
public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrategy {
54
protected static final String MULTILINE_START = "/*"; //$NON-NLS-1$#
55
protected static final String MULTILINE_MID = " * "; //$NON-NLS-1$
56
protected static final String MULTILINE_END = "*/"; //$NON-NLS-1$
58
public DefaultMultilineCommentAutoEditStrategy() {
62
* @see org.eclipse.jface.text.IAutoEditStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand)
64
public void customizeDocumentCommand(IDocument doc, DocumentCommand cmd) {
65
if(doc instanceof IDocumentExtension4) {
66
boolean forNewLine= cmd.length == 0 && cmd.text != null && endsWithDelimiter(doc, cmd.text);
67
boolean forCommentEnd= "/".equals(cmd.text); //$NON-NLS-1$
69
if(forNewLine || forCommentEnd) {
70
IDocumentExtension4 ext4= (IDocumentExtension4) doc;
71
DocumentRewriteSession drs= ext4.startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED_SMALL);
74
customizeDocumentAfterNewLine(doc, cmd);
75
} else if (forCommentEnd) {
76
customizeDocumentForMultilineCommentEnd(doc, cmd);
79
ext4.stopRewriteSession(drs);
86
* This implements a rule that when in a multi-line comment context typing a forward slash with
87
* one white space after the "*" will move eliminate the whitespace.
91
protected void customizeDocumentForMultilineCommentEnd(IDocument doc, DocumentCommand command) {
92
if (command.offset < 2 || doc.getLength() == 0) {
96
if ("* ".equals(doc.get(command.offset - 2, 2))) { //$NON-NLS-1$
97
// modify document command
101
} catch (BadLocationException excp) {
107
* Copies the indentation of the previous line and adds a star.
108
* If the comment just started on this line adds also a blank.
110
* @param doc the document to work on
111
* @param c the command to deal with
113
public void customizeDocumentAfterNewLine(IDocument doc, final DocumentCommand c) {
114
int offset= c.offset;
115
if (offset == -1 || doc.getLength() == 0)
118
final StringBuilder buf= new StringBuilder(c.text);
120
// find start of line
121
IRegion line= doc.getLineInformationOfOffset(c.offset);
122
int lineStart= line.getOffset();
123
int firstNonWS= findEndOfWhiteSpaceAt(doc, lineStart, c.offset);
125
IRegion prefix= findPrefixRange(doc, line);
126
String indentation= doc.get(prefix.getOffset(), prefix.getLength());
127
int lengthToAdd= Math.min(offset - prefix.getOffset(), prefix.getLength());
128
buf.append(indentation.substring(0, lengthToAdd));
130
boolean commentAtStart= firstNonWS < c.offset && doc.getChar(firstNonWS) == '/';
131
if (commentAtStart) {
132
// comment started on this line
133
buf.append(MULTILINE_MID);
136
c.shiftsCaret= false;
137
c.caretOffset= c.offset + buf.length();
139
if(commentAtStart && shouldCloseMultiline(doc, c.offset)) {
141
doc.replace(c.offset, 0, indentation+" "+MULTILINE_END); // close the comment in order to parse //$NON-NLS-1$
142
buf.append("\n"); //$NON-NLS-1$
144
// as we are auto-closing, the comment becomes eligible for auto-doc'ing
145
IASTDeclaration dec= null;
146
IASTTranslationUnit ast= getAST();
149
dec= findFollowingDeclaration(ast, offset);
151
IASTNodeSelector ans= ast.getNodeSelector(ast.getFilePath());
152
IASTNode node= ans.findEnclosingNode(offset, 0);
153
if(node instanceof IASTDeclaration) {
154
dec= (IASTDeclaration) node;
160
ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING /* this! */, offset, false);
161
StringBuilder content= customizeAfterNewLineForDeclaration(doc, dec, partition);
162
buf.append(indent(content, indentation + MULTILINE_MID));
165
} catch(BadLocationException ble) {
166
ble.printStackTrace();
170
c.text= buf.toString();
172
} catch (BadLocationException excp) {
177
protected StringBuilder customizeAfterNewLineForDeclaration(IDocument doc, IASTDeclaration dec, ITypedRegion region) {
178
return new StringBuilder();
186
* Locates the {@link IASTDeclaration} most immediately following the specified offset
187
* @param unit the translation unit, or null (in which case the result will also be null)
188
* @param offset the offset to begin the search from
189
* @return the {@link IASTDeclaration} most immediately following the specified offset, or null if there
190
* is no {@link IASTDeclaration}
192
public static IASTDeclaration findFollowingDeclaration(IASTTranslationUnit unit, final int offset) {
193
final IASTDeclaration[] dec= new IASTDeclaration[1];
194
final ASTVisitor av= new ASTVisitor() {
196
shouldVisitTranslationUnit= true;
197
shouldVisitDeclarations= true;
203
IASTDeclaration stopWhenLeaving;
206
public int visit(IASTDeclaration declaration) {
207
IASTNodeLocation loc= declaration.getFileLocation();
209
int candidateOffset= loc.getNodeOffset();
210
int candidateEndOffset= candidateOffset+loc.getNodeLength();
212
if(offset <= candidateOffset) {
214
return PROCESS_ABORT;
217
boolean candidateEnclosesOffset= (offset >= candidateOffset) && (offset < candidateEndOffset);
218
if(candidateEnclosesOffset) {
219
stopWhenLeaving= declaration;
222
return PROCESS_CONTINUE;
225
public int leave(IASTDeclaration declaration) {
226
if(declaration==stopWhenLeaving)
227
return PROCESS_ABORT;
228
return PROCESS_CONTINUE;
239
* @return the AST unit for the active editor, or null if there is no active editor, or
240
* the AST could not be obtained.
242
public IASTTranslationUnit getAST() {
243
final ITranslationUnit unit= getTranslationUnit();
246
IASTTranslationUnit ast= unit.getAST(null, ITranslationUnit.AST_SKIP_ALL_HEADERS);
249
} catch(CModelException ce) {
251
} catch(CoreException ce) {
258
* Assuming the offset is within a multi-line comment, returns a guess as to
259
* whether the enclosing multi-line comment is a new comment. The result is undefined if
260
* the offset does not occur within a multi-line comment.
262
* @param document the document
263
* @param offset the offset
264
* @return <code>true</code> if the comment should be closed, <code>false</code> if not
269
public boolean shouldCloseMultiline(IDocument document, int offset) {
271
IRegion line= document.getLineInformationOfOffset(offset);
272
ITypedRegion partition= TextUtilities.getPartition(document, ICPartitions.C_PARTITIONING, offset, false);
273
int partitionEnd= partition.getOffset() + partition.getLength();
274
if (line.getOffset() >= partitionEnd)
277
String comment= document.get(partition.getOffset(), partition.getLength());
278
if (comment.indexOf(MULTILINE_START, offset - partition.getOffset()) != -1)
279
return true; // enclosed another comment -> probably a new comment
281
if (document.getLength() == partitionEnd) {
282
return !comment.endsWith(MULTILINE_END);
287
} catch (BadLocationException e) {
293
* @return the ITranslationUnit for the active editor, or null if no active
294
* editor could be found.
299
protected static ITranslationUnit getTranslationUnit() {
300
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
304
IWorkbenchPage page= window.getActivePage();
308
IEditorPart editor= page.getActiveEditor();
312
IWorkingCopyManager manager= CUIPlugin.getDefault().getWorkingCopyManager();
313
ITranslationUnit unit= manager.getWorkingCopy(editor.getEditorInput());
321
* Returns a new buffer with the specified indent string inserted at the beginning
322
* of each line in the specified input buffer
326
protected static final StringBuilder indent(StringBuilder buffer, String indent) {
327
StringBuilder result= new StringBuilder();
328
BufferedReader br= new BufferedReader(new StringReader(buffer.toString()));
330
for(String line= br.readLine(); line!=null; line= br.readLine()) {
331
result.append(indent + line + "\n"); //$NON-NLS-1$
333
} catch(IOException ioe) {
334
throw new AssertionError(); // we can't get IO errors from a string backed reader
340
* Returns the offset of the first non-whitespace character in the specified document, searching
341
* right/downward from the specified start offset up to the specified end offset. If there is
342
* no non-whitespace then the end offset is returned.
346
* @throws BadLocationException
348
protected static int findEndOfWhiteSpaceAt(IDocument document, int offset, int end) throws BadLocationException {
349
while (offset < end) {
350
char c= document.getChar(offset);
351
if (c != ' ' && c != '\t') {
360
* Returns the range of the java-doc prefix on the given line in
361
* <code>document</code>. The prefix greedily matches the following regex
362
* pattern: <code>\w*\*\w*</code>, that is, any number of whitespace
363
* characters, followed by an asterisk ('*'), followed by any number of
364
* whitespace characters.
366
* @param document the document to which <code>line</code> refers
367
* @param line the line from which to extract the prefix range
368
* @return an <code>IRegion</code> describing the range of the prefix on
370
* @throws BadLocationException if accessing the document fails
372
protected static IRegion findPrefixRange(IDocument document, IRegion line) throws BadLocationException {
373
int lineOffset= line.getOffset();
374
int lineEnd= lineOffset + line.getLength();
375
int indentEnd= findEndOfWhiteSpaceAt(document, lineOffset, lineEnd);
376
if (indentEnd < lineEnd && document.getChar(indentEnd) == '*') {
378
while (indentEnd < lineEnd && document.getChar(indentEnd) != ' ')
380
while (indentEnd < lineEnd && document.getChar(indentEnd) == ' ')
383
return new Region(lineOffset, indentEnd - lineOffset);
387
* Returns whether the text ends with one of the specified IDocument object's
388
* legal line delimiters.
390
protected static boolean endsWithDelimiter(IDocument d, String txt) {
391
String[] delimiters= d.getLegalLineDelimiters();
392
for (int i= 0; i < delimiters.length; i++) {
393
if (txt.endsWith(delimiters[i]))