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
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
15 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextDoubleClickStrategy;
20 import org.eclipse.jface.text.ITextViewer;
23 * Double click strategy aware of PHP identifier syntax rules.
25 public class PHPDoubleClickSelector implements ITextDoubleClickStrategy {
27 protected ITextViewer fText;
29 protected int fStartPos;
30 protected int fEndPos;
32 protected static char[] fgBrackets = { '{', '}', '(', ')', '[', ']', '"', '"' };
35 * Create a PHPDoubleClickSelector.
37 public PHPDoubleClickSelector() {
42 * Method declared on ITextDoubleClickStrategy
44 public void doubleClicked(ITextViewer text) {
46 fPos = text.getSelectedRange().x;
53 if (!selectBracketBlock())
58 * Match the brackets at the current selection. Return true if successful,
61 protected boolean matchBracketsAt() {
63 char prevChar, nextChar;
66 int bracketIndex1 = fgBrackets.length;
67 int bracketIndex2 = fgBrackets.length;
72 // get the chars preceding and following the start position
75 IDocument doc = fText.getDocument();
77 prevChar = doc.getChar(fPos - 1);
78 nextChar = doc.getChar(fPos);
80 // is the char either an open or close bracket?
81 for (i = 0; i < fgBrackets.length; i = i + 2) {
82 if (prevChar == fgBrackets[i]) {
87 for (i = 1; i < fgBrackets.length; i = i + 2) {
88 if (nextChar == fgBrackets[i]) {
94 if (fStartPos > -1 && bracketIndex1 < bracketIndex2) {
95 fEndPos = searchForClosingBracket(fStartPos, prevChar, fgBrackets[bracketIndex1 + 1], doc);
100 } else if (fEndPos > -1) {
101 fStartPos = searchForOpenBracket(fEndPos, fgBrackets[bracketIndex2 - 1], nextChar, doc);
108 } catch (BadLocationException x) {
115 * Select the word at the current selection. Return true if successful,
118 protected boolean matchWord() {
120 IDocument doc = fText.getDocument();
128 c = doc.getChar(pos);
129 if (!Scanner.isPHPIdentifierPart(c) && (c != '$')) {
138 int length = doc.getLength();
140 while (pos < length) {
141 c = doc.getChar(pos);
142 if (!Scanner.isPHPIdentifierPart(c) && (c != '$'))
151 } catch (BadLocationException x) {
158 * Returns the position of the closing bracket after startPosition.
159 * @returns the location of the closing bracket.
160 * @param startPosition - the beginning position
161 * @param openBracket - the character that represents the open bracket
162 * @param closeBracket - the character that represents the close bracket
163 * @param document - the document being searched
165 protected int searchForClosingBracket(int startPosition, char openBracket, char closeBracket, IDocument document)
166 throws BadLocationException {
168 int closePosition = startPosition + 1;
169 int length = document.getLength();
172 while (closePosition < length && stack > 0) {
173 nextChar = document.getChar(closePosition);
174 if (nextChar == openBracket && nextChar != closeBracket)
176 else if (nextChar == closeBracket)
182 return closePosition - 1;
189 * Returns the position of the open bracket before startPosition.
190 * @returns the location of the starting bracket.
191 * @param startPosition - the beginning position
192 * @param openBracket - the character that represents the open bracket
193 * @param closeBracket - the character that represents the close bracket
194 * @param document - the document being searched
196 protected int searchForOpenBracket(int startPosition, char openBracket, char closeBracket, IDocument document)
197 throws BadLocationException {
199 int openPos = startPosition - 1;
202 while (openPos >= 0 && stack > 0) {
203 nextChar = document.getChar(openPos);
204 if (nextChar == closeBracket && nextChar != openBracket)
206 else if (nextChar == openBracket)
218 * Select the area between the selected bracket and the closing bracket. Return
219 * true if successful.
221 protected boolean selectBracketBlock() {
222 if (matchBracketsAt()) {
224 if (fStartPos == fEndPos)
225 fText.setSelectedRange(fStartPos, 0);
227 fText.setSelectedRange(fStartPos + 1, fEndPos - fStartPos - 1);
235 * Select the word at the current selection.
237 protected void selectWord() {
240 if (fStartPos == fEndPos)
241 fText.setSelectedRange(fStartPos, 0);
243 fText.setSelectedRange(fStartPos + 1, fEndPos - fStartPos - 1);