KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > SelectionAwareSourceRangeComputer


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.corext.refactoring.util;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20
21 import org.eclipse.jdt.core.ToolFactory;
22 import org.eclipse.jdt.core.compiler.IScanner;
23 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
24 import org.eclipse.jdt.core.dom.ASTNode;
25 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
26 import org.eclipse.jdt.core.formatter.IndentManipulation;
27
28 import org.eclipse.jdt.internal.corext.dom.TokenScanner;
29
30 public class SelectionAwareSourceRangeComputer extends TargetSourceRangeComputer {
31
32     private ASTNode[] fSelectedNodes;
33     private IDocument fDocument;
34     private int fSelectionStart;
35     private int fSelectionLength;
36     
37     private Map JavaDoc/*<ASTNode, SourceRange>*/ fRanges;
38     
39     public SelectionAwareSourceRangeComputer(ASTNode[] selectedNodes, IDocument document, int selectionStart, int selectionLength) throws BadLocationException, CoreException {
40         fSelectedNodes= selectedNodes;
41         fDocument= document;
42         fSelectionStart= selectionStart;
43         fSelectionLength= selectionLength;
44     }
45     
46     public SourceRange computeSourceRange(ASTNode node) {
47         try {
48             if (fRanges == null)
49                 initializeRanges();
50             SourceRange result= (SourceRange)fRanges.get(node);
51             if (result != null)
52                 return result;
53             return super.computeSourceRange(node);
54         } catch (BadLocationException e) {
55             // fall back to standard implementation
56
fRanges= new HashMap JavaDoc();
57         } catch (CoreException e) {
58             // fall back to standard implementation
59
fRanges= new HashMap JavaDoc();
60         }
61         return super.computeSourceRange(node);
62     }
63
64     private void initializeRanges() throws BadLocationException, CoreException {
65         fRanges= new HashMap JavaDoc();
66         if (fSelectedNodes.length == 0)
67             return;
68         
69         fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
70         int last= fSelectedNodes.length - 1;
71         fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));
72         
73         IScanner scanner= ToolFactory.createScanner(true, false, false, false);
74         String JavaDoc documentPortionToScan= fDocument.get(fSelectionStart, fSelectionLength);
75         scanner.setSource(documentPortionToScan.toCharArray());
76         TokenScanner tokenizer= new TokenScanner(scanner);
77         int pos= tokenizer.getNextStartOffset(0, false);
78         
79         ASTNode currentNode= fSelectedNodes[0];
80         int newStart= Math.min(fSelectionStart + pos, currentNode.getStartPosition());
81         SourceRange range= (SourceRange)fRanges.get(currentNode);
82         fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));
83         
84         currentNode= fSelectedNodes[last];
85         int scannerStart= currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
86         tokenizer.setOffset(scannerStart);
87         pos= scannerStart;
88         int token= -1;
89         try {
90             while (true) {
91                 token= tokenizer.readNext(false);
92                 pos= tokenizer.getCurrentEndOffset();
93             }
94         } catch (CoreException e) {
95         }
96         if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
97             int index= pos - 1;
98             while(index >= 0 && IndentManipulation.isLineDelimiterChar(documentPortionToScan.charAt(index))) {
99                 pos--;
100                 index--;
101             }
102         }
103         
104         int newEnd= Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
105         range= (SourceRange)fRanges.get(currentNode);
106         fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));
107     }
108 }
109
Popular Tags