1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / JavaIndenter.java
index fa4715c..d6b5cf1 100644 (file)
@@ -15,10 +15,14 @@ import net.sourceforge.phpdt.core.formatter.DefaultCodeFormatterConstants;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.core.runtime.Plugin;
-import org.eclipse.jface.text.Assert;
+//incastrix
+//import org.eclipse.jface.text.Assert;
+import org.eclipse.core.runtime.Assert;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITypedRegion;
+import org.eclipse.jface.text.TextUtilities;
 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
 
 /**
@@ -90,9 +94,9 @@ public class JavaIndenter {
         *         reference position to <code>offset</code> resides, or
         *         <code>null</code> if it cannot be determined
         */
-       public StringBuffer getReferenceIndentation(int offset) {
-               return getReferenceIndentation(offset, false);
-       }
+//     public StringBuffer getReferenceIndentation(int offset) {
+//             return getReferenceIndentation(offset, false);
+//     }
 
        /**
         * Computes the indentation at the reference point of <code>position</code>.
@@ -168,7 +172,8 @@ public class JavaIndenter {
                        return null;
 
                // add additional indent
-               indent.append(createIndent(fIndent));
+               //indent.append(createIndent(fIndent));
+               indent.insert(0, createIndent(fIndent));
                if (fIndent < 0)
                        unindent(indent);
 
@@ -326,9 +331,9 @@ public class JavaIndenter {
         * @return the reference statement relative to which <code>offset</code>
         *         should be indented, or {@link JavaHeuristicScanner#NOT_FOUND}
         */
-       public int findReferencePosition(int offset) {
-               return findReferencePosition(offset, peekChar(offset));
-       }
+//     public int findReferencePosition(int offset) {
+//             return findReferencePosition(offset, peekChar(offset));
+//     }
 
        /**
         * Peeks the next char in the document that comes after <code>offset</code>
@@ -407,6 +412,15 @@ public class JavaIndenter {
                                                prevPos + 1 - lineOffset).trim().length() == 0;
                                int prevToken = fScanner.previousToken(prevPos,
                                                JavaHeuristicScanner.UNBOUND);
+                               if (prevToken == Symbols.TokenEOF && nextToken == Symbols.TokenEOF) {
+                                       ITypedRegion partition = TextUtilities.getPartition(fDocument, IPHPPartitions.PHP_PARTITIONING, offset, true);
+                                       if (partition.getType().equals(IPHPPartitions.PHP_SINGLELINE_COMMENT)) {
+                                               fAlign = fScanner.getPosition();
+                                       } else {
+                                               fAlign = JavaHeuristicScanner.NOT_FOUND;
+                                       }
+                                       return JavaHeuristicScanner.NOT_FOUND;
+                               }
                                boolean bracelessBlockStart = fScanner.isBracelessBlockStart(
                                                prevPos, JavaHeuristicScanner.UNBOUND);
 
@@ -505,31 +519,34 @@ public class JavaIndenter {
                // align braces, but handle the case where we align with the method
                // declaration start instead of
                // the opening brace.
-               // if (matchBrace) {
-               // if (skipScope(Symbols.TokenLBRACE, Symbols.TokenRBRACE)) {
-               // try {
-               // // align with the opening brace that is on a line by its own
-               // int lineOffset= fDocument.getLineOffset(fLine);
-               // if (lineOffset <= fPosition && fDocument.get(lineOffset, fPosition -
-               // lineOffset).trim().length() == 0)
-               // return fPosition;
-               // } catch (BadLocationException e) {
-               // // concurrent modification - walk default path
-               // }
-               // // if the opening brace is not on the start of the line, skip to the
-               // start
-               // int pos= skipToStatementStart(true, true);
-               // fIndent= 0; // indent is aligned with reference position
-               // return pos;
-               // } else {
-               // // if we can't find the matching brace, the heuristic is to unindent
-               // // by one against the normal position
-               // int pos= findReferencePosition(offset, danglingElse, false,
-               // matchParen, matchCase);
-               // fIndent--;
-               // return pos;
-               // }
-               // }
+               if (matchBrace) {
+                       if (skipScope(Symbols.TokenLBRACE, Symbols.TokenRBRACE)) {
+                               try {
+                                       // align with the opening brace that is on a line by its own
+                                       int lineOffset = fDocument.getLineOffset(fLine);
+                                       if (lineOffset <= fPosition
+                                                       && fDocument
+                                                                       .get(lineOffset, fPosition - lineOffset)
+                                                                       .trim().length() == 0)
+                                               return fPosition;
+                               } catch (BadLocationException e) {
+                                       // concurrent modification - walk default path
+                               }
+                               // if the opening brace is not on the start of the line, skip to
+                               // the start
+                               int pos = skipToStatementStart(true, true);
+                               fIndent = 0; // indent is aligned with reference position
+                               return pos;
+                       } else {
+                               // if we can't find the matching brace, the heuristic is to
+                               // unindent
+                               // by one against the normal position
+                               int pos = findReferencePosition(offset, danglingElse, false,
+                                               matchParen, matchCase);
+                               fIndent--;
+                               return pos;
+                       }
+               }
 
                // align parenthesis'
                if (matchParen) {
@@ -549,9 +566,9 @@ public class JavaIndenter {
                // the only reliable way to get case labels aligned (due to many
                // different styles of using braces in a block)
                // is to go for another case statement, or the scope opening brace
-               // if (matchCase) {
-               // return matchCaseAlignment();
-               // }
+               if (matchCase) {
+                       return matchCaseAlignment();
+               }
 
                nextToken();
                switch (fToken) {
@@ -787,41 +804,40 @@ public class JavaIndenter {
         * 
         * @return the reference offset for a <code>switch</code> label
         */
-       // private int matchCaseAlignment() {
-       // while (true) {
-       // nextToken();
-       // switch (fToken) {
-       // // invalid cases: another case label or an LBRACE must come before a case
-       // // -> bail out with the current position
-       // case Symbols.TokenLPAREN:
-       // case Symbols.TokenLBRACKET:
-       // case Symbols.TokenEOF:
-       // return fPosition;
-       // case Symbols.TokenLBRACE:
-       // // opening brace of switch statement
-       // fIndent= prefCaseIndent();
-       // return fPosition;
-       // case Symbols.TokenCASE:
-       // case Symbols.TokenDEFAULT:
-       // // align with previous label
-       // fIndent= 0;
-       // return fPosition;
-       //
-       // // scopes: skip them
-       // case Symbols.TokenRPAREN:
-       // case Symbols.TokenRBRACKET:
-       // case Symbols.TokenRBRACE:
-       // skipScope();
-       // break;
-       //
-       // default:
-       // // keep searching
-       // continue;
-       //
-       // }
-       // }
-       // }
-       /**
+       private int matchCaseAlignment() {
+               while (true) {
+                       nextToken();
+                       switch (fToken) {
+                       // invalid cases: another case label or an LBRACE must come before a
+                       // case
+                       // -> bail out with the current position
+                       case Symbols.TokenLPAREN:
+                       case Symbols.TokenLBRACKET:
+                       case Symbols.TokenEOF:
+                               return fPosition;
+                       case Symbols.TokenLBRACE:
+                               // opening brace of switch statement
+                               fIndent = 1; //prefCaseIndent() is for Java
+                               return fPosition;
+                       case Symbols.TokenCASE:
+                       case Symbols.TokenDEFAULT:
+                               // align with previous label
+                               fIndent = 0;
+                               return fPosition;
+                       // scopes: skip them
+                       case Symbols.TokenRPAREN:
+                       case Symbols.TokenRBRACKET:
+                       case Symbols.TokenRBRACE:
+                               skipScope();
+                               break;
+                       default:
+                               // keep searching
+                               continue;
+                       }
+               }
+       }
+
+        /**
         * Returns the reference position for a list element. The algorithm tries to
         * match any previous indentation on the same list. If there is none, the
         * reference position returned is determined depending on the type of list:
@@ -873,14 +889,17 @@ public class JavaIndenter {
                        case Symbols.TokenQUESTIONMARK:
                                if (prefTernaryDeepAlign()) {
                                        setFirstElementAlignment(fPosition - 1, fPosition + 1);
-                                       return fPosition;
                                } else {
                                        fIndent = prefTernaryIndent();
-                                       return fPosition;
                                }
+                               return fPosition;
                        case Symbols.TokenEOF:
                                return 0;
 
+                       case Symbols.TokenEQUAL:
+                               // indent assignments
+                               fIndent= prefAssignmentIndent();
+                               return fPosition;
                        }
                }
        }
@@ -1447,41 +1466,41 @@ public class JavaIndenter {
                return 1; // sensible default
        }
 
-       private boolean prefIndentBracesForBlocks() {
-               Plugin plugin = JavaCore.getPlugin();
-               if (plugin != null) {
-                       String option = JavaCore
-                                       .getOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK);
-                       return option
-                                       .equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED);
-               }
-
-               return false; // sensible default
-       }
-
-       private boolean prefIndentBracesForArrays() {
-               Plugin plugin = JavaCore.getPlugin();
-               if (plugin != null) {
-                       String option = JavaCore
-                                       .getOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ARRAY_INITIALIZER);
-                       return option
-                                       .equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED);
-               }
-
-               return false; // sensible default
-       }
-
-       private boolean prefIndentBracesForMethods() {
-               Plugin plugin = JavaCore.getPlugin();
-               if (plugin != null) {
-                       String option = JavaCore
-                                       .getOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_METHOD_DECLARATION);
-                       return option
-                                       .equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED);
-               }
-
-               return false; // sensible default
-       }
+//     private boolean prefIndentBracesForBlocks() {
+//             Plugin plugin = JavaCore.getPlugin();
+//             if (plugin != null) {
+//                     String option = JavaCore
+//                                     .getOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_BLOCK);
+//                     return option
+//                                     .equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED);
+//             }
+//
+//             return false; // sensible default
+//     }
+
+//     private boolean prefIndentBracesForArrays() {
+//             Plugin plugin = JavaCore.getPlugin();
+//             if (plugin != null) {
+//                     String option = JavaCore
+//                                     .getOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_ARRAY_INITIALIZER);
+//                     return option
+//                                     .equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED);
+//             }
+//
+//             return false; // sensible default
+//     }
+
+//     private boolean prefIndentBracesForMethods() {
+//             Plugin plugin = JavaCore.getPlugin();
+//             if (plugin != null) {
+//                     String option = JavaCore
+//                                     .getOption(DefaultCodeFormatterConstants.FORMATTER_BRACE_POSITION_FOR_METHOD_DECLARATION);
+//                     return option
+//                                     .equals(DefaultCodeFormatterConstants.NEXT_LINE_SHIFTED);
+//             }
+//
+//             return false; // sensible default
+//     }
 
        private int prefContinuationIndent() {
                Plugin plugin = JavaCore.getPlugin();