replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / phpdoc / JavaDocAutoIndentStrategy.java
index 40f04d2..a8b6374 100644 (file)
@@ -1,17 +1,17 @@
 /*******************************************************************************
  * Copyright (c) 2000, 2004 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials 
+ * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/cpl-v10.html
- * 
+ *
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *******************************************************************************/
 
 package net.sourceforge.phpdt.internal.ui.text.phpdoc;
 
+
 import java.text.BreakIterator;
 
 import net.sourceforge.phpdt.core.ICompilationUnit;
@@ -29,7 +29,7 @@ import org.eclipse.core.runtime.CoreException;
 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;
@@ -46,26 +46,26 @@ import org.eclipse.ui.texteditor.ITextEditorExtension3;
 /**
  * Auto indent strategy for java doc comments
  */
-public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
+public class JavaDocAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy {
 
        private String fPartitioning;
 
        /**
         * Creates a new Javadoc auto indent strategy for the given document partitioning.
-        * 
+        *
         * @param partitioning the document partitioning
         */
        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$
        }
@@ -79,19 +79,19 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
         * @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
@@ -104,7 +104,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                                                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
@@ -123,14 +123,14 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 //                                                                     // ignore
 //                                                             }
 //                                                     }
-                                               }                                               
+                                               }
 
                                        }
-                               }                       
+                               }
                        }
-                       
+
                        c.text= buf.toString();
-                       
+
                } catch (BadLocationException excp) {
                        // stop work
                }
@@ -145,16 +145,16 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                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.
         */
@@ -173,12 +173,12 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 //             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
        {
@@ -188,7 +188,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                }
                return null;
        }
-       
+
        private String createMethodTags(IDocument document, DocumentCommand command, String indentation, String lineDelimiter, IMethod method)
                throws CoreException, BadLocationException
        {
@@ -196,7 +196,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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) {
@@ -219,7 +219,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 //             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;
@@ -229,7 +229,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                                // modify document command
                                c.length++;
                                c.offset--;
-                       }                                       
+                       }
                } catch (BadLocationException excp) {
                        // stop work
                }
@@ -240,36 +240,36 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
         * 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;
@@ -282,12 +282,12 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
         * @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);
@@ -299,7 +299,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                                        return;
                                }
                        }
-                       
+
                        if (command.text != null && command.text.equals("/")) { //$NON-NLS-1$
                                jdocIndentForCommentEnd(document, command);
                                return;
@@ -307,8 +307,8 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                        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;
@@ -318,11 +318,11 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                        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);
 
@@ -335,21 +335,21 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                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;
 
@@ -358,13 +358,13 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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);
@@ -374,7 +374,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                                // 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];
@@ -386,13 +386,13 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                                        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;
                        }
                }
 
@@ -401,20 +401,20 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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];
                }
        }
 
        /**
         * Method jdocWrapParagraphFromLine.
-        * 
+        *
         * @param document
         * @param line
         * @param always
@@ -432,7 +432,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                        if (!isLineTooLong(document, line) && !isLineTooShort(document, line))
                                return;
                }
-               
+
                boolean caretRelativeToParagraphOffset= false;
                int caret= caretOffset[0];
 
@@ -449,7 +449,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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) {
@@ -458,12 +458,12 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                                }
                        }
 
-                       String string= getLineContents(document, currentLine);                  
+                       String string= getLineContents(document, currentLine);
                        buffer.append(string);
                        currentLine++;
                }
                String paragraph= buffer.toString();
-               
+
                if (paragraph.trim().length() == 0)
                        return;
 
@@ -477,32 +477,32 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                caretOffset[0]= caretRelativeToParagraphOffset ? caretOffset[0] + beginning : caret + beginning;
        }
-       
+
        /**
-        * Line break iterator to handle whitespaces as first class citizens. 
+        * 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;
@@ -525,16 +525,16 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                        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.  
-        * 
+        * Formats a paragraph, using break iterator.
+        *
         * @param offset an offset within the paragraph, which will be updated with respect to formatting.
         */
        private static String formatParagraph(String paragraph, int[] offset, String prefix, String lineDelimiter, int margin) {
@@ -544,7 +544,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                StringBuffer paragraphBuffer= new StringBuffer();
                StringBuffer lineBuffer= new StringBuffer();
                StringBuffer whiteSpaceBuffer= new StringBuffer();
-               
+
                int index= offset[0];
                int indexBuffer= -1;
 
@@ -560,7 +560,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                        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());
@@ -568,8 +568,8 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                        } 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());
@@ -604,23 +604,23 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                        }
                }
 
-               // 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();
        }
 
        /**
@@ -629,7 +629,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
         */
        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;
                }
@@ -639,7 +639,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                                column += tabWidth - (column % tabWidth);
                        else
                                column++;
-                               
+
                return column;
        }
 
@@ -666,10 +666,10 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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());
        }
 
        /**
@@ -702,14 +702,14 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
        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.
@@ -725,10 +725,10 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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
@@ -747,10 +747,10 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
        }
 
        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);
@@ -759,7 +759,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                        // erase line delimiter
                        String lineDelimiter= document.getLineDelimiter(line);
                        if (lineDelimiter != null && lineDelimiter.equals(text)) {
-                               
+
                                String prefix= jdocExtractLinePrefix(document, line + 1);
 
                                // strip prefix if any
@@ -794,7 +794,7 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                } catch (BadLocationException e) {
                  PHPeclipsePlugin.log(e);
                }
-                               
+
                try {
                        int line= document.getLineOfOffset(c.offset);
                        int lineOffset= document.getLineOffset(line);
@@ -818,9 +818,9 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
                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();
@@ -834,5 +834,5 @@ public class JavaDocAutoIndentStrategy extends DefaultAutoIndentStrategy {
 
                return unit;
        }
-                       
+
 }