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
 
   9  *     IBM Corporation - initial API and implementation
 
  10  ******************************************************************************/
 
  11 package net.sourceforge.phpdt.internal.codeassist.select;
 
  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.
 
  17  * Source positions are zero-based and inclusive.
 
  19 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
 
  20 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 
  22 public class SelectionScanner extends Scanner {
 
  24         public char[] selectionIdentifier;
 
  25         public int selectionStart, selectionEnd;
 
  27  * Truncate the current identifier if it is containing the cursor location. Since completion is performed
 
  28  * on an identifier prefix.
 
  32 public SelectionScanner(boolean assertMode) {
 
  33         super(false, false, false, assertMode);
 
  36 public char[] getCurrentIdentifierSource() {
 
  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);
 
  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);
 
  47                         return selectionIdentifier;
 
  50         return super.getCurrentIdentifierSource();
 
  53  * In case we actually read a keyword which corresponds to the selected
 
  54  * range, we pretend we read an identifier.
 
  56 public int scanIdentifierOrKeyword() throws InvalidInputException {
 
  58         int id = super.scanIdentifierOrKeyword();
 
  60         // convert completed keyword into an identifier
 
  61         if (id != TokenNameIdentifier
 
  62                 && startPosition == selectionStart 
 
  63                 && currentPosition == selectionEnd+1){
 
  64                 return TokenNameIdentifier;