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

Commit

Permalink
Changed: by default non-zero exit value is hard failure for tool ops.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Nov 11, 2015
1 parent 51116ec commit ed72423
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 259 deletions.
@@ -1,113 +1,113 @@
/*******************************************************************************
* 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.ide.ui.text.completion;


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.utils.operation.TimeoutProgressMonitor;
import melnorme.lang.ide.ui.LangImageProvider;
import melnorme.lang.ide.ui.LangImages;
import melnorme.lang.ide.ui.LangUIMessages;
import melnorme.lang.ide.ui.LangUIPlugin_Actual;
import melnorme.lang.ide.ui.editor.actions.SourceOperationContext;
import melnorme.lang.ide.ui.views.AbstractLangImageProvider;
import melnorme.lang.ide.ui.views.StructureElementLabelProvider;
import melnorme.lang.tooling.ToolCompletionProposal;
import melnorme.lang.tooling.completion.LangCompletionResult;
import melnorme.lang.tooling.ops.OperationSoftFailure;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.collections.Indexable;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;

public abstract class LangCompletionProposalComputer extends AbstractCompletionProposalComputer {

@Override
protected Indexable<ICompletionProposal> doComputeCompletionProposals(SourceOperationContext context,
int offset) throws CoreException, CommonException, OperationSoftFailure {

final TimeoutProgressMonitor pm = new TimeoutProgressMonitor(5000);
try {

return computeProposals(context, offset, pm);

} catch (OperationCancellation e) {
if(pm.isCanceled()) {
throw new CommonException(LangUIMessages.ContentAssist_Timeout);
}
// This shouldn't be possible in most concrete implementations,
// as OperationCancellation should only occur when the timeout is reached.
throw new OperationSoftFailure(LangUIMessages.ContentAssist_Cancelled);
}

}

protected Indexable<ICompletionProposal> computeProposals(SourceOperationContext context, int offset,
TimeoutProgressMonitor pm)
throws CoreException, CommonException, OperationCancellation, OperationSoftFailure
{
LangCompletionResult result = doComputeProposals(context, offset, pm);
Indexable<ToolCompletionProposal> resultProposals = result.getValidatedProposals();

ArrayList2<ICompletionProposal> proposals = new ArrayList2<>();
for (ToolCompletionProposal proposal : resultProposals) {
proposals.add(adaptToolProposal(proposal));
}

return proposals;
}

protected abstract LangCompletionResult doComputeProposals(SourceOperationContext context,
int offset, TimeoutProgressMonitor pm)
throws CoreException, CommonException, OperationCancellation, OperationSoftFailure;

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

protected ICompletionProposal adaptToolProposal(ToolCompletionProposal proposal) {
IContextInformation ctxInfo = null; // TODO: context information
return new LangCompletionProposal(proposal, getImage(proposal), ctxInfo);
}

protected Image getImage(ToolCompletionProposal proposal) {
ImageDescriptor imageDescriptor = createImageDescriptor(proposal);
return LangImages.getImageDescriptorRegistry().get(imageDescriptor);
}

public ImageDescriptor createImageDescriptor(ToolCompletionProposal proposal) {
ImageDescriptor baseImage = getBaseImageDescriptor(proposal);

StructureElementLabelProvider labelDecorator = LangUIPlugin_Actual.getStructureElementLabelProvider();
return labelDecorator.getElementImageDescriptor(baseImage, proposal.getAttributes());
}

protected ImageDescriptor getBaseImageDescriptor(ToolCompletionProposal proposal) {
return getImageProvider().getImageDescriptor(proposal.getKind()).getDescriptor();
}

protected AbstractLangImageProvider getImageProvider() {
return new LangImageProvider();
}

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

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

/*******************************************************************************
* 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.ide.ui.text.completion;


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.utils.operation.TimeoutProgressMonitor;
import melnorme.lang.ide.ui.LangImageProvider;
import melnorme.lang.ide.ui.LangImages;
import melnorme.lang.ide.ui.LangUIMessages;
import melnorme.lang.ide.ui.LangUIPlugin_Actual;
import melnorme.lang.ide.ui.editor.actions.SourceOperationContext;
import melnorme.lang.ide.ui.views.AbstractLangImageProvider;
import melnorme.lang.ide.ui.views.StructureElementLabelProvider;
import melnorme.lang.tooling.ToolCompletionProposal;
import melnorme.lang.tooling.completion.LangCompletionResult;
import melnorme.lang.tooling.ops.OperationSoftFailure;
import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.collections.Indexable;
import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;

public abstract class LangCompletionProposalComputer extends AbstractCompletionProposalComputer {

@Override
protected Indexable<ICompletionProposal> doComputeCompletionProposals(SourceOperationContext context,
int offset) throws CoreException, CommonException, OperationSoftFailure {

final TimeoutProgressMonitor pm = new TimeoutProgressMonitor(5000);
try {

return computeProposals(context, offset, pm);

} catch (OperationCancellation e) {
if(pm.isCanceled()) {
throw new CommonException(LangUIMessages.ContentAssist_Timeout);
}
// This shouldn't be possible in most concrete implementations,
// as OperationCancellation should only occur when the timeout is reached.
throw new OperationSoftFailure(LangUIMessages.ContentAssist_Cancelled);
}

}

protected Indexable<ICompletionProposal> computeProposals(SourceOperationContext context, int offset,
TimeoutProgressMonitor pm)
throws CoreException, CommonException, OperationCancellation, OperationSoftFailure
{
LangCompletionResult result = doComputeProposals(context, offset, pm);
Indexable<ToolCompletionProposal> resultProposals = result.getValidatedProposals();

ArrayList2<ICompletionProposal> proposals = new ArrayList2<>();
for (ToolCompletionProposal proposal : resultProposals) {
proposals.add(adaptToolProposal(proposal));
}

return proposals;
}

protected abstract LangCompletionResult doComputeProposals(SourceOperationContext context,
int offset, TimeoutProgressMonitor pm)
throws CoreException, CommonException, OperationCancellation;

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

protected ICompletionProposal adaptToolProposal(ToolCompletionProposal proposal) {
IContextInformation ctxInfo = null; // TODO: context information
return new LangCompletionProposal(proposal, getImage(proposal), ctxInfo);
}

protected Image getImage(ToolCompletionProposal proposal) {
ImageDescriptor imageDescriptor = createImageDescriptor(proposal);
return LangImages.getImageDescriptorRegistry().get(imageDescriptor);
}

public ImageDescriptor createImageDescriptor(ToolCompletionProposal proposal) {
ImageDescriptor baseImage = getBaseImageDescriptor(proposal);

StructureElementLabelProvider labelDecorator = LangUIPlugin_Actual.getStructureElementLabelProvider();
return labelDecorator.getElementImageDescriptor(baseImage, proposal.getAttributes());
}

protected ImageDescriptor getBaseImageDescriptor(ToolCompletionProposal proposal) {
return getImageProvider().getImageDescriptor(proposal.getKind()).getDescriptor();
}

protected AbstractLangImageProvider getImageProvider() {
return new LangImageProvider();
}

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

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

}
@@ -1,54 +1,54 @@
/*******************************************************************************
* 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.completion;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import melnorme.lang.tooling.ToolCompletionProposal;
import melnorme.lang.tooling.ops.OperationSoftFailure;
import melnorme.utilbox.collections.Indexable;

public class LangCompletionResult {

protected final String errorMessage;
protected final Indexable<ToolCompletionProposal> proposals;

public LangCompletionResult(String errorMessage) {
super();
this.errorMessage = assertNotNull(errorMessage);
this.proposals = null;
}

public LangCompletionResult(Indexable<ToolCompletionProposal> proposals) {
this.errorMessage = null;
this.proposals = proposals;
}

public boolean isErrorResult() {
return errorMessage != null;
}

public String getErrorMessage() {
return errorMessage;
}

protected Indexable<ToolCompletionProposal> getProposals() {
return proposals;
}

public Indexable<ToolCompletionProposal> getValidatedProposals() throws OperationSoftFailure {
if(isErrorResult()) {
throw new OperationSoftFailure(getErrorMessage());
}

return getProposals();
}

/*******************************************************************************
* 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.completion;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import melnorme.lang.tooling.ToolCompletionProposal;
import melnorme.lang.tooling.ops.OperationSoftFailure;
import melnorme.utilbox.collections.Indexable;

public class LangCompletionResult {

protected final String errorMessage;
protected final Indexable<ToolCompletionProposal> proposals;

public LangCompletionResult(String errorMessage) {
super();
this.errorMessage = assertNotNull(errorMessage);
this.proposals = null;
}

public LangCompletionResult(Indexable<ToolCompletionProposal> proposals) {
this.errorMessage = null;
this.proposals = proposals;
}

public boolean isErrorResult() {
return errorMessage != null;
}

public String getErrorMessage() {
return errorMessage;
}

public Indexable<ToolCompletionProposal> getProposals_maybeNull() {
return proposals;
}

public Indexable<ToolCompletionProposal> getValidatedProposals() throws OperationSoftFailure {
if(isErrorResult()) {
throw new OperationSoftFailure(getErrorMessage());
}

return getProposals_maybeNull();
}

}

0 comments on commit ed72423

Please sign in to comment.