Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
refactor AbstractToolOperation and related.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Feb 12, 2016
1 parent f6e4625 commit d874798
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 27 deletions.
Expand Up @@ -114,7 +114,7 @@ public void notifyMessage(String msgId, StatusLevel statusLevel, String title, S

/* ----------------- ----------------- */

protected EclipseCancelMonitor cm(IProgressMonitor pm) {
public static EclipseCancelMonitor cm(IProgressMonitor pm) {
return new EclipseCancelMonitor(pm);
}

Expand Down Expand Up @@ -213,39 +213,42 @@ protected void handleProcessStartResult(ProcessStartHelper psh) {

/* ----------------- ----------------- */

public ExternalProcessResult runEngineTool(ProcessBuilder pb, String clientInput, IProgressMonitor pm)
public ExternalProcessResult runEngineTool(ProcessBuilder pb, String processInput, IProgressMonitor pm)
throws CoreException, OperationCancellation {
try {
return runEngineTool(pb, clientInput, cm(pm));
return runEngineTool(pb, processInput, cm(pm));
} catch(CommonException ce) {
throw LangCore.createCoreException(ce);
}
}

public ExternalProcessResult runEngineTool(ProcessBuilder pb, String clientInput, ICancelMonitor cm)
public ExternalProcessResult runEngineTool(ProcessBuilder pb, String processInput, ICancelMonitor cm)
throws CommonException, OperationCancellation {
IOperationConsoleHandler handler = startNewOperation(ProcessStartKind.ENGINE_TOOLS, false, false);
return new RunToolTask(handler, pb, cm).runProcess(clientInput);
return new RunToolTask(handler, pb, cm).runProcess(processInput);
}

/* ----------------- ----------------- */

/**
* Helper to start engine client processes in the tool manager.
*/
public class ToolManagerEngineToolRunner implements IOperationService {
public class ToolManagerEngineToolRunner2 implements IOperationService {

protected final boolean throwOnNonZeroStatus;
protected final EclipseCancelMonitor cm;

public ToolManagerEngineToolRunner(IProgressMonitor monitor, boolean throwOnNonZeroStatus) {
public ToolManagerEngineToolRunner2() {
this(false);
}

@Deprecated
protected ToolManagerEngineToolRunner2(boolean throwOnNonZeroStatus) {
this.throwOnNonZeroStatus = throwOnNonZeroStatus;
this.cm = new EclipseCancelMonitor(monitor);
}

@Override
public ExternalProcessResult runProcess(ProcessBuilder pb, String input) throws CommonException,
OperationCancellation {
public ExternalProcessResult runProcess(ProcessBuilder pb, String input, ICancelMonitor cm)
throws CommonException, OperationCancellation {
IOperationConsoleHandler handler = startNewOperation(ProcessStartKind.ENGINE_TOOLS, false, false);
return new RunToolTask(handler, pb, cm).runProcess(input, throwOnNonZeroStatus);
}
Expand Down
Expand Up @@ -33,12 +33,16 @@
import melnorme.lang.ide.ui.utils.operations.AbstractEditorOperation2;
import melnorme.lang.tooling.ast.SourceRange;
import melnorme.lang.tooling.ops.FindDefinitionResult;
import melnorme.lang.tooling.ops.IProcessRunner;
import melnorme.lang.tooling.ops.SourceLineColumnRange;
import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;

public abstract class AbstractOpenElementOperation extends AbstractEditorOperation2<FindDefinitionResult> {
public abstract class AbstractOpenElementOperation extends AbstractEditorOperation2<FindDefinitionResult>
implements IProcessRunner {

protected final String source;
protected final SourceRange range; // range of element to open. Usually only offset matters
Expand Down Expand Up @@ -162,4 +166,12 @@ protected static int getOffsetFrom(IDocument doc, int line_oneBased, int column_
return lineOffset + column_oneBased-1;
}

/* ----------------- ----------------- */

@Override
public ExternalProcessResult runProcess(ProcessBuilder pb, String input, ICancelMonitor cm)
throws OperationCancellation, CommonException {
return LangCore.getToolManager().runEngineTool(pb, input, cm);
}

}
Expand Up @@ -12,14 +12,13 @@


import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;

import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.operations.AbstractToolManager.ToolManagerEngineToolRunner;
import melnorme.lang.ide.core.operations.AbstractToolManager.ToolManagerEngineToolRunner2;
import melnorme.lang.ide.core.utils.operation.TimeoutProgressMonitor;
import melnorme.lang.ide.ui.LangImageProvider;
import melnorme.lang.ide.ui.LangImages;
Expand Down Expand Up @@ -106,8 +105,8 @@ protected AbstractLangImageProvider getImageProvider() {

/* ----------------- ----------------- */

protected ToolManagerEngineToolRunner getEngineToolRunner(IProgressMonitor pm) {
return LangCore.getToolManager().new ToolManagerEngineToolRunner(pm, false);
protected ToolManagerEngineToolRunner2 getEngineToolRunner() {
return LangCore.getToolManager().new ToolManagerEngineToolRunner2();
}

}
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Display;

import melnorme.lang.ide.core.utils.operation.EclipseCancelMonitor;
import melnorme.lang.ide.ui.utils.UIOperationsStatusHandler;
import melnorme.lang.tooling.data.Severity;
import melnorme.utilbox.concurrency.OperationCancellation;
Expand Down Expand Up @@ -64,4 +65,8 @@ protected void dialogError(String message) {
UIOperationsStatusHandler.displayStatusMessage(operationName, Severity.ERROR, message);
}

public static EclipseCancelMonitor cm(IProgressMonitor pm) {
return new EclipseCancelMonitor(pm);
}

}
27 changes: 27 additions & 0 deletions plugin_tooling/src-lang/melnorme/lang/tooling/data/InfoResult.java
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2016 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.tooling.data;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

@SuppressWarnings("serial")
public class InfoResult extends Throwable {

public InfoResult(String message) {
super(assertNotNull(message));
}

@Override
public String getMessage() {
return super.getMessage();
}

}
Expand Up @@ -12,6 +12,7 @@

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;
Expand All @@ -25,9 +26,9 @@ public AbstractToolOperation(IOperationService opHelper) {
this.operationHelper = assertNotNull(opHelper);
}

protected ExternalProcessResult runToolProcess(ProcessBuilder pb, String input)
protected ExternalProcessResult runToolProcess(ProcessBuilder pb, String input, ICancelMonitor cm)
throws CommonException, OperationCancellation {
return operationHelper.runProcess(pb, input);
return operationHelper.runProcess(pb, input, cm);
}

public IOperationService getOperationHelper() {
Expand Down
@@ -0,0 +1,63 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.tooling.ops;


import melnorme.lang.tooling.data.InfoResult;
import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;

public abstract class AbstractToolOutputParser2<RESULT> extends ToolOutputParseHelper {

protected final String toolPath;
protected final boolean nonZeroExitIsFatal;

public AbstractToolOutputParser2(String toolPath, boolean nonZeroResultIsFatal) {
super();
this.toolPath = toolPath;
this.nonZeroExitIsFatal = nonZeroResultIsFatal;
}

public FindDefinitionResult execute(IProcessRunner opRunner,
ICancelMonitor cm) throws OperationCancellation, CommonException, InfoResult {
ProcessBuilder pb = createProcessBuilder();

ExternalProcessResult result = opRunner.runProcess(pb, null, cm);
if(result.exitValue != 0) {
handleNonZeroExitValue(result);
}

return parseToolResult(result);
}

protected abstract ProcessBuilder createProcessBuilder() throws CommonException;

@SuppressWarnings("unused")
protected void handleNonZeroExitValue( ExternalProcessResult result) throws CommonException, InfoResult {
String nonZeroExitMsg = getToolName() + " did not complete successfully.";
if(nonZeroExitIsFatal) {
throw new CommonException(nonZeroExitMsg);
} else {
throw new InfoResult(nonZeroExitMsg);
}
}

protected abstract String getToolName();

public FindDefinitionResult parseToolResult(ExternalProcessResult result) throws CommonException {
return parseToolResult(result.getStdOutBytes().toString());
}

public abstract FindDefinitionResult parseToolResult(String output) throws CommonException;

}
Expand Up @@ -10,20 +10,12 @@
*******************************************************************************/
package melnorme.lang.tooling.ops;

import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.ILogHandler;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;

/**
* Service/helper class to perform certain operation tasks (such as running a process)
* under a context that is abstracted away. (Usually it's a UI context that observes the tasks)
*/
public interface IOperationService extends ILogHandler {

/**
* Start a process from given ProcessBuilder pb, run it with given input until completion.
*/
ExternalProcessResult runProcess(ProcessBuilder pb, String input) throws CommonException, OperationCancellation;
public interface IOperationService extends ILogHandler, IProcessRunner {

}
@@ -0,0 +1,26 @@
/*******************************************************************************
* Copyright (c) 2016 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.tooling.ops;

import melnorme.utilbox.concurrency.ICancelMonitor;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;

public interface IProcessRunner {

/**
* Start a process from given ProcessBuilder pb, run it with given input until completion.
*/
ExternalProcessResult runProcess(ProcessBuilder pb, String input, ICancelMonitor cm)
throws OperationCancellation, CommonException;

}

0 comments on commit d874798

Please sign in to comment.