]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core extension/org/eclipse/jdt/internal/corext/dom/fragments/Util.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core extension / org / eclipse / jdt / internal / corext / dom / fragments / Util.java
CommitLineData
1b2798f6
EK
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
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.dom.fragments;
12
13import org.eclipse.core.runtime.Assert;
14
15import org.eclipse.jdt.core.IBuffer;
16import org.eclipse.jdt.core.ISourceRange;
17import org.eclipse.jdt.core.ToolFactory;
18import org.eclipse.jdt.core.compiler.IScanner;
19import org.eclipse.jdt.core.compiler.ITerminalSymbols;
20import org.eclipse.jdt.core.compiler.InvalidInputException;
21import org.eclipse.jdt.core.dom.ASTNode;
22
23import org.eclipse.jdt.internal.corext.SourceRangeFactory;
24
25
26/**
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.
32 */
33class Util {
34 static boolean rangeIncludesNonWhitespaceOutsideRange(ISourceRange selection, ISourceRange nodes, IBuffer buffer) {
35 if (!covers(selection, nodes))
36 return false;
37
38 //TODO: skip leading comments. Consider that leading line comment must be followed by newline!
39 if(!isJustWhitespace(selection.getOffset(), nodes.getOffset(), buffer))
40 return true;
41 if(!isJustWhitespaceOrComment(nodes.getOffset() + nodes.getLength(), selection.getOffset() + selection.getLength(), buffer))
42 return true;
43 return false;
44 }
45 private static boolean isJustWhitespace(int start, int end, IBuffer buffer) {
46 if (start == end)
47 return true;
48 Assert.isTrue(start <= end);
49 return 0 == buffer.getText(start, end - start).trim().length();
50 }
51 private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) {
52 if (start == end)
53 return true;
54 Assert.isTrue(start <= end);
55 String trimmedText= buffer.getText(start, end - start).trim();
56 if (0 == trimmedText.length()) {
57 return true;
58 } else {
59 IScanner scanner= ToolFactory.createScanner(false, false, false, null);
60 scanner.setSource(trimmedText.toCharArray());
61 try {
62 return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF;
63 } catch (InvalidInputException e) {
64 return false;
65 }
66 }
67 }
68
69 public static int getEndExclusive(ISourceRange sourceRange) {
70 return sourceRange.getOffset() + sourceRange.getLength();
71 }
72
73 public static int getEndInclusive(ISourceRange sourceRange) {
74 return getEndExclusive(sourceRange) - 1;
75 }
76
77 public static boolean covers(ISourceRange sourceRange, ASTNode astNode) {
78 return covers(sourceRange, SourceRangeFactory.create(astNode));
79 }
80
81 public static boolean covers(ISourceRange thisRange, ISourceRange otherRange) {
82 return thisRange.getOffset() <= otherRange.getOffset()
83 && getEndInclusive(thisRange) >= getEndInclusive(otherRange);
84 }
85
86}