1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. 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 implementation
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.ITextDoubleClickStrategy;
19 import org.eclipse.jface.text.ITextViewer;
22 * Double click strategy aware of PHP identifier syntax rules.
24 public class PHPDoubleClickSelector implements ITextDoubleClickStrategy {
26 protected ITextViewer fText;
30 protected int fStartPos;
32 protected int fEndPos;
34 protected static char[] fgBrackets = { '{', '}', '(', ')', '[', ']', '"',
38 * Create a PHPDoubleClickSelector.
40 public PHPDoubleClickSelector() {
45 * (non-Javadoc) Method declared on ITextDoubleClickStrategy
47 public void doubleClicked(ITextViewer text) {
49 fPos = text.getSelectedRange().x;
56 if (!selectBracketBlock())
61 * Match the brackets at the current selection. Return true if successful,
64 protected boolean matchBracketsAt() {
66 char prevChar, nextChar;
69 int bracketIndex1 = fgBrackets.length;
70 int bracketIndex2 = fgBrackets.length;
75 // get the chars preceding and following the start position
78 IDocument doc = fText.getDocument();
80 prevChar = doc.getChar(fPos - 1);
81 nextChar = doc.getChar(fPos);
83 // is the char either an open or close bracket?
84 for (i = 0; i < fgBrackets.length; i = i + 2) {
85 if (prevChar == fgBrackets[i]) {
90 for (i = 1; i < fgBrackets.length; i = i + 2) {
91 if (nextChar == fgBrackets[i]) {
97 if (fStartPos > -1 && bracketIndex1 < bracketIndex2) {
98 fEndPos = searchForClosingBracket(fStartPos, prevChar,
99 fgBrackets[bracketIndex1 + 1], doc);
104 } else if (fEndPos > -1) {
105 fStartPos = searchForOpenBracket(fEndPos,
106 fgBrackets[bracketIndex2 - 1], nextChar, doc);
113 } catch (BadLocationException x) {
120 * Select the word at the current selection. Return true if successful,
123 protected boolean matchWord() {
125 IDocument doc = fText.getDocument();
133 c = doc.getChar(pos);
134 if (!Scanner.isPHPIdentifierPart(c) && (c != '$')) {
143 int length = doc.getLength();
145 while (pos < length) {
146 c = doc.getChar(pos);
147 if (!Scanner.isPHPIdentifierPart(c) && (c != '$'))
156 } catch (BadLocationException x) {
163 * Returns the position of the closing bracket after startPosition.
165 * @returns the location of the closing bracket.
166 * @param startPosition -
167 * the beginning position
168 * @param openBracket -
169 * the character that represents the open bracket
170 * @param closeBracket -
171 * the character that represents the close bracket
173 * the document being searched
175 protected int searchForClosingBracket(int startPosition, char openBracket,
176 char closeBracket, IDocument document) throws BadLocationException {
178 int closePosition = startPosition + 1;
179 int length = document.getLength();
182 while (closePosition < length && stack > 0) {
183 nextChar = document.getChar(closePosition);
184 if (nextChar == openBracket && nextChar != closeBracket)
186 else if (nextChar == closeBracket)
192 return closePosition - 1;
199 * Returns the position of the open bracket before startPosition.
201 * @returns the location of the starting bracket.
202 * @param startPosition -
203 * the beginning position
204 * @param openBracket -
205 * the character that represents the open bracket
206 * @param closeBracket -
207 * the character that represents the close bracket
209 * the document being searched
211 protected int searchForOpenBracket(int startPosition, char openBracket,
212 char closeBracket, IDocument document) throws BadLocationException {
214 int openPos = startPosition - 1;
217 while (openPos >= 0 && stack > 0) {
218 nextChar = document.getChar(openPos);
219 if (nextChar == closeBracket && nextChar != openBracket)
221 else if (nextChar == openBracket)
233 * Select the area between the selected bracket and the closing bracket.
234 * Return true if successful.
236 protected boolean selectBracketBlock() {
237 if (matchBracketsAt()) {
239 if (fStartPos == fEndPos)
240 fText.setSelectedRange(fStartPos, 0);
242 fText.setSelectedRange(fStartPos + 1, fEndPos - fStartPos - 1);
250 * Select the word at the current selection.
252 protected void selectWord() {
255 if (fStartPos == fEndPos)
256 fText.setSelectedRange(fStartPos, 0);
258 fText.setSelectedRange(fStartPos + 1, fEndPos - fStartPos - 1);