/*******************************************************************************
* Copyright (c) 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Zend Technologies
*******************************************************************************/
package org.eclipse.php.internal.ui.corext.dom.fragments;
import java.io.IOException;
import java.io.StringReader;
import org.eclipse.core.runtime.Assert;
import org.eclipse.dltk.core.ISourceRange;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.php.internal.core.documentModel.parser.Scanner;
import org.eclipse.php.internal.core.documentModel.parser.php5.PHPLexer;
/**
* This class houses a collection of static methods which do not refer to, or
* otherwise depend on, other classes in this package. Each package-visible
* method is called by more than one other class in this package. Since they do
* not depend on other classes in this package, they could be moved to some less
* specialized package.
*/
class Util {
static boolean covers(ISourceRange range, ISourceRange nodeRange) {
return range.getOffset() <= nodeRange.getOffset() && getEndInclusive(range) >= getEndInclusive(nodeRange);
}
static int getEndExclusive(ISourceRange range) {
return range.getOffset() + range.getLength();
}
static int getEndInclusive(ISourceRange range) {
return getEndExclusive(range) - 1;
}
static boolean rangeIncludesNonWhitespaceOutsideRange(ISourceRange selection, ISourceRange nodes,
IDocument document) throws BadLocationException {
if (!covers(selection, nodes))
return false;
// TODO: skip leading comments. Consider that leading line comment must
// be followed by newline!
// check the start of the nodes and the selection
if (!isJustWhitespace(selection.getOffset(), nodes.getOffset(), document))
return true;
// check the end of the nodes and the selection
if (!isJustWhitespaceOrComment(nodes.getOffset() + nodes.getLength(),
selection.getOffset() + selection.getLength(), document))
return true;
return false;
}
private static boolean isJustWhitespace(int start, int end, IDocument buffer) throws BadLocationException {
if (start == end)
return true;
Assert.isTrue(start <= end);
return 0 == buffer.get(start, end - start).trim().length();
}
private static boolean isJustWhitespaceOrComment(int start, int end, IDocument document) {
if (start == end)
return true;
Assert.isTrue(start <= end);
// gets the new text from the document
String trimmedText;
try {
trimmedText = document.get(start, end - start).trim();
} catch (BadLocationException e1) {
return false;
}
// if there are no tokens in the trimmed text return true
if (0 == trimmedText.length()) {
return true;
} else {
Scanner scanner = new PHPLexer(new StringReader(trimmedText));
scanner.initialize(PHPLexer.ST_PHP_IN_SCRIPTING);
try {
return scanner.yylex() == null;
} catch (IOException e) {
return false;
}
}
}
}