/******************************************************************************* * Copyright (c) 2017 Rogue Wave Software Inc. 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: * Rogue Wave Software Inc. - initial implementation *******************************************************************************/ package org.eclipse.php.phpunit.ui.view.actions; import org.eclipse.core.resources.IProject; import org.eclipse.dltk.core.*; import org.eclipse.php.phpunit.ui.view.PHPUnitView; import org.eclipse.ui.texteditor.ITextEditor; /** * Open a class on a PHPUnitTest method. */ public class OpenTestAction extends OpenEditorAction { private int endPosition = 0; private String functionName; private int startPosition = 0; public OpenTestAction(final String label, final PHPUnitView testRunner, final String className, final String fileName, final int lineNumber) { this(label, testRunner, className, fileName, lineNumber, null); } public OpenTestAction(final String label, final PHPUnitView testRunner, final String className, final String fileName, final int lineNumber, final String method) { super(label, testRunner, className, fileName, lineNumber); functionName = method; } @Override protected IModelElement findElement(final IProject project, final String elementName, final String fileName) throws ModelException { if (elementName == null) { final IMethod function = findFunction(project, functionName, fileName); if (function == null) { return null; } startPosition = function.getNameRange().getOffset(); endPosition = startPosition + function.getNameRange().getOffset(); return function; } final IType phpClass = findClass(project, elementName, fileName); if (phpClass == null) { return null; } ISourceRange nameRange = phpClass.getNameRange(); startPosition = nameRange.getOffset(); endPosition = startPosition + nameRange.getLength(); if (functionName == null) { return phpClass; } final IMethod function = findMethod(phpClass); if (function == null) { return phpClass; } startPosition = function.getNameRange().getOffset(); endPosition = startPosition + function.getNameRange().getLength(); return function; } IMethod findMethod(final IType phpClass) throws ModelException { final IMethod[] functions = phpClass.getMethods(); for (IMethod function : functions) { if (function.getElementName().equalsIgnoreCase(functionName)) { return function; } } return null; } @Override public boolean isEnabled() { if (functionName != null) { return findFunction(getLaunchedProject(), functionName, fFileName) != null; } if (fClassName != null) { return findClass(getLaunchedProject(), fClassName, fFileName) != null; } return false; } @Override protected void reveal(final ITextEditor textEditor) { if (endPosition > 0) { textEditor.selectAndReveal(startPosition, endPosition - startPosition); } } }