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;
}
}
if (computed != null)
indent = computed.toString();
else
- indent = new String();
+ //indent = new String();
+ return true; // prevent affecting html part
}
// change document:
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;
* 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);
}
/**
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
return TokenQUESTIONMARK;
case EQUAL:
return TokenEQUAL;
+ case '>':
+ try {
+ switch (fDocument.getChar(fPos)) {
+ case '=':
+ fPos--;
+ return TokenOTHER;
+ case '?':
+ fPos--;
+ return TokenEOF;
+ }
+ } catch (BadLocationException e) {
+ }
}
// else
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;
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;
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;
/**
return null;
// add additional indent
- indent.append(createIndent(fIndent));
+ //indent.append(createIndent(fIndent));
+ indent.insert(0, createIndent(fIndent));
if (fIndent < 0)
unindent(indent);
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);
// 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) {
// 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) {
*
* @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:
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;
}
}
}