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 org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.ITextDoubleClickStrategy;
18 import org.eclipse.jface.text.ITextViewer;
21 * Double click strategy aware of PHP identifier syntax rules.
23 public class PHPDoubleClickSelector implements ITextDoubleClickStrategy {
25 protected ITextViewer fText;
27 protected int fStartPos;
28 protected int fEndPos;
30 protected static char[] fgBrackets = { '{', '}', '(', ')', '[', ']', '"', '"' };
33 * Create a JavaDoubleClickSelector.
35 public PHPDoubleClickSelector() {
40 * Method declared on ITextDoubleClickStrategy
42 public void doubleClicked(ITextViewer text) {
44 fPos = text.getSelectedRange().x;
51 if (!selectBracketBlock())
56 * Match the brackets at the current selection. Return true if successful,
59 protected boolean matchBracketsAt() {
61 char prevChar, nextChar;
64 int bracketIndex1 = fgBrackets.length;
65 int bracketIndex2 = fgBrackets.length;
70 // get the chars preceding and following the start position
73 IDocument doc = fText.getDocument();
75 prevChar = doc.getChar(fPos - 1);
76 nextChar = doc.getChar(fPos);
78 // is the char either an open or close bracket?
79 for (i = 0; i < fgBrackets.length; i = i + 2) {
80 if (prevChar == fgBrackets[i]) {
85 for (i = 1; i < fgBrackets.length; i = i + 2) {
86 if (nextChar == fgBrackets[i]) {
92 if (fStartPos > -1 && bracketIndex1 < bracketIndex2) {
93 fEndPos = searchForClosingBracket(fStartPos, prevChar, fgBrackets[bracketIndex1 + 1], doc);
98 } else if (fEndPos > -1) {
99 fStartPos = searchForOpenBracket(fEndPos, fgBrackets[bracketIndex2 - 1], nextChar, doc);
106 } catch (BadLocationException x) {
113 * Select the word at the current selection. Return true if successful,
116 protected boolean matchWord() {
118 IDocument doc = fText.getDocument();
126 c = doc.getChar(pos);
127 if (!Character.isJavaIdentifierPart(c) && (c != '$')) {
136 int length = doc.getLength();
138 while (pos < length) {
139 c = doc.getChar(pos);
140 if (!Character.isJavaIdentifierPart(c) && (c != '$'))
149 } catch (BadLocationException x) {
156 * Returns the position of the closing bracket after startPosition.
157 * @returns the location of the closing bracket.
158 * @param startPosition - the beginning position
159 * @param openBracket - the character that represents the open bracket
160 * @param closeBracket - the character that represents the close bracket
161 * @param document - the document being searched
163 protected int searchForClosingBracket(int startPosition, char openBracket, char closeBracket, IDocument document)
164 throws BadLocationException {
166 int closePosition = startPosition + 1;
167 int length = document.getLength();
170 while (closePosition < length && stack > 0) {
171 nextChar = document.getChar(closePosition);
172 if (nextChar == openBracket && nextChar != closeBracket)
174 else if (nextChar == closeBracket)
180 return closePosition - 1;
187 * Returns the position of the open bracket before startPosition.
188 * @returns the location of the starting bracket.
189 * @param startPosition - the beginning position
190 * @param openBracket - the character that represents the open bracket
191 * @param closeBracket - the character that represents the close bracket
192 * @param document - the document being searched
194 protected int searchForOpenBracket(int startPosition, char openBracket, char closeBracket, IDocument document)
195 throws BadLocationException {
197 int openPos = startPosition - 1;
200 while (openPos >= 0 && stack > 0) {
201 nextChar = document.getChar(openPos);
202 if (nextChar == closeBracket && nextChar != openBracket)
204 else if (nextChar == openBracket)
216 * Select the area between the selected bracket and the closing bracket. Return
217 * true if successful.
219 protected boolean selectBracketBlock() {
220 if (matchBracketsAt()) {
222 if (fStartPos == fEndPos)
223 fText.setSelectedRange(fStartPos, 0);
225 fText.setSelectedRange(fStartPos + 1, fEndPos - fStartPos - 1);
233 * Select the word at the current selection.
235 protected void selectWord() {
238 if (fStartPos == fEndPos)
239 fText.setSelectedRange(fStartPos, 0);
241 fText.setSelectedRange(fStartPos + 1, fEndPos - fStartPos - 1);