01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.dom.fragments;
11:
12: import org.eclipse.core.runtime.Assert;
13:
14: import org.eclipse.jdt.core.IBuffer;
15: import org.eclipse.jdt.core.ToolFactory;
16: import org.eclipse.jdt.core.compiler.IScanner;
17: import org.eclipse.jdt.core.compiler.ITerminalSymbols;
18: import org.eclipse.jdt.core.compiler.InvalidInputException;
19:
20: import org.eclipse.jdt.internal.corext.SourceRange;
21:
22: /**
23: * This class houses a collection of static methods which do not refer to,
24: * or otherwise depend on, other classes in this package. Each
25: * package-visible method is called by more than one other class in this
26: * package. Since they do not depend on other classes in this package,
27: * they could be moved to some less specialized package.
28: */
29: class Util {
30: static boolean rangeIncludesNonWhitespaceOutsideRange(
31: SourceRange selection, SourceRange nodes, IBuffer buffer) {
32: if (!selection.covers(nodes))
33: return false;
34:
35: //TODO: skip leading comments. Consider that leading line comment must be followed by newline!
36: if (!isJustWhitespace(selection.getOffset(), nodes.getOffset(),
37: buffer))
38: return true;
39: if (!isJustWhitespaceOrComment(nodes.getOffset()
40: + nodes.getLength(), selection.getOffset()
41: + selection.getLength(), buffer))
42: return true;
43: return false;
44: }
45:
46: private static boolean isJustWhitespace(int start, int end,
47: IBuffer buffer) {
48: if (start == end)
49: return true;
50: Assert.isTrue(start <= end);
51: return 0 == buffer.getText(start, end - start).trim().length();
52: }
53:
54: private static boolean isJustWhitespaceOrComment(int start,
55: int end, IBuffer buffer) {
56: if (start == end)
57: return true;
58: Assert.isTrue(start <= end);
59: String trimmedText = buffer.getText(start, end - start).trim();
60: if (0 == trimmedText.length()) {
61: return true;
62: } else {
63: IScanner scanner = ToolFactory.createScanner(false, false,
64: false, null);
65: scanner.setSource(trimmedText.toCharArray());
66: try {
67: return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF;
68: } catch (InvalidInputException e) {
69: return false;
70: }
71: }
72: }
73: }
|