1 11 package org.eclipse.jdt.internal.corext.refactoring.util; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 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 fRanges= new HashMap (); 57 } catch (CoreException e) { 58 fRanges= new HashMap (); 60 } 61 return super.computeSourceRange(node); 62 } 63 64 private void initializeRanges() throws BadLocationException, CoreException { 65 fRanges= new HashMap (); 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 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 |