X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/SmartSemicolonAutoEditStrategy.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/SmartSemicolonAutoEditStrategy.java index edd3013..5dfea87 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/SmartSemicolonAutoEditStrategy.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/SmartSemicolonAutoEditStrategy.java @@ -1,10 +1,10 @@ /******************************************************************************* * 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 *******************************************************************************/ @@ -12,6 +12,7 @@ package net.sourceforge.phpdt.internal.ui.text; import java.util.Arrays; +import net.sourceforge.phpdt.internal.compiler.parser.Scanner; import net.sourceforge.phpdt.internal.core.Assert; import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager.UndoSpec; import net.sourceforge.phpdt.ui.PreferenceConstants; @@ -41,11 +42,11 @@ import org.eclipse.ui.texteditor.ITextEditorExtension3; /** * Modifies DocumentCommands inserting semicolons and opening braces to place them * smartly, i.e. moving them to the end of a line if that is what the user expects. - * + * *

In practice, semicolons and braces (and the caret) are moved to the end of the line if they are typed * anywhere except for semicolons in a for statements definition. If the line contains a semicolon * or brace after the current caret position, the cursor is moved after it.

- * + * * @see org.eclipse.jface.text.DocumentCommand * @since 3.0 */ @@ -65,7 +66,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Creates a new SmartSemicolonAutoEditStrategy. - * + * * @param partitioning the document partitioning */ public SmartSemicolonAutoEditStrategy(String partitioning) { @@ -96,7 +97,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { return; if (fCharacter == BRACECHAR && !store.getBoolean(PreferenceConstants.EDITOR_SMART_OPENING_BRACE)) return; - + IWorkbenchPage page= PHPeclipsePlugin.getActivePage(); if (page == null) return; @@ -131,17 +132,17 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { if (position < pos) return; - // never double already existing content + // never double already existing content if (alreadyPresent(document, fCharacter, position)) return; - + // don't do special processing if what we do is actually the normal behaviour String insertion= adjustSpacing(document, position, fCharacter); if (command.offset == position && insertion.equals(command.text)) return; try { - + final SmartBackspaceManager manager= (SmartBackspaceManager) editor.getAdapter(SmartBackspaceManager.class); if (manager != null && PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_BACKSPACE)) { TextEdit e1= new ReplaceEdit(command.offset, command.text.length(), document.get(command.offset, command.length)); @@ -160,7 +161,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { s1); manager.register(s2); } - + // 3: modify command command.offset= position; command.length= 0; @@ -174,13 +175,13 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { PHPeclipsePlugin.log(e); } - + } /** * Returns true if the document command is applied on a multi * line selection, false otherwise. - * + * * @param document the document * @param command the command * @return true if command is a multiline command @@ -196,8 +197,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Adds a space before a brace if it is inserted after a parenthesis, equal sign, or one - * of the keywords try, else, do. - * + * of the keywords try, else, do. + * * @param document the document we are working on * @param position the insert position of character * @param character the character to be inserted @@ -226,7 +227,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Checks whether a character to be inserted is already present at the insert location (perhaps * separated by some whitespace from position. - * + * * @param document the document we are working on * @param position the insert position of ch * @param character the character to be inserted @@ -245,7 +246,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Computes the next insert position of the given character in the current line. - * + * * @param document the document we are working on * @param line the line where the change is being made * @param offset the position of the caret in the line when character was typed @@ -282,7 +283,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { if (insertPos > 0 && text.charAt(insertPos - 1) == character) insertPos= insertPos - 1; } - + } else { Assert.isTrue(false); return -1; @@ -294,7 +295,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Computes an insert position for an opening brace if offset maps to a position in * document that looks like being the RHS of an assignment or like an array definition. - * + * * @param document the document being modified * @param line the current line under investigation * @param offset the offset of the caret position, relative to the line start. @@ -307,29 +308,29 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { if (pos == 0) return -1; - + int p= firstNonWhitespaceBackward(document, pos - 1, partitioning, -1); - + if (p == -1) return -1; - + try { - + char ch= document.getChar(p); if (ch != '=' && ch != ']') return -1; - + if (p == 0) return offset; - + p= firstNonWhitespaceBackward(document, p - 1, partitioning, -1); if (p == -1) return -1; - + ch= document.getChar(p); - if (Character.isJavaIdentifierPart(ch) || ch == ']' || ch == '[') + if (Scanner.isPHPIdentifierPart(ch) || ch == ']' || ch == '[') return offset; - + } catch (BadLocationException e) { } return -1; @@ -337,9 +338,9 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Computes an insert position for an opening brace if offset maps to a position in - * document involving a keyword taking a block after it. These are: try, + * document involving a keyword taking a block after it. These are: try, * do, synchronized, static, finally, or else. - * + * * @param document the document being modified * @param line the current line under investigation * @param offset the offset of the caret position, relative to the line start. @@ -367,7 +368,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Computes an insert position for an opening brace if offset maps to a position in * document with a expression in parenthesis that will take a block after the closing parenthesis. - * + * * @param document the document being modified * @param line the current line under investigation * @param offset the offset of the caret position, relative to the line start. @@ -383,7 +384,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { int scanTo= scanForward(document, pos, partitioning, length, '}'); if (scanTo == -1) scanTo= length; - + int closingParen= findClosingParenToLeft(document, pos, partitioning) - 1; while (true) { @@ -419,7 +420,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds a closing parenthesis to the left of position in document, where that parenthesis is only * separated by whitespace from position. If no such parenthesis can be found, position is returned. - * + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -441,7 +442,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds the first whitespace character position to the right of (and including) position. - * + * * @param document the document being modified * @param position the first character position in document to be considered * @return the position of a whitespace character greater or equal than position separated only by whitespace, or -1 if none found @@ -467,8 +468,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds the highest position in document such that the position is <= position * and > bound and Character.isWhitespace(document.getChar(pos)) evaluates to false - * and the position is in the default partition. - * + * and the position is in the default partition. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -494,8 +495,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds the smallest position in document such that the position is >= position * and < bound and Character.isWhitespace(document.getChar(pos)) evaluates to false - * and the position is in the default partition. - * + * and the position is in the default partition. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -521,8 +522,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds the highest position in document such that the position is <= position * and > bound and document.getChar(position) == ch evaluates to true for at least one - * ch in chars and the position is in the default partition. - * + * ch in chars and the position is in the default partition. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -533,9 +534,9 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { private static int scanBackward(IDocument document, int position, String partitioning, int bound, char[] chars) { Assert.isTrue(bound >= -1); Assert.isTrue(position < document.getLength() ); - + Arrays.sort(chars); - + try { while (position > bound) { @@ -552,8 +553,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { // /** // * Finds the highest position in document such that the position is <= position // * and > bound and document.getChar(position) == ch evaluates to true -// * and the position is in the default partition. -// * +// * and the position is in the default partition. +// * // * @param document the document being modified // * @param position the first character position in document to be considered // * @param bound the first position in document to not consider any more, with scanTo > position @@ -567,8 +568,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds the lowest position in document such that the position is >= position * and < bound and document.getChar(position) == ch evaluates to true for at least one - * ch in chars and the position is in the default partition. - * + * ch in chars and the position is in the default partition. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -579,9 +580,9 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { private static int scanForward(IDocument document, int position, String partitioning, int bound, char[] chars) { Assert.isTrue(position >= 0); Assert.isTrue(bound <= document.getLength()); - + Arrays.sort(chars); - + try { while (position < bound) { @@ -598,8 +599,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Finds the lowest position in document such that the position is >= position * and < bound and document.getChar(position) == ch evaluates to true - * and the position is in the default partition. - * + * and the position is in the default partition. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -614,7 +615,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Checks whether the content of document in the range (offset, length) * contains the new keyword. - * + * * @param document the document being modified * @param offset the first character position in document to be considered * @param length the length of the character range to be considered @@ -629,19 +630,19 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { try { String text= document.get(offset, length); int pos= text.indexOf("new"); //$NON-NLS-1$ - + while (pos != -1 && !isDefaultPartition(document, pos + offset, partitioning)) pos= text.indexOf("new", pos + 2); //$NON-NLS-1$ if (pos < 0) return false; - if (pos != 0 && Character.isJavaIdentifierPart(text.charAt(pos - 1))) + if (pos != 0 && Scanner.isPHPIdentifierPart(text.charAt(pos - 1))) return false; - if (pos + 3 < length && Character.isJavaIdentifierPart(text.charAt(pos + 3))) + if (pos + 3 < length && Scanner.isPHPIdentifierPart(text.charAt(pos + 3))) return false; - + return true; } catch (BadLocationException e) { @@ -653,7 +654,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { * Checks whether the content of document at position looks like an * anonymous class definition. position must be to the left of the opening * parenthesis of the definition's parameter list. - * + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -672,7 +673,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Checks whether position resides in a default (Java) partition of document. - * + * * @param document the document being modified * @param position the position to be checked * @param partitioning the document partitioning @@ -681,21 +682,21 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { private static boolean isDefaultPartition(IDocument document, int position, String partitioning) { Assert.isTrue(position >= 0); Assert.isTrue(position <= document.getLength()); - + try { // don't use getPartition2 since we're interested in the scanned character's partition ITypedRegion region= TextUtilities.getPartition(document, partitioning, position, false); return region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE); - + } catch (BadLocationException e) { } - + return false; } /** * Finds the position of the parenthesis matching the closing parenthesis at position. - * + * * @param document the document being modified * @param position the position in document of a closing parenthesis * @param partitioning the document partitioning @@ -712,18 +713,18 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { try { Assert.isTrue(document.getChar(position) == CLOSING_PAREN); - + int depth= 1; while (true) { position= scanBackward(document, position - 1, partitioning, -1, new char[] {CLOSING_PAREN, OPENING_PAREN}); if (position == -1) return -1; - + if (document.getChar(position) == CLOSING_PAREN) depth++; else depth--; - + if (depth == 0) return position; } @@ -734,10 +735,10 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { } /** - * Checks whether, to the left of position and separated only by whitespace, + * Checks whether, to the left of position and separated only by whitespace, * document contains a keyword taking a parameter list and a block after it. - * These are: if, while, catch, for, synchronized, switch. - * + * These are: if, while, catch, for, synchronized, switch. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -757,12 +758,12 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { } /** - * Checks whether code>document contains the String like such + * Checks whether code>document contains the String like such * that its last character is at position. If like starts with a - * identifier part (as determined by {@link Character#isJavaIdentifier(char)}), it is also made + * identifier part (as determined by {@link Scanner#isPHPIdentifierPart(char)}), it is also made * sure that like is preceded by some non-identifier character or stands at the * document start. - * + * * @param document the document being modified * @param position the first character position in document to be considered * @param like the String to look for. @@ -777,7 +778,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { if (!like.equals(document.get(position - length + 1, length))) return false; - if (position >= length && Character.isJavaIdentifierPart(like.charAt(0)) && Character.isJavaIdentifierPart(document.getChar(position - length))) + if (position >= length && Scanner.isPHPIdentifierPart(like.charAt(0)) && Scanner.isPHPIdentifierPart(document.getChar(position - length))) return false; } catch (BadLocationException e) { @@ -791,23 +792,23 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { * Checks whether the content of document at position looks like a * method declaration header (i.e. only the return type and method name). position * must be just left of the opening parenthesis of the parameter list. - * + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning * @return true if the content of document looks like a method definition, false otherwise */ private static boolean looksLikeMethodDecl(IDocument document, int position, String partitioning) { - + // method name position= eatIdentToLeft(document, position, partitioning); if (position < 1) return false; - + position= eatBrackets(document, position - 1, partitioning); if (position < 1) return false; - + position= eatIdentToLeft(document, position - 1, partitioning); return position != -1; @@ -816,9 +817,9 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * From position to the left, eats any whitespace and then a pair of brackets * as used to declare an array return type like
String [ ]
. - * The return value is either the position of the opening bracket or position if no - * pair of brackets can be parsed. - * + * The return value is either the position of the opening bracket or position if no + * pair of brackets can be parsed. + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -840,12 +841,12 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { } /** - * From position to the left, eats any whitespace and the first identifier, returning + * From position to the left, eats any whitespace and the first identifier, returning * the position of the first identifier character (in normal read order). *

When called on a document with content " some string " and positionition 13, the * return value will be 6 (the first letter in string). *

- * + * * @param document the document being modified * @param position the first character position in document to be considered * @param partitioning the document partitioning @@ -855,7 +856,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { if (position < 0) return -1; Assert.isTrue(position < document.getLength()); - + int p= firstNonWhitespaceBackward(document, position, partitioning, -1); if (p == -1) return -1; @@ -864,7 +865,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { while (p >= 0) { char ch= document.getChar(p); - if (Character.isJavaIdentifierPart(ch)) { + if (Scanner.isPHPIdentifierPart(ch)) { p--; continue; } @@ -888,7 +889,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Returns a position in the first java partition after the last non-empty and non-comment partition. * There is no non-whitespace from the returned position to the end of the partition it is contained in. - * + * * @param document the document being modified * @param line the line under investigation * @param offset the caret offset into line @@ -925,11 +926,11 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /** * Returns a valid insert location (except for whitespace) in partition or -1 if - * there is no valid insert location. + * there is no valid insert location. * An valid insert location is right after any java string or character partition, or at the end - * of a java default partition, but never behind maxOffset. Comment partitions or + * of a java default partition, but never behind maxOffset. Comment partitions or * empty java partitions do never yield valid insert positions. - * + * * @param doc the document being modified * @param partition the current partition * @param maxOffset the maximum offset to consider @@ -953,6 +954,8 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { return endOffset; if (IPHPPartitions.PHP_STRING_SQ.equals(partition.getType())) return endOffset; + if (IPHPPartitions.PHP_STRING_HEREDOC.equals(partition.getType())) + return endOffset; if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) { try { if (doc.get(partition.getOffset(), endOffset - partition.getOffset()).trim().length() == 0) @@ -963,7 +966,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { return INVALID; } } - // default: we don't know anything about the partition - assume valid + // default: we don't know anything about the partition - assume valid return endOffset; } @@ -971,7 +974,7 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { * Determines whether the current line contains a for statement. * Algorithm: any "for" word in the line is a positive, "for" contained in a string literal will * produce a false positive. - * + * * @param line the line where the change is being made * @param offset the position of the caret * @return true if line contains for, false otherwise @@ -980,16 +983,16 @@ public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy { /* searching for (^|\s)for(\s|$) */ int forPos= line.indexOf("for"); //$NON-NLS-1$ if (forPos != -1) { - if ((forPos == 0 || !Character.isJavaIdentifierPart(line.charAt(forPos - 1))) && (line.length() == forPos + 3 || !Character.isJavaIdentifierPart(line.charAt(forPos + 3)))) + if ((forPos == 0 || !Scanner.isPHPIdentifierPart(line.charAt(forPos - 1))) && (line.length() == forPos + 3 || !Scanner.isPHPIdentifierPart(line.charAt(forPos + 3)))) return true; } return false; } /** - * Returns the position in text after which there comes only whitespace, up to + * Returns the position in text after which there comes only whitespace, up to * offset. - * + * * @param text the text being searched * @param offset the maximum offset to search for * @return the smallest value v such that text.substring(v, offset).trim() == 0