package net.sourceforge.phpdt.internal.ui.text.phpdoc;
-
+
import java.text.BreakIterator;
import net.sourceforge.phpdt.core.ICompilationUnit;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.DefaultAutoIndentStrategy;
+import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
/**
* Auto indent strategy for java doc comments
*/
-public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
+public class JavaDocAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
private String fPartitioning;
public JavaDocAutoIndentStrategy(String partitioning) {
fPartitioning= partitioning;
}
-
+
private static String getLineDelimiter(IDocument document) {
try {
if (document.getNumberOfLines() > 1)
return document.getLineDelimiter(0);
} catch (BadLocationException e) {
PHPeclipsePlugin.log(e);
- }
+ }
return System.getProperty("line.separator"); //$NON-NLS-1$
}
* @param c the command to deal with
*/
private void jdocIndentAfterNewLine(IDocument d, DocumentCommand c) {
-
+
if (c.offset == -1 || d.getLength() == 0)
return;
-
+
try {
// find start of line
int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset);
IRegion info= d.getLineInformationOfOffset(p);
int start= info.getOffset();
-
+
// find white spaces
int end= findEndOfWhiteSpace(d, start, c.offset);
-
+
StringBuffer buf= new StringBuffer(c.text);
if (end >= start) { // 1GEYL1R: ITPJUI:ALL - java doc edit smartness not work for class comments
// append to input
if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CLOSE_JAVADOCS) && isNewComment(d, c.offset, fPartitioning)) {
String lineDelimiter= getLineDelimiter(d);
-
+
String endTag= lineDelimiter + indentation + " */"; //$NON-NLS-1$
d.replace(c.offset, 0, endTag); //$NON-NLS-1$
// evaluate method signature
// // ignore
// }
// }
- }
+ }
}
- }
+ }
}
-
+
c.text= buf.toString();
-
+
} catch (BadLocationException excp) {
// stop work
}
switch (element.getElementType()) {
case IJavaElement.TYPE:
- return createTypeTags(document, command, indentation, lineDelimiter, (IType) element);
+ return createTypeTags(document, command, indentation, lineDelimiter, (IType) element);
case IJavaElement.METHOD:
return createMethodTags(document, command, indentation, lineDelimiter, (IMethod) element);
default:
return null;
- }
+ }
}
-
+
/*
* Removes start and end of a comment and corrects indentation and line delimiters.
*/
// return Strings.changeIndent(comment, 0, CodeFormatterUtil.getTabWidth(), indentation, lineDelimiter);
return Strings.changeIndent(comment, 0, getTabWidth(), indentation, lineDelimiter);
}
-
+
public static int getTabWidth() {
Preferences preferences= PHPeclipsePlugin.getDefault().getPluginPreferences();
return preferences.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
}
-
+
private String createTypeTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, IType type)
throws CoreException
{
}
return null;
}
-
+
private String createMethodTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, IMethod method)
throws CoreException, BadLocationException
{
ISourceRange sourceRange= method.getSourceRange();
if (sourceRange == null || sourceRange.getOffset() != partition.getOffset())
return null;
-
+
// IMethod inheritedMethod= getInheritedMethod(method);
// String comment= CodeGeneration.getMethodComment(method, inheritedMethod, lineDelimiter);
// if (comment != null) {
// return JavaModelUtil.findMethodDeclarationInHierarchy(typeHierarchy, declaringType,
// method.getElementName(), method.getParameterTypes(), method.isConstructor());
// }
-
+
protected void jdocIndentForCommentEnd(IDocument d, DocumentCommand c) {
if (c.offset < 2 || d.getLength() == 0) {
return;
// modify document command
c.length++;
c.offset--;
- }
+ }
} catch (BadLocationException excp) {
// stop work
}
* If in doubt, it will assume that the javadoc is new.
*/
private static boolean isNewComment(IDocument document, int commandOffset, String partitioning) {
-
+
try {
int lineIndex= document.getLineOfOffset(commandOffset) + 1;
if (lineIndex >= document.getNumberOfLines())
return true;
-
+
IRegion line= document.getLineInformation(lineIndex);
ITypedRegion partition= TextUtilities.getPartition(document, partitioning, commandOffset, false);
int partitionEnd= partition.getOffset() + partition.getLength();
if (line.getOffset() >= partitionEnd)
return false;
-
+
if (document.getLength() == partitionEnd)
return true; // partition goes to end of document - probably a new comment
-
+
String comment= document.get(partition.getOffset(), partition.getLength());
if (comment.indexOf("/*", 2) != -1) //$NON-NLS-1$
return true; // enclosed another comment -> probably a new comment
-
+
return false;
-
+
} catch (BadLocationException e) {
return false;
- }
+ }
}
-
+
private boolean isSmartMode() {
IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
if (page != null) {
- IEditorPart part= page.getActiveEditor();
+ IEditorPart part= page.getActiveEditor();
if (part instanceof ITextEditorExtension3) {
ITextEditorExtension3 extension= (ITextEditorExtension3) part;
return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT;
* @see IAutoIndentStrategy#customizeDocumentCommand
*/
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
-
+
if (!isSmartMode())
return;
try {
-
+
if (command.text != null && command.length == 0) {
String[] lineDelimiters= document.getLegalLineDelimiters();
int index= TextUtilities.endsWith(lineDelimiters, command.text);
return;
}
}
-
+
if (command.text != null && command.text.equals("/")) { //$NON-NLS-1$
jdocIndentForCommentEnd(document, command);
return;
ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, true);
int partitionStart= partition.getOffset();
- int partitionEnd= partition.getLength() + partitionStart;
-
+ int partitionEnd= partition.getLength() + partitionStart;
+
String text= command.text;
int offset= command.offset;
int length= command.length;
final int POSTFIX_LENGTH= "*/".length(); //$NON-NLS-1$
if ((offset < partitionStart + PREFIX_LENGTH || offset + length > partitionEnd - POSTFIX_LENGTH) ||
text != null && text.length() >= 2 && ((text.indexOf("*/") != -1) || (document.getChar(offset) == '*' && text.startsWith("/")))) //$NON-NLS-1$ //$NON-NLS-2$
- return;
+ return;
if (command.text == null || command.text.length() == 0)
jdocHandleBackspaceDelete(document, command);
-
+
else if (command.text != null && command.length == 0 && command.text.length() > 0)
jdocWrapParagraphOnInsert(document, command);
if (!command.doit)
return;
-
+
document.replace(command.offset, command.length, command.text);
command.doit= false;
if (command.text != null)
command.offset += command.text.length();
command.length= 0;
- command.text= null;
+ command.text= null;
}
protected void jdocWrapParagraphOnInsert(IDocument document, DocumentCommand command) throws BadLocationException {
// Assert.isTrue(command.length == 0);
// Assert.isTrue(command.text != null && command.text.length() == 1);
-
+
if (!getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_FORMAT_JAVADOCS))
return;
int lineOffset= region.getOffset();
int lineLength= region.getLength();
- String lineContents= document.get(lineOffset, lineLength);
+ String lineContents= document.get(lineOffset, lineLength);
StringBuffer buffer= new StringBuffer(lineContents);
int start= command.offset - lineOffset;
- int end= command.length + start;
+ int end= command.length + start;
buffer.replace(start, end, command.text);
-
- // handle whitespace
+
+ // handle whitespace
if (command.text != null && command.text.length() != 0 && command.text.trim().length() == 0) {
String endOfLine= document.get(command.offset, lineOffset + lineLength - command.offset);
// move caret to next line
flushCommand(document, command);
- if (isLineTooShort(document, line)) {
+ if (isLineTooShort(document, line)) {
int[] caretOffset= {command.offset};
jdocWrapParagraphFromLine(document, line, caretOffset, false);
command.offset= caretOffset[0];
String lineDelimiter= document.getLineDelimiter(line);
String nextLinePrefix= jdocExtractLinePrefix(document, line + 1);
command.offset += lineDelimiter.length() + nextLinePrefix.length();
- }
+ }
return;
// inside whitespace at end of line
} else if (endOfLine.trim().length() == 0) {
// simply insert space
- return;
+ return;
}
}
boolean wrapAlways= command.offset >= lineOffset && command.offset <= lineOffset + prefix.length();
// must insert the text now because it may include whitepace
- flushCommand(document, command);
+ flushCommand(document, command);
if (wrapAlways || calculateDisplayedWidth(buffer.toString()) > getMargin() || isLineTooShort(document, line)) {
- int[] caretOffset= {command.offset};
+ int[] caretOffset= {command.offset};
jdocWrapParagraphFromLine(document, line, caretOffset, wrapAlways);
- if (!wrapAlways)
+ if (!wrapAlways)
command.offset= caretOffset[0];
}
}
if (!isLineTooLong(document, line) && !isLineTooShort(document, line))
return;
}
-
+
boolean caretRelativeToParagraphOffset= false;
int caret= caretOffset[0];
StringBuffer buffer= new StringBuffer();
int currentLine= line;
while (line == currentLine || isJavaDocLine(document, currentLine)) {
-
+
if (buffer.length() != 0 && !Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
buffer.append(' ');
if (currentLine <= caretLine) {
}
}
- String string= getLineContents(document, currentLine);
+ String string= getLineContents(document, currentLine);
buffer.append(string);
currentLine++;
}
String paragraph= buffer.toString();
-
+
if (paragraph.trim().length() == 0)
return;
caretOffset[0]= caretRelativeToParagraphOffset ? caretOffset[0] + beginning : caret + beginning;
}
-
+
/**
* Line break iterator to handle whitespaces as first class citizens.
*/
private static class LineBreakIterator {
-
+
private final String fString;
private final BreakIterator fIterator= BreakIterator.getLineInstance();
-
+
private int fStart;
private int fEnd;
private int fBufferedEnd;
-
+
public LineBreakIterator(String string) {
fString= string;
fIterator.setText(string);
}
-
+
public int first() {
fBufferedEnd= -1;
fStart= fIterator.first();
- return fStart;
+ return fStart;
}
-
+
public int next() {
-
+
if (fBufferedEnd != -1) {
fStart= fEnd;
fEnd= fBufferedEnd;
final String word= string.trim();
if (word.length() == string.length())
return fEnd;
-
+
// suspected whitespace
fBufferedEnd= fEnd;
return fStart + word.length();
- }
+ }
}
-
+
/**
* Formats a paragraph, using break iterator.
*
StringBuffer paragraphBuffer= new StringBuffer();
StringBuffer lineBuffer= new StringBuffer();
StringBuffer whiteSpaceBuffer= new StringBuffer();
-
+
int index= offset[0];
int indexBuffer= -1;
- // line delimiter could be null
- if (lineDelimiter == null)
- lineDelimiter= ""; //$NON-NLS-1$
+ // line delimiter could be null
+ if (lineDelimiter == null)
+ lineDelimiter= ""; //$NON-NLS-1$
for (int start= iterator.first(), end= iterator.next(); end != BreakIterator.DONE; start= end, end= iterator.next()) {
if (word.trim().length() == 0) {
whiteSpaceBuffer.append(word);
- // first word of line is always appended
+ // first word of line is always appended
} else if (lineBuffer.length() == 0) {
lineBuffer.append(prefix);
lineBuffer.append(whiteSpaceBuffer.toString());
} else {
String line= lineBuffer.toString() + whiteSpaceBuffer.toString() + word.toString();
-
- // margin exceeded
+
+ // margin exceeded
if (calculateDisplayedWidth(line) > margin) {
// flush line buffer and wrap paragraph
paragraphBuffer.append(lineBuffer.toString());
}
}
- // flush line buffer
+ // flush line buffer
paragraphBuffer.append(lineBuffer.toString());
paragraphBuffer.append(lineDelimiter);
// flush index buffer
- if (indexBuffer != -1)
+ if (indexBuffer != -1)
offset[0]= indexBuffer;
// last position is not returned by break iterator
else if (offset[0] == paragraph.length())
offset[0]= paragraphBuffer.length() - lineDelimiter.length();
-
+
return paragraphBuffer.toString();
}
private static IPreferenceStore getPreferenceStore() {
- return PHPeclipsePlugin.getDefault().getPreferenceStore();
+ return PHPeclipsePlugin.getDefault().getPreferenceStore();
}
/**
*/
private static int calculateDisplayedWidth(String string) {
- int tabWidth= getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
+ int tabWidth= getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
if (tabWidth<=0) {
tabWidth = 2;
}
column += tabWidth - (column % tabWidth);
else
column++;
-
+
return column;
}
private String getLineContents(IDocument d, int line) throws BadLocationException {
int offset = d.getLineOffset(line);
int length = d.getLineLength(line);
- String lineDelimiter= d.getLineDelimiter(line);
- if (lineDelimiter != null)
- length= length - lineDelimiter.length();
+ String lineDelimiter= d.getLineDelimiter(line);
+ if (lineDelimiter != null)
+ length= length - lineDelimiter.length();
String lineContents = d.get(offset, length);
int trim = jdocExtractLinePrefix(d, line).length();
return lineContents.substring(trim);
}
-
+
private static String getLine(IDocument document, int line) throws BadLocationException {
- IRegion region= document.getLineInformation(line);
- return document.get(region.getOffset(), region.getLength());
+ IRegion region= document.getLineInformation(line);
+ return document.get(region.getOffset(), region.getLength());
}
/**
private static final String[] fgInlineTags= {
"<b>", "<i>", "<em>", "<strong>", "<code>" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
};
-
+
private boolean isInlineTag(String string) {
for (int i= 0; i < fgInlineTags.length; i++)
if (string.startsWith(fgInlineTags[i]))
- return true;
+ return true;
return false;
}
-
+
/**
* returns true if the specified line is part of a paragraph and should be merged with
* the previous line.
length -= firstChar - offset;
String lineContents= document.get(firstChar, length);
- String prefix= lineContents.trim();
+ String prefix= lineContents.trim();
if (!prefix.startsWith("*") || prefix.startsWith("*/")) //$NON-NLS-1$ //$NON-NLS-2$
return false;
-
+
lineContents= lineContents.substring(1).trim().toLowerCase();
// preserve empty lines
}
protected void jdocHandleBackspaceDelete(IDocument document, DocumentCommand c) {
-
+
if (!getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_FORMAT_JAVADOCS))
return;
-
+
try {
String text= document.get(c.offset, c.length);
int line= document.getLineOfOffset(c.offset);
// erase line delimiter
String lineDelimiter= document.getLineDelimiter(line);
if (lineDelimiter != null && lineDelimiter.equals(text)) {
-
+
String prefix= jdocExtractLinePrefix(document, line + 1);
// strip prefix if any
} catch (BadLocationException e) {
PHPeclipsePlugin.log(e);
}
-
+
try {
int line= document.getLineOfOffset(c.offset);
int lineOffset= document.getLineOffset(line);
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null)
return null;
-
+
IWorkbenchPage page= window.getActivePage();
- if (page == null)
+ if (page == null)
return null;
IEditorPart editor= page.getActiveEditor();
return unit;
}
-
+
}