KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > fragments > Util


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

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 /**
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(SourceRange selection, SourceRange nodes, IBuffer buffer) {
31         if(!selection.covers(nodes))
32             return false;
33
34         //TODO: skip leading comments. Consider that leading line comment must be followed by newline!
35
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 JavaDoc 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