KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > codeassist > select > SelectionScanner


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.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 org.eclipse.jdt.internal.compiler.parser.Scanner;
20
21 public class SelectionScanner extends Scanner {
22
23     public char[] selectionIdentifier;
24     public int selectionStart, selectionEnd;
25 /*
26  * Truncate the current identifier if it is containing the cursor location. Since completion is performed
27  * on an identifier prefix.
28  *
29  */

30  
31 public SelectionScanner(long sourceLevel) {
32     super(false /*comment*/, false /*whitespace*/, false /*nls*/, sourceLevel, null /*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/);
33 }
34
35 public char[] getCurrentIdentifierSource() {
36
37     if (selectionIdentifier == null){
38         if (selectionStart == startPosition && selectionEnd == currentPosition-1){
39             if (withoutUnicodePtr != 0){ // check unicode scenario
40
System.arraycopy(withoutUnicodeBuffer, 1, selectionIdentifier = new char[withoutUnicodePtr], 0, withoutUnicodePtr);
41             } else {
42                 int length = currentPosition - startPosition;
43                 // no char[] sharing around completionIdentifier, we want it to be unique so as to use identity checks
44
System.arraycopy(source, startPosition, (selectionIdentifier = new char[length]), 0, length);
45             }
46             return selectionIdentifier;
47         }
48     }
49     return super.getCurrentIdentifierSource();
50 }
51 /*
52  * In case we actually read a keyword which corresponds to the selected
53  * range, we pretend we read an identifier.
54  */

55 public int scanIdentifierOrKeyword() {
56
57     int id = super.scanIdentifierOrKeyword();
58
59     // convert completed keyword into an identifier
60
if (id != TokenNameIdentifier
61         && startPosition == selectionStart
62         && currentPosition == selectionEnd+1){
63         return TokenNameIdentifier;
64     }
65     return id;
66 }
67 }
68
Popular Tags