Fixed: 1345914 - indentation badly wanted
authortoshihiro <toshihiro>
Thu, 12 Jul 2007 02:57:58 +0000 (02:57 +0000)
committertoshihiro <toshihiro>
Thu, 12 Jul 2007 02:57:58 +0000 (02:57 +0000)
(repaired java indenter. not created php indenter. also this doesn't affect html part.)

net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/actions/IndentAction.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/JavaHeuristicScanner.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/JavaIndenter.java

index 44cfdb5..ce26e9b 100644 (file)
@@ -277,39 +277,40 @@ public class IndentAction extends TextEditorAction {
                                        to++;
                                indent = command.text.substring(1, to);
 
-                       } else if (!fIsTabAction && partition.getOffset() == offset
-                                       && type.equals(IPHPPartitions.PHP_SINGLELINE_COMMENT)) {
-
-                               // line comment starting at position 0 -> indent inside
-                               int slashes = 2;
-                               while (slashes < document.getLength() - 1
-                                               && document.get(offset + slashes, 2).equals("//")) //$NON-NLS-1$
-                                       slashes += 2;
-
-                               wsStart = offset + slashes;
-
-                               StringBuffer computed = indenter.computeIndentation(offset);
-                               int tabSize = PHPeclipsePlugin
-                                               .getDefault()
-                                               .getPreferenceStore()
-                                               .getInt(
-                                                               AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
-                               while (slashes > 0 && computed.length() > 0) {
-                                       char c = computed.charAt(0);
-                                       if (c == '\t')
-                                               if (slashes > tabSize)
-                                                       slashes -= tabSize;
-                                               else
-                                                       break;
-                                       else if (c == ' ')
-                                               slashes--;
-                                       else
-                                               break;
-
-                                       computed.deleteCharAt(0);
-                               }
-
-                               indent = document.get(offset, wsStart - offset) + computed;
+// omit Java style
+//                     } else if (!fIsTabAction && partition.getOffset() == offset
+//                                     && type.equals(IPHPPartitions.PHP_SINGLELINE_COMMENT)) {
+//
+//                             // line comment starting at position 0 -> indent inside
+//                             int slashes = 2;
+//                             while (slashes < document.getLength() - 1
+//                                             && document.get(offset + slashes, 2).equals("//")) //$NON-NLS-1$
+//                                     slashes += 2;
+//
+//                             wsStart = offset + slashes;
+//
+//                             StringBuffer computed = indenter.computeIndentation(offset);
+//                             int tabSize = PHPeclipsePlugin
+//                                             .getDefault()
+//                                             .getPreferenceStore()
+//                                             .getInt(
+//                                                             AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
+//                             while (slashes > 0 && computed.length() > 0) {
+//                                     char c = computed.charAt(0);
+//                                     if (c == '\t')
+//                                             if (slashes > tabSize)
+//                                                     slashes -= tabSize;
+//                                             else
+//                                                     break;
+//                                     else if (c == ' ')
+//                                             slashes--;
+//                                     else
+//                                             break;
+//
+//                                     computed.deleteCharAt(0);
+//                             }
+//
+//                             indent = document.get(offset, wsStart - offset) + computed;
 
                        }
                }
@@ -320,7 +321,8 @@ public class IndentAction extends TextEditorAction {
                        if (computed != null)
                                indent = computed.toString();
                        else
-                               indent = new String();
+                               //indent = new String();
+                               return true; // prevent affecting html part
                }
 
                // change document:
index 6bea251..0ca33ab 100644 (file)
@@ -13,6 +13,7 @@ package net.sourceforge.phpdt.internal.ui.text;
 import java.util.Arrays;
 
 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
+import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
 
 import org.eclipse.jface.text.Assert;
 import org.eclipse.jface.text.BadLocationException;
@@ -327,8 +328,10 @@ public class JavaHeuristicScanner implements Symbols {
         *            the document to scan.
         */
        public JavaHeuristicScanner(IDocument document) {
+//             this(document, IPHPPartitions.PHP_PARTITIONING,
+//                             IDocument.DEFAULT_CONTENT_TYPE);
                this(document, IPHPPartitions.PHP_PARTITIONING,
-                               IDocument.DEFAULT_CONTENT_TYPE);
+                               PHPDocumentPartitioner.PHP_SCRIPT_CODE);
        }
 
        /**
@@ -380,7 +383,25 @@ public class JavaHeuristicScanner implements Symbols {
                case QUESTIONMARK:
                        return TokenQUESTIONMARK;
                case EQUAL:
+                       try {
+                               if (fDocument.getChar(fPos) == '>') {
+                                       fPos++;
+                                       return TokenOTHER;
+                               }
+                       } catch (BadLocationException e) {
+                       }
                        return TokenEQUAL;
+               case '<':
+                       try {
+                               if (fDocument.get(fPos, 4).equalsIgnoreCase("?php")) {
+                                       fPos += 4;
+                                       return TokenEOF;
+                               } else if (fDocument.getChar(fPos) == '?') {
+                                       fPos++;
+                                       return TokenEOF;
+                               }
+                       } catch (BadLocationException e) {
+                       }
                }
 
                // else
@@ -451,6 +472,18 @@ public class JavaHeuristicScanner implements Symbols {
                        return TokenQUESTIONMARK;
                case EQUAL:
                        return TokenEQUAL;
+               case '>':
+                       try {
+                               switch (fDocument.getChar(fPos)) {
+                               case '=':
+                                       fPos--;
+                                       return TokenOTHER;
+                               case '?':
+                                       fPos--;
+                                       return TokenEOF;
+                               }
+                       } catch (BadLocationException e) {
+                       }
                }
 
                // else
@@ -752,6 +785,15 @@ public class JavaHeuristicScanner implements Symbols {
                        while (fPos < bound) {
 
                                fChar = fDocument.getChar(fPos);
+                               // omit closing tag
+                               if (fChar == '?') {
+                                       if (fPos < fDocument.getLength() - 1) {
+                                               if (fDocument.get(fPos - 1, 2).equalsIgnoreCase("?>")) {
+                                                       fPos++;
+                                                       return NOT_FOUND;
+                                               }
+                                       }
+                               }
                                if (condition.stop(fChar, fPos, true))
                                        return fPos;
 
@@ -841,6 +883,22 @@ public class JavaHeuristicScanner implements Symbols {
                        while (fPos > bound) {
 
                                fChar = fDocument.getChar(fPos);
+                               // omit opening tag
+                               if (fChar == 'p' || fChar == 'P') {
+                                       if (fPos >= 4) {
+                                               if (fDocument.get(fPos - 4, 5).equalsIgnoreCase("<?php")) {
+                                                       fPos -= 4;
+                                                       return NOT_FOUND;
+                                               }
+                                       }
+                               } else if (fChar == '?') {
+                                       if (fPos >= 1) {
+                                               if (fDocument.get(fPos - 1, 2).equalsIgnoreCase("<?")) {
+                                                       fPos--;
+                                                       return NOT_FOUND;
+                                               }
+                                       }
+                               }
                                if (condition.stop(fChar, fPos, false))
                                        return fPos;
 
index fa4715c..d3fb1b8 100644 (file)
@@ -19,6 +19,8 @@ import org.eclipse.jface.text.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;
 
 /**
@@ -168,7 +170,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);
 
@@ -407,6 +410,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 +517,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 +564,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 +802,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 +887,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;
                        }
                }
        }