1 11 package org.eclipse.jdt.internal.corext.dom.fragments; 12 13 import org.eclipse.jdt.core.IBuffer; 14 import org.eclipse.jdt.core.ToolFactory; 15 import org.eclipse.jdt.core.compiler.IScanner; 16 import org.eclipse.jdt.core.compiler.ITerminalSymbols; 17 import org.eclipse.jdt.core.compiler.InvalidInputException; 18 19 import org.eclipse.jdt.internal.corext.Assert; 20 import org.eclipse.jdt.internal.corext.SourceRange; 21 22 29 class Util { 30 static boolean rangeIncludesNonWhitespaceOutsideRange(SourceRange selection, SourceRange nodes, IBuffer buffer) { 31 if(!selection.covers(nodes)) 32 return false; 33 34 if(!isJustWhitespace(selection.getOffset(), nodes.getOffset(), buffer)) 36 return true; 37 if(!isJustWhitespaceOrComment(nodes.getOffset() + nodes.getLength(), selection.getOffset() + selection.getLength(), buffer)) 38 return true; 39 return false; 40 } 41 private static boolean isJustWhitespace(int start, int end, IBuffer buffer) { 42 if (start == end) 43 return true; 44 Assert.isTrue(start <= end); 45 return 0 == buffer.getText(start, end - start).trim().length(); 46 } 47 private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) { 48 if (start == end) 49 return true; 50 Assert.isTrue(start <= end); 51 String trimmedText= buffer.getText(start, end - start).trim(); 52 if (0 == trimmedText.length()) { 53 return true; 54 } else { 55 IScanner scanner= ToolFactory.createScanner(false, false, false, null); 56 scanner.setSource(trimmedText.toCharArray()); 57 try { 58 return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF; 59 } catch (InvalidInputException e) { 60 return false; 61 } 62 } 63 } 64 } 65 | Popular Tags |