From b000a3c5e349c04dbaa5fc6cfa749072e3e19223 Mon Sep 17 00:00:00 2001 From: toshihiro Date: Thu, 12 Jul 2007 02:57:58 +0000 Subject: [PATCH] Fixed: 1345914 - indentation badly wanted (repaired java indenter. not created php indenter. also this doesn't affect html part.) --- .../phpdt/internal/ui/actions/IndentAction.java | 70 +++++----- .../internal/ui/text/JavaHeuristicScanner.java | 60 ++++++++- .../phpdt/internal/ui/text/JavaIndenter.java | 149 +++++++++++--------- 3 files changed, 178 insertions(+), 101 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/actions/IndentAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/actions/IndentAction.java index 44cfdb5..ce26e9b 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/actions/IndentAction.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/actions/IndentAction.java @@ -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: diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/JavaHeuristicScanner.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/JavaHeuristicScanner.java index 6bea251..0ca33ab 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/JavaHeuristicScanner.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/JavaHeuristicScanner.java @@ -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("= 1) { + if (fDocument.get(fPos - 1, 2).equalsIgnoreCase("switch 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; } } } -- 1.7.1