fafb5e785f0906ada6612491b18fbda285a3f299
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / codeassist / select / SelectionScanner.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines 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 v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.codeassist.select;
12
13 /*
14  * Scanner aware of a selection range. If finding an identifier which source range is exactly
15  * the same, then will record it so that the parser can make use of it.
16  *
17  * Source positions are zero-based and inclusive.
18  */
19 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
20 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
21
22 public class SelectionScanner extends Scanner {
23
24         public char[] selectionIdentifier;
25         public int selectionStart, selectionEnd;
26 /* 
27  * Truncate the current identifier if it is containing the cursor location. Since completion is performed
28  * on an identifier prefix.
29  *
30  */
31  
32 public SelectionScanner(boolean assertMode) {
33         super(false, false, false, assertMode);
34 }
35
36 public char[] getCurrentIdentifierSource() {
37
38         if (selectionIdentifier == null){
39                 if (selectionStart == startPosition && selectionEnd == currentPosition-1){
40                         if (withoutUnicodePtr != 0){                    // check unicode scenario
41                                 System.arraycopy(withoutUnicodeBuffer, 1, selectionIdentifier = new char[withoutUnicodePtr], 0, withoutUnicodePtr);
42                         } else {
43                                 int length = currentPosition - startPosition;
44                                 // no char[] sharing around completionIdentifier, we want it to be unique so as to use identity checks  
45                                 System.arraycopy(source, startPosition, (selectionIdentifier = new char[length]), 0, length);
46                         }
47                         return selectionIdentifier;
48                 }
49         }
50         return super.getCurrentIdentifierSource();
51 }
52 /*
53  * In case we actually read a keyword which corresponds to the selected
54  * range, we pretend we read an identifier.
55  */
56 public int scanIdentifierOrKeyword() throws InvalidInputException {
57
58         int id = super.scanIdentifierOrKeyword();
59
60         // convert completed keyword into an identifier
61         if (id != TokenNameIdentifier
62                 && startPosition == selectionStart 
63                 && currentPosition == selectionEnd+1){
64                 return TokenNameIdentifier;
65         }
66         return id;
67 }
68 }