1 /*******************************************************************************
2 * Copyright (c) 2000, 2009 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.dom.fragments;
13 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.jdt.core.IBuffer;
16 import org.eclipse.jdt.core.ISourceRange;
17 import org.eclipse.jdt.core.ToolFactory;
18 import org.eclipse.jdt.core.compiler.IScanner;
19 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
20 import org.eclipse.jdt.core.compiler.InvalidInputException;
21 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.internal.corext.SourceRangeFactory;
27 * This class houses a collection of static methods which do not refer to,
28 * or otherwise depend on, other classes in this package. Each
29 * package-visible method is called by more than one other class in this
30 * package. Since they do not depend on other classes in this package,
31 * they could be moved to some less specialized package.
34 static boolean rangeIncludesNonWhitespaceOutsideRange(ISourceRange selection, ISourceRange nodes, IBuffer buffer) {
35 if (!covers(selection, nodes))
38 //TODO: skip leading comments. Consider that leading line comment must be followed by newline!
39 if(!isJustWhitespace(selection.getOffset(), nodes.getOffset(), buffer))
41 if(!isJustWhitespaceOrComment(nodes.getOffset() + nodes.getLength(), selection.getOffset() + selection.getLength(), buffer))
45 private static boolean isJustWhitespace(int start, int end, IBuffer buffer) {
48 Assert.isTrue(start <= end);
49 return 0 == buffer.getText(start, end - start).trim().length();
51 private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) {
54 Assert.isTrue(start <= end);
55 String trimmedText= buffer.getText(start, end - start).trim();
56 if (0 == trimmedText.length()) {
59 IScanner scanner= ToolFactory.createScanner(false, false, false, null);
60 scanner.setSource(trimmedText.toCharArray());
62 return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF;
63 } catch (InvalidInputException e) {
69 public static int getEndExclusive(ISourceRange sourceRange) {
70 return sourceRange.getOffset() + sourceRange.getLength();
73 public static int getEndInclusive(ISourceRange sourceRange) {
74 return getEndExclusive(sourceRange) - 1;
77 public static boolean covers(ISourceRange sourceRange, ASTNode astNode) {
78 return covers(sourceRange, SourceRangeFactory.create(astNode));
81 public static boolean covers(ISourceRange thisRange, ISourceRange otherRange) {
82 return thisRange.getOffset() <= otherRange.getOffset()
83 && getEndInclusive(thisRange) >= getEndInclusive(otherRange);