]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/util/SelectionAwareSourceRangeComputer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / util / SelectionAwareSourceRangeComputer.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.util;
12
13import java.util.HashMap;
14import java.util.Map;
15
16import org.eclipse.core.runtime.CoreException;
17
18import org.eclipse.jdt.core.IBuffer;
19import org.eclipse.jdt.core.ToolFactory;
20import org.eclipse.jdt.core.compiler.IScanner;
21import org.eclipse.jdt.core.compiler.ITerminalSymbols;
22import org.eclipse.jdt.core.dom.ASTNode;
23import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
24import org.eclipse.jdt.core.formatter.IndentManipulation;
25
26import org.eclipse.jdt.internal.corext.dom.TokenScanner;
27
28public class SelectionAwareSourceRangeComputer extends TargetSourceRangeComputer {
29
30 private ASTNode[] fSelectedNodes;
31 private int fSelectionStart;
32 private int fSelectionLength;
33
34 private Map<ASTNode, SourceRange> fRanges;
35 private String fDocumentPortionToScan;
36
37 public SelectionAwareSourceRangeComputer(ASTNode[] selectedNodes, IBuffer buffer, int selectionStart, int selectionLength) {
38 fSelectedNodes= selectedNodes;
39 fSelectionStart= selectionStart;
40 fSelectionLength= selectionLength;
41 fDocumentPortionToScan= buffer.getText(fSelectionStart, fSelectionLength);
42 }
43
44 @Override
45 public SourceRange computeSourceRange(ASTNode node) {
46 try {
47 if (fRanges == null)
48 initializeRanges();
49 SourceRange result= fRanges.get(node);
50 if (result != null)
51 return result;
52 return super.computeSourceRange(node);
53 } catch (CoreException e) {
54 // fall back to standard implementation
55 fRanges= new HashMap<ASTNode, SourceRange>();
56 }
57 return super.computeSourceRange(node);
58 }
59
60 private void initializeRanges() throws CoreException {
61 fRanges= new HashMap<ASTNode, SourceRange>();
62 if (fSelectedNodes.length == 0)
63 return;
64
65 fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
66 int last= fSelectedNodes.length - 1;
67 fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));
68
69 IScanner scanner= ToolFactory.createScanner(true, false, false, false);
70 char[] source= fDocumentPortionToScan.toCharArray();
71 scanner.setSource(source);
72 fDocumentPortionToScan= null; // initializeRanges() is only called once
73
74 TokenScanner tokenizer= new TokenScanner(scanner);
75 int pos= tokenizer.getNextStartOffset(0, false);
76
77 ASTNode currentNode= fSelectedNodes[0];
78 int newStart= Math.min(fSelectionStart + pos, currentNode.getStartPosition());
79 SourceRange range= fRanges.get(currentNode);
80 fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));
81
82 currentNode= fSelectedNodes[last];
83 int scannerStart= currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
84 tokenizer.setOffset(scannerStart);
85 pos= scannerStart;
86 int token= -1;
87 try {
88 while (true) {
89 token= tokenizer.readNext(false);
90 pos= tokenizer.getCurrentEndOffset();
91 }
92 } catch (CoreException e) {
93 }
94 if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
95 int index= pos - 1;
96 while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
97 pos--;
98 index--;
99 }
100 }
101
102 int newEnd= Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
103 range= fRanges.get(currentNode);
104 fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));
105 }
106}