1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.util.Arrays;
15 import net.sourceforge.phpdt.internal.core.Assert;
16 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager.UndoSpec;
17 import net.sourceforge.phpdt.ui.PreferenceConstants;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.PHPUnitEditor;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.DocumentCommand;
24 import org.eclipse.jface.text.IAutoEditStrategy;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.ITypedRegion;
29 import org.eclipse.jface.text.Region;
30 import org.eclipse.jface.text.TextSelection;
31 import org.eclipse.jface.text.TextUtilities;
32 import org.eclipse.text.edits.DeleteEdit;
33 import org.eclipse.text.edits.MalformedTreeException;
34 import org.eclipse.text.edits.ReplaceEdit;
35 import org.eclipse.text.edits.TextEdit;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IWorkbenchPage;
38 import org.eclipse.ui.texteditor.ITextEditorExtension2;
39 import org.eclipse.ui.texteditor.ITextEditorExtension3;
42 * Modifies <code>DocumentCommand</code>s inserting semicolons and opening braces to place them
43 * smartly, i.e. moving them to the end of a line if that is what the user expects.
45 * <p>In practice, semicolons and braces (and the caret) are moved to the end of the line if they are typed
46 * anywhere except for semicolons in a <code>for</code> statements definition. If the line contains a semicolon
47 * or brace after the current caret position, the cursor is moved after it.</p>
49 * @see org.eclipse.jface.text.DocumentCommand
52 public class SmartSemicolonAutoEditStrategy implements IAutoEditStrategy {
54 /** String representation of a semicolon. */
55 private static final String SEMICOLON= ";"; //$NON-NLS-1$
56 /** Char representation of a semicolon. */
57 private static final char SEMICHAR= ';';
58 /** String represenattion of a opening brace. */
59 private static final String BRACE= "{"; //$NON-NLS-1$
60 /** Char representation of a opening brace */
61 private static final char BRACECHAR= '{';
63 private char fCharacter;
64 private String fPartitioning;
67 * Creates a new SmartSemicolonAutoEditStrategy.
69 * @param partitioning the document partitioning
71 public SmartSemicolonAutoEditStrategy(String partitioning) {
72 fPartitioning= partitioning;
76 * @see org.eclipse.jface.text.IAutoEditStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand)
78 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
80 // also customize if <code>doit</code> is false (so it works in code completion situations)
84 if (command.text == null)
87 if (command.text.equals(SEMICOLON))
89 else if (command.text.equals(BRACE))
90 fCharacter= BRACECHAR;
94 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
95 if (fCharacter == SEMICHAR && !store.getBoolean(PreferenceConstants.EDITOR_SMART_SEMICOLON))
97 if (fCharacter == BRACECHAR && !store.getBoolean(PreferenceConstants.EDITOR_SMART_OPENING_BRACE))
100 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
103 IEditorPart part= page.getActiveEditor();
104 if (!(part instanceof PHPUnitEditor))
106 PHPUnitEditor editor= (PHPUnitEditor)part;
107 if (editor.getInsertMode() != ITextEditorExtension3.SMART_INSERT || !editor.isEditable())
109 ITextEditorExtension2 extension= (ITextEditorExtension2)editor.getAdapter(ITextEditorExtension2.class);
110 if (extension != null && !extension.validateEditorInputState())
112 if (isMultilineSelection(document, command))
115 // 1: find concerned line / position in java code, location in statement
116 int pos= command.offset;
119 IRegion l= document.getLineInformationOfOffset(pos);
120 line= new TextSelection(document, l.getOffset(), l.getLength());
121 } catch (BadLocationException e) {
125 // 2: choose action based on findings (is for-Statement?)
126 // for now: compute the best position to insert the new character
127 int positionInLine= computeCharacterPosition(document, line, pos - line.getOffset(), fCharacter, fPartitioning);
128 int position= positionInLine + line.getOffset();
130 // never position before the current position!
134 // never double already existing content
135 if (alreadyPresent(document, fCharacter, position))
138 // don't do special processing if what we do is actually the normal behaviour
139 String insertion= adjustSpacing(document, position, fCharacter);
140 if (command.offset == position && insertion.equals(command.text))
145 final SmartBackspaceManager manager= (SmartBackspaceManager) editor.getAdapter(SmartBackspaceManager.class);
146 if (manager != null && PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_SMART_BACKSPACE)) {
147 TextEdit e1= new ReplaceEdit(command.offset, command.text.length(), document.get(command.offset, command.length));
148 UndoSpec s1= new UndoSpec(command.offset + command.text.length(),
149 new Region(command.offset, 0),
154 DeleteEdit smart= new DeleteEdit(position, insertion.length());
155 ReplaceEdit raw= new ReplaceEdit(command.offset, command.length, command.text);
156 UndoSpec s2= new UndoSpec(position + insertion.length(),
157 new Region(command.offset + command.text.length(), 0),
158 new TextEdit[] {smart, raw},
161 manager.register(s2);
165 command.offset= position;
167 command.caretOffset= position;
168 command.text= insertion;
171 } catch (MalformedTreeException e) {
172 PHPeclipsePlugin.log(e);
173 } catch (BadLocationException e) {
174 PHPeclipsePlugin.log(e);
181 * Returns <code>true</code> if the document command is applied on a multi
182 * line selection, <code>false</code> otherwise.
184 * @param document the document
185 * @param command the command
186 * @return <code>true</code> if <code>command</code> is a multiline command
188 private boolean isMultilineSelection(IDocument document, DocumentCommand command) {
190 return document.getNumberOfLines(command.offset, command.length) > 1;
191 } catch (BadLocationException e) {
198 * Adds a space before a brace if it is inserted after a parenthesis, equal sign, or one
199 * of the keywords <code>try, else, do</code>.
201 * @param document the document we are working on
202 * @param position the insert position of <code>character</code>
203 * @param character the character to be inserted
204 * @return a <code>String</code> consisting of <code>character</code> plus any additional spacing
206 private String adjustSpacing(IDocument doc, int position, char character) {
207 if (character == BRACECHAR) {
208 if (position > 0 && position <= doc.getLength()) {
209 int pos= position - 1;
210 if (looksLike(doc, pos, ")") //$NON-NLS-1$
211 || looksLike(doc, pos, "=") //$NON-NLS-1$
212 || looksLike(doc, pos, "]") //$NON-NLS-1$
213 || looksLike(doc, pos, "try") //$NON-NLS-1$
214 || looksLike(doc, pos, "else") //$NON-NLS-1$
215 || looksLike(doc, pos, "synchronized") //$NON-NLS-1$
216 || looksLike(doc, pos, "static") //$NON-NLS-1$
217 || looksLike(doc, pos, "finally") //$NON-NLS-1$
218 || looksLike(doc, pos, "do")) //$NON-NLS-1$
219 return new String(new char[] { ' ', character });
223 return new String(new char[] { character });
227 * Checks whether a character to be inserted is already present at the insert location (perhaps
228 * separated by some whitespace from <code>position</code>.
230 * @param document the document we are working on
231 * @param position the insert position of <code>ch</code>
232 * @param character the character to be inserted
233 * @return <code>true</code> if <code>ch</code> is already present at <code>location</code>, <code>false</code> otherwise
235 private boolean alreadyPresent(IDocument document, char ch, int position) {
236 int pos= firstNonWhitespaceForward(document, position, fPartitioning, document.getLength());
238 if (pos != -1 && document.getChar(pos) == ch)
240 } catch (BadLocationException e) {
247 * Computes the next insert position of the given character in the current line.
249 * @param document the document we are working on
250 * @param line the line where the change is being made
251 * @param offset the position of the caret in the line when <code>character</code> was typed
252 * @param character the character to look for
253 * @param partitioning the document partitioning
254 * @return the position where <code>character</code> should be inserted / replaced
256 protected static int computeCharacterPosition(IDocument document, ITextSelection line, int offset, char character, String partitioning) {
257 String text= line.getText();
262 if (character == BRACECHAR) {
264 insertPos= computeArrayInitializationPos(document, line, offset, partitioning);
266 if (insertPos == -1) {
267 insertPos= computeAfterTryDoElse(document, line, offset);
270 if (insertPos == -1) {
271 insertPos= computeAfterParenthesis(document, line, offset, partitioning);
274 } else if (character == SEMICHAR) {
276 if (isForStatement(text, offset)) {
277 insertPos= -1; // don't do anything in for statements, as semis are vital part of these
279 int nextPartitionPos= nextPartitionOrLineEnd(document, line, offset, partitioning);
280 insertPos= startOfWhitespaceBeforeOffset(text, nextPartitionPos);
281 // if there is a semi present, return its location as alreadyPresent() will take it out this way.
282 if (insertPos > 0 && text.charAt(insertPos - 1) == character)
283 insertPos= insertPos - 1;
287 Assert.isTrue(false);
295 * Computes an insert position for an opening brace if <code>offset</code> maps to a position in
296 * <code>document</code> that looks like being the RHS of an assignment or like an array definition.
298 * @param document the document being modified
299 * @param line the current line under investigation
300 * @param offset the offset of the caret position, relative to the line start.
301 * @param partitioning the document partitioning
302 * @return an insert position relative to the line start if <code>line</code> looks like being an array initialization at <code>offset</code>, -1 otherwise
304 private static int computeArrayInitializationPos(IDocument document, ITextSelection line, int offset, String partitioning) {
305 // search backward while WS, find = (not != <= >= ==) in default partition
306 int pos= offset + line.getOffset();
311 int p= firstNonWhitespaceBackward(document, pos - 1, partitioning, -1);
318 char ch= document.getChar(p);
319 if (ch != '=' && ch != ']')
325 p= firstNonWhitespaceBackward(document, p - 1, partitioning, -1);
329 ch= document.getChar(p);
330 if (Character.isJavaIdentifierPart(ch) || ch == ']' || ch == '[')
333 } catch (BadLocationException e) {
339 * Computes an insert position for an opening brace if <code>offset</code> maps to a position in
340 * <code>document</code> involving a keyword taking a block after it. These are: <code>try</code>,
341 * <code>do</code>, <code>synchronized</code>, <code>static</code>, <code>finally</code>, or <code>else</code>.
343 * @param document the document being modified
344 * @param line the current line under investigation
345 * @param offset the offset of the caret position, relative to the line start.
346 * @return an insert position relative to the line start if <code>line</code> contains one of the above keywords at or before <code>offset</code>, -1 otherwise
348 private static int computeAfterTryDoElse(IDocument doc, ITextSelection line, int offset) {
349 // search backward while WS, find 'try', 'do', 'else' in default partition
350 int p= offset + line.getOffset();
351 p= firstWhitespaceToRight(doc, p);
356 if (looksLike(doc, p, "try") //$NON-NLS-1$
357 || looksLike(doc, p, "do") //$NON-NLS-1$
358 || looksLike(doc, p, "synchronized") //$NON-NLS-1$
359 || looksLike(doc, p, "static") //$NON-NLS-1$
360 || looksLike(doc, p, "finally") //$NON-NLS-1$
361 || looksLike(doc, p, "else")) //$NON-NLS-1$
362 return p + 1 - line.getOffset();
368 * Computes an insert position for an opening brace if <code>offset</code> maps to a position in
369 * <code>document</code> with a expression in parenthesis that will take a block after the closing parenthesis.
371 * @param document the document being modified
372 * @param line the current line under investigation
373 * @param offset the offset of the caret position, relative to the line start.
374 * @param partitioning the document partitioning
375 * @return an insert position relative to the line start if <code>line</code> contains a parenthesized expression that can be followed by a block, -1 otherwise
377 private static int computeAfterParenthesis(IDocument document, ITextSelection line, int offset, String partitioning) {
378 // find the opening parenthesis for every closing parenthesis on the current line after offset
379 // return the position behind the closing parenthesis if it looks like a method declaration
380 // or an expression for an if, while, for, catch statement
381 int pos= offset + line.getOffset();
382 int length= line.getOffset() + line.getLength();
383 int scanTo= scanForward(document, pos, partitioning, length, '}');
387 int closingParen= findClosingParenToLeft(document, pos, partitioning) - 1;
390 int startScan= closingParen + 1;
391 closingParen= scanForward(document, startScan, partitioning, scanTo, ')');
392 if (closingParen == -1)
395 int openingParen= findOpeningParenMatch(document, closingParen, partitioning);
397 // no way an expression at the beginning of the document can mean anything
398 if (openingParen < 1)
401 // only select insert positions for parenthesis currently embracing the caret
402 if (openingParen > pos)
405 if (looksLikeAnonymousClassDef(document, openingParen - 1, partitioning))
406 return closingParen + 1 - line.getOffset();
408 if (looksLikeIfWhileForCatch(document, openingParen - 1, partitioning))
409 return closingParen + 1 - line.getOffset();
411 if (looksLikeMethodDecl(document, openingParen - 1, partitioning))
412 return closingParen + 1 - line.getOffset();
420 * Finds a closing parenthesis to the left of <code>position</code> in document, where that parenthesis is only
421 * separated by whitespace from <code>position</code>. If no such parenthesis can be found, <code>position</code> is returned.
423 * @param document the document being modified
424 * @param position the first character position in <code>document</code> to be considered
425 * @param partitioning the document partitioning
426 * @return the position of a closing parenthesis left to <code>position</code> separated only by whitespace, or <code>position</code> if no parenthesis can be found
428 private static int findClosingParenToLeft(IDocument document, int position, String partitioning) {
429 final char CLOSING_PAREN= ')';
434 int nonWS= firstNonWhitespaceBackward(document, position - 1, partitioning, -1);
435 if (nonWS != -1 && document.getChar(nonWS) == CLOSING_PAREN)
437 } catch (BadLocationException e1) {
443 * Finds the first whitespace character position to the right of (and including) <code>position</code>.
445 * @param document the document being modified
446 * @param position the first character position in <code>document</code> to be considered
447 * @return the position of a whitespace character greater or equal than <code>position</code> separated only by whitespace, or -1 if none found
449 private static int firstWhitespaceToRight(IDocument document, int position) {
450 int length= document.getLength();
451 Assert.isTrue(position >= 0);
452 Assert.isTrue(position <= length);
455 while (position < length) {
456 char ch= document.getChar(position);
457 if (Character.isWhitespace(ch))
462 } catch (BadLocationException e) {
468 * Finds the highest position in <code>document</code> such that the position is <= <code>position</code>
469 * and > <code>bound</code> and <code>Character.isWhitespace(document.getChar(pos))</code> evaluates to <code>false</code>
470 * and the position is in the default partition.
472 * @param document the document being modified
473 * @param position the first character position in <code>document</code> to be considered
474 * @param partitioning the document partitioning
475 * @param bound the first position in <code>document</code> to not consider any more, with <code>bound</code> > <code>position</code>
476 * @return the highest position of one element in <code>chars</code> in [<code>position</code>, <code>scanTo</code>) that resides in a Java partition, or <code>-1</code> if none can be found
478 private static int firstNonWhitespaceBackward(IDocument document, int position, String partitioning, int bound) {
479 Assert.isTrue(position < document.getLength());
480 Assert.isTrue(bound >= -1);
483 while (position > bound) {
484 char ch= document.getChar(position);
485 if (!Character.isWhitespace(ch) && isDefaultPartition(document, position, partitioning))
489 } catch (BadLocationException e) {
495 * Finds the smallest position in <code>document</code> such that the position is >= <code>position</code>
496 * and < <code>bound</code> and <code>Character.isWhitespace(document.getChar(pos))</code> evaluates to <code>false</code>
497 * and the position is in the default partition.
499 * @param document the document being modified
500 * @param position the first character position in <code>document</code> to be considered
501 * @param partitioning the document partitioning
502 * @param bound the first position in <code>document</code> to not consider any more, with <code>bound</code> > <code>position</code>
503 * @return the smallest position of one element in <code>chars</code> in [<code>position</code>, <code>scanTo</code>) that resides in a Java partition, or <code>-1</code> if none can be found
505 private static int firstNonWhitespaceForward(IDocument document, int position, String partitioning, int bound) {
506 Assert.isTrue(position >= 0);
507 Assert.isTrue(bound <= document.getLength());
510 while (position < bound) {
511 char ch= document.getChar(position);
512 if (!Character.isWhitespace(ch) && isDefaultPartition(document, position, partitioning))
516 } catch (BadLocationException e) {
522 * Finds the highest position in <code>document</code> such that the position is <= <code>position</code>
523 * and > <code>bound</code> and <code>document.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
524 * ch in <code>chars</code> and the position is in the default partition.
526 * @param document the document being modified
527 * @param position the first character position in <code>document</code> to be considered
528 * @param partitioning the document partitioning
529 * @param bound the first position in <code>document</code> to not consider any more, with <code>scanTo</code> > <code>position</code>
530 * @param chars an array of <code>char</code> to search for
531 * @return the highest position of one element in <code>chars</code> in (<code>bound</code>, <code>position</code>] that resides in a Java partition, or <code>-1</code> if none can be found
533 private static int scanBackward(IDocument document, int position, String partitioning, int bound, char[] chars) {
534 Assert.isTrue(bound >= -1);
535 Assert.isTrue(position < document.getLength() );
540 while (position > bound) {
542 if (Arrays.binarySearch(chars, document.getChar(position)) >= 0 && isDefaultPartition(document, position, partitioning))
547 } catch (BadLocationException e) {
553 // * Finds the highest position in <code>document</code> such that the position is <= <code>position</code>
554 // * and > <code>bound</code> and <code>document.getChar(position) == ch</code> evaluates to <code>true</code>
555 // * and the position is in the default partition.
557 // * @param document the document being modified
558 // * @param position the first character position in <code>document</code> to be considered
559 // * @param bound the first position in <code>document</code> to not consider any more, with <code>scanTo</code> > <code>position</code>
560 // * @param chars an array of <code>char</code> to search for
561 // * @return the highest position of one element in <code>chars</code> in [<code>position</code>, <code>scanTo</code>) that resides in a Java partition, or <code>-1</code> if none can be found
563 // private static int scanBackward(IDocument document, int position, int bound, char ch) {
564 // return scanBackward(document, position, bound, new char[] {ch});
568 * Finds the lowest position in <code>document</code> such that the position is >= <code>position</code>
569 * and < <code>bound</code> and <code>document.getChar(position) == ch</code> evaluates to <code>true</code> for at least one
570 * ch in <code>chars</code> and the position is in the default partition.
572 * @param document the document being modified
573 * @param position the first character position in <code>document</code> to be considered
574 * @param partitioning the document partitioning
575 * @param bound the first position in <code>document</code> to not consider any more, with <code>scanTo</code> > <code>position</code>
576 * @param chars an array of <code>char</code> to search for
577 * @return the lowest position of one element in <code>chars</code> in [<code>position</code>, <code>bound</code>) that resides in a Java partition, or <code>-1</code> if none can be found
579 private static int scanForward(IDocument document, int position, String partitioning, int bound, char[] chars) {
580 Assert.isTrue(position >= 0);
581 Assert.isTrue(bound <= document.getLength());
586 while (position < bound) {
588 if (Arrays.binarySearch(chars, document.getChar(position)) >= 0 && isDefaultPartition(document, position, partitioning))
593 } catch (BadLocationException e) {
599 * Finds the lowest position in <code>document</code> such that the position is >= <code>position</code>
600 * and < <code>bound</code> and <code>document.getChar(position) == ch</code> evaluates to <code>true</code>
601 * and the position is in the default partition.
603 * @param document the document being modified
604 * @param position the first character position in <code>document</code> to be considered
605 * @param partitioning the document partitioning
606 * @param bound the first position in <code>document</code> to not consider any more, with <code>scanTo</code> > <code>position</code>
607 * @param chars an array of <code>char</code> to search for
608 * @return the lowest position of one element in <code>chars</code> in [<code>position</code>, <code>bound</code>) that resides in a Java partition, or <code>-1</code> if none can be found
610 private static int scanForward(IDocument document, int position, String partitioning, int bound, char ch) {
611 return scanForward(document, position, partitioning, bound, new char[] {ch});
615 * Checks whether the content of <code>document</code> in the range (<code>offset</code>, <code>length</code>)
616 * contains the <code>new</code> keyword.
618 * @param document the document being modified
619 * @param offset the first character position in <code>document</code> to be considered
620 * @param length the length of the character range to be considered
621 * @param partitioning the document partitioning
622 * @return <code>true</code> if the specified character range contains a <code>new</code> keyword, <code>false</code> otherwise.
624 private static boolean isNewMatch(IDocument document, int offset, int length, String partitioning) {
625 Assert.isTrue(length >= 0);
626 Assert.isTrue(offset >= 0);
627 Assert.isTrue(offset + length < document.getLength() + 1);
630 String text= document.get(offset, length);
631 int pos= text.indexOf("new"); //$NON-NLS-1$
633 while (pos != -1 && !isDefaultPartition(document, pos + offset, partitioning))
634 pos= text.indexOf("new", pos + 2); //$NON-NLS-1$
639 if (pos != 0 && Character.isJavaIdentifierPart(text.charAt(pos - 1)))
642 if (pos + 3 < length && Character.isJavaIdentifierPart(text.charAt(pos + 3)))
647 } catch (BadLocationException e) {
653 * Checks whether the content of <code>document</code> at <code>position</code> looks like an
654 * anonymous class definition. <code>position</code> must be to the left of the opening
655 * parenthesis of the definition's parameter list.
657 * @param document the document being modified
658 * @param position the first character position in <code>document</code> to be considered
659 * @param partitioning the document partitioning
660 * @return <code>true</code> if the content of <code>document</code> looks like an anonymous class definition, <code>false</code> otherwise
662 private static boolean looksLikeAnonymousClassDef(IDocument document, int position, String partitioning) {
663 int previousCommaOrParen= scanBackward(document, position - 1, partitioning, -1, new char[] {',', '('});
664 if (previousCommaOrParen == -1 || position < previousCommaOrParen + 5) // 2 for borders, 3 for "new"
667 if (isNewMatch(document, previousCommaOrParen + 1, position - previousCommaOrParen - 2, partitioning))
674 * Checks whether <code>position</code> resides in a default (Java) partition of <code>document</code>.
676 * @param document the document being modified
677 * @param position the position to be checked
678 * @param partitioning the document partitioning
679 * @return <code>true</code> if <code>position</code> is in the default partition of <code>document</code>, <code>false</code> otherwise
681 private static boolean isDefaultPartition(IDocument document, int position, String partitioning) {
682 Assert.isTrue(position >= 0);
683 Assert.isTrue(position <= document.getLength());
686 // don't use getPartition2 since we're interested in the scanned character's partition
687 ITypedRegion region= TextUtilities.getPartition(document, partitioning, position, false);
688 return region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE);
690 } catch (BadLocationException e) {
697 * Finds the position of the parenthesis matching the closing parenthesis at <code>position</code>.
699 * @param document the document being modified
700 * @param position the position in <code>document</code> of a closing parenthesis
701 * @param partitioning the document partitioning
702 * @return the position in <code>document</code> of the matching parenthesis, or -1 if none can be found
704 private static int findOpeningParenMatch(IDocument document, int position, String partitioning) {
705 final char CLOSING_PAREN= ')';
706 final char OPENING_PAREN= '(';
708 Assert.isTrue(position < document.getLength());
709 Assert.isTrue(position >= 0);
710 Assert.isTrue(isDefaultPartition(document, position, partitioning));
714 Assert.isTrue(document.getChar(position) == CLOSING_PAREN);
718 position= scanBackward(document, position - 1, partitioning, -1, new char[] {CLOSING_PAREN, OPENING_PAREN});
722 if (document.getChar(position) == CLOSING_PAREN)
731 } catch (BadLocationException e) {
737 * Checks whether, to the left of <code>position</code> and separated only by whitespace,
738 * <code>document</code> contains a keyword taking a parameter list and a block after it.
739 * These are: <code>if</code>, <code>while</code>, <code>catch</code>, <code>for</code>, <code>synchronized</code>, <code>switch</code>.
741 * @param document the document being modified
742 * @param position the first character position in <code>document</code> to be considered
743 * @param partitioning the document partitioning
744 * @return <code>true</code> if <code>document</code> contains any of the above keywords to the left of <code>position</code>, <code>false</code> otherwise
746 private static boolean looksLikeIfWhileForCatch(IDocument document, int position, String partitioning) {
747 position= firstNonWhitespaceBackward(document, position, partitioning, -1);
751 return looksLike(document, position, "if") //$NON-NLS-1$
752 || looksLike(document, position, "while") //$NON-NLS-1$
753 || looksLike(document, position, "catch") //$NON-NLS-1$
754 || looksLike(document, position, "synchronized") //$NON-NLS-1$
755 || looksLike(document, position, "switch") //$NON-NLS-1$
756 || looksLike(document, position, "for"); //$NON-NLS-1$
760 * Checks whether code>document</code> contains the <code>String</code> <code>like</code> such
761 * that its last character is at <code>position</code>. If <code>like</code> starts with a
762 * identifier part (as determined by {@link Character#isJavaIdentifier(char)}), it is also made
763 * sure that <code>like</code> is preceded by some non-identifier character or stands at the
766 * @param document the document being modified
767 * @param position the first character position in <code>document</code> to be considered
768 * @param like the <code>String</code> to look for.
769 * @return <code>true</code> if <code>document</code> contains <code>like</code> such that it ends at <code>position</code>, <code>false</code> otherwise
771 private static boolean looksLike(IDocument document, int position, String like) {
772 int length= like.length();
773 if (position < length - 1)
777 if (!like.equals(document.get(position - length + 1, length)))
780 if (position >= length && Character.isJavaIdentifierPart(like.charAt(0)) && Character.isJavaIdentifierPart(document.getChar(position - length)))
783 } catch (BadLocationException e) {
791 * Checks whether the content of <code>document</code> at <code>position</code> looks like a
792 * method declaration header (i.e. only the return type and method name). <code>position</code>
793 * must be just left of the opening parenthesis of the parameter list.
795 * @param document the document being modified
796 * @param position the first character position in <code>document</code> to be considered
797 * @param partitioning the document partitioning
798 * @return <code>true</code> if the content of <code>document</code> looks like a method definition, <code>false</code> otherwise
800 private static boolean looksLikeMethodDecl(IDocument document, int position, String partitioning) {
803 position= eatIdentToLeft(document, position, partitioning);
807 position= eatBrackets(document, position - 1, partitioning);
811 position= eatIdentToLeft(document, position - 1, partitioning);
813 return position != -1;
817 * From <code>position</code> to the left, eats any whitespace and then a pair of brackets
818 * as used to declare an array return type like <pre>String [ ]</pre>.
819 * The return value is either the position of the opening bracket or <code>position</code> if no
820 * pair of brackets can be parsed.
822 * @param document the document being modified
823 * @param position the first character position in <code>document</code> to be considered
824 * @param partitioning the document partitioning
825 * @return the smallest character position of bracket pair or <code>position</code>
827 private static int eatBrackets(IDocument document, int position, String partitioning) {
828 // accept array return type
829 int pos= firstNonWhitespaceBackward(document, position, partitioning, -1);
831 if (pos > 1 && document.getChar(pos) == ']') {
832 pos= firstNonWhitespaceBackward(document, pos - 1, partitioning, -1);
833 if (pos > 0 && document.getChar(pos) == '[')
836 } catch (BadLocationException e) {
843 * From <code>position</code> to the left, eats any whitespace and the first identifier, returning
844 * the position of the first identifier character (in normal read order).
845 * <p>When called on a document with content <code>" some string "</code> and positionition 13, the
846 * return value will be 6 (the first letter in <code>string</code>).
849 * @param document the document being modified
850 * @param position the first character position in <code>document</code> to be considered
851 * @param partitioning the document partitioning
852 * @return the smallest character position of an identifier or -1 if none can be found; always <= <code>position</code>
854 private static int eatIdentToLeft(IDocument document, int position, String partitioning) {
857 Assert.isTrue(position < document.getLength());
859 int p= firstNonWhitespaceBackward(document, position, partitioning, -1);
866 char ch= document.getChar(p);
867 if (Character.isJavaIdentifierPart(ch)) {
872 // length must be > 0
873 if (Character.isWhitespace(ch) && p != position)
880 // start of document reached
883 } catch (BadLocationException e) {
889 * Returns a position in the first java partition after the last non-empty and non-comment partition.
890 * There is no non-whitespace from the returned position to the end of the partition it is contained in.
892 * @param document the document being modified
893 * @param line the line under investigation
894 * @param offset the caret offset into <code>line</code>
895 * @param partitioning the document partitioning
896 * @return the position of the next Java partition, or the end of <code>line</code>
898 private static int nextPartitionOrLineEnd(IDocument document, ITextSelection line, int offset, String partitioning) {
899 // run relative to document
900 final int docOffset= offset + line.getOffset();
901 final int eol= line.getOffset() + line.getLength();
902 int nextPartitionPos= eol; // init with line end
903 int validPosition= docOffset;
906 ITypedRegion partition= TextUtilities.getPartition(document, partitioning, nextPartitionPos, true);
907 validPosition= getValidPositionForPartition(document, partition, eol);
908 while (validPosition == -1) {
909 nextPartitionPos= partition.getOffset() - 1;
910 if (nextPartitionPos < docOffset) {
911 validPosition= docOffset;
914 partition= TextUtilities.getPartition(document, partitioning, nextPartitionPos, false);
915 validPosition= getValidPositionForPartition(document, partition, eol);
917 } catch (BadLocationException e) {
920 validPosition= Math.max(validPosition, docOffset);
921 // make relative to line
922 validPosition -= line.getOffset();
923 return validPosition;
927 * Returns a valid insert location (except for whitespace) in <code>partition</code> or -1 if
928 * there is no valid insert location.
929 * An valid insert location is right after any java string or character partition, or at the end
930 * of a java default partition, but never behind <code>maxOffset</code>. Comment partitions or
931 * empty java partitions do never yield valid insert positions.
933 * @param doc the document being modified
934 * @param partition the current partition
935 * @param maxOffset the maximum offset to consider
936 * @return a valid insert location in <code>partition</code>, or -1 if there is no valid insert location
938 private static int getValidPositionForPartition(IDocument doc, ITypedRegion partition, int maxOffset) {
939 final int INVALID= -1;
941 if (IPHPPartitions.PHP_PHPDOC_COMMENT.equals(partition.getType()))
943 if (IPHPPartitions.PHP_MULTILINE_COMMENT.equals(partition.getType()))
945 if (IPHPPartitions.PHP_SINGLELINE_COMMENT.equals(partition.getType()))
948 int endOffset= Math.min(maxOffset, partition.getOffset() + partition.getLength());
950 // if (IPHPPartitions.JAVA_CHARACTER.equals(partition.getType()))
952 if (IPHPPartitions.PHP_STRING_DQ.equals(partition.getType()))
954 if (IPHPPartitions.PHP_STRING_SQ.equals(partition.getType()))
956 if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
958 if (doc.get(partition.getOffset(), endOffset - partition.getOffset()).trim().length() == 0)
962 } catch (BadLocationException e) {
966 // default: we don't know anything about the partition - assume valid
971 * Determines whether the current line contains a for statement.
972 * Algorithm: any "for" word in the line is a positive, "for" contained in a string literal will
973 * produce a false positive.
975 * @param line the line where the change is being made
976 * @param offset the position of the caret
977 * @return <code>true</code> if <code>line</code> contains <code>for</code>, <code>false</code> otherwise
979 private static boolean isForStatement(String line, int offset) {
980 /* searching for (^|\s)for(\s|$) */
981 int forPos= line.indexOf("for"); //$NON-NLS-1$
983 if ((forPos == 0 || !Character.isJavaIdentifierPart(line.charAt(forPos - 1))) && (line.length() == forPos + 3 || !Character.isJavaIdentifierPart(line.charAt(forPos + 3))))
990 * Returns the position in <code>text</code> after which there comes only whitespace, up to
991 * <code>offset</code>.
993 * @param text the text being searched
994 * @param offset the maximum offset to search for
995 * @return the smallest value <code>v</code> such that <code>text.substring(v, offset).trim() == 0</code>
997 private static int startOfWhitespaceBeforeOffset(String text, int offset) {
998 int i= Math.min(offset, text.length());
999 for (; i >= 1; i--) {
1000 if (!Character.isWhitespace(text.charAt(i - 1)))