use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification in project xtext-xtend by eclipse.
the class CreateMemberQuickfixes method newLocalVariableQuickfix.
protected void newLocalVariableQuickfix(final String variableName, XAbstractFeatureCall call, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
LightweightTypeReference variableType = getNewMemberType(call);
final StringBuilderBasedAppendable localVarDescriptionBuilder = new StringBuilderBasedAppendable();
localVarDescriptionBuilder.append("...").newLine();
final String defaultValueLiteral = getDefaultValueLiteral(variableType);
localVarDescriptionBuilder.append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
localVarDescriptionBuilder.newLine().append("...");
issueResolutionAcceptor.accept(issue, "Create local variable '" + variableName + "'", localVarDescriptionBuilder.toString(), "fix_local_var.png", new SemanticModificationWrapper(issue.getUriToProblem(), new ISemanticModification() {
@Override
public void apply(/* @Nullable */
final EObject element, /* @Nullable */
final IModificationContext context) throws Exception {
if (element != null) {
XtendMember xtendMember = EcoreUtil2.getContainerOfType(element, XtendMember.class);
if (xtendMember != null) {
int offset = getFirstOffsetOfKeyword(xtendMember, "{");
IXtextDocument xtextDocument = context.getXtextDocument();
if (offset != -1 && xtextDocument != null) {
final ReplacingAppendable appendable = appendableFactory.create(xtextDocument, (XtextResource) element.eResource(), offset, 0, new OptionalParameters() {
{
baseIndentationLevel = 1;
}
});
appendable.increaseIndentation().newLine().append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
appendable.commitChanges();
}
}
}
}
}));
}
use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method specifyTypeExplicitly.
@Fix(IssueCodes.API_TYPE_INFERENCE)
public void specifyTypeExplicitly(Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Infer type", "Infer type", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
EStructuralFeature featureAfterType = null;
JvmIdentifiableElement jvmElement = null;
if (element instanceof XtendFunction) {
XtendFunction function = (XtendFunction) element;
if (function.getCreateExtensionInfo() == null) {
featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__NAME;
} else {
featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__CREATE_EXTENSION_INFO;
}
jvmElement = associations.getDirectlyInferredOperation((XtendFunction) element);
} else if (element instanceof XtendField) {
featureAfterType = XtendPackage.Literals.XTEND_FIELD__NAME;
jvmElement = associations.getJvmField((XtendField) element);
}
if (jvmElement != null) {
LightweightTypeReference type = batchTypeResolver.resolveTypes(element).getActualType(jvmElement);
INode node = Iterables.getFirst(NodeModelUtils.findNodesForFeature(element, featureAfterType), null);
if (node == null) {
throw new IllegalStateException("Could not determine node for " + element);
}
if (type == null) {
throw new IllegalStateException("Could not determine type for " + element);
}
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), node.getOffset(), 0);
appendable.append(type);
appendable.append(" ");
appendable.commitChanges();
}
}
});
}
use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method implementAbstractMethods.
@Fix(IssueCodes.CLASS_MUST_BE_ABSTRACT)
public void implementAbstractMethods(final Issue issue, IssueResolutionAcceptor acceptor) {
doOverrideMethods(issue, acceptor, "Add unimplemented methods");
acceptor.accept(issue, "Make class abstract", "Make class abstract", "fix_indent.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
internalDoAddAbstractKeyword(element, context);
}
});
}
use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method addThrowsDeclaration.
@Fix(org.eclipse.xtext.xbase.validation.IssueCodes.UNHANDLED_EXCEPTION)
public void addThrowsDeclaration(final Issue issue, IssueResolutionAcceptor acceptor) {
if (issue.getData() != null && issue.getData().length > 0)
acceptor.accept(issue, "Add throws declaration", "Add throws declaration", "fix_indent.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
String[] issueData = issue.getData();
XtendExecutable xtendExecutable = EcoreUtil2.getContainerOfType(element, XtendExecutable.class);
XtextResource xtextResource = (XtextResource) xtendExecutable.eResource();
List<JvmType> exceptions = getExceptions(issueData, xtextResource);
if (exceptions.size() > 0) {
int insertPosition;
if (xtendExecutable.getExpression() == null) {
ICompositeNode functionNode = NodeModelUtils.findActualNodeFor(xtendExecutable);
if (functionNode == null)
throw new IllegalStateException("functionNode may not be null");
insertPosition = functionNode.getEndOffset();
} else {
ICompositeNode expressionNode = NodeModelUtils.findActualNodeFor(xtendExecutable.getExpression());
if (expressionNode == null)
throw new IllegalStateException("expressionNode may not be null");
insertPosition = expressionNode.getOffset();
}
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) xtendExecutable.eResource(), insertPosition, 0);
if (xtendExecutable.getExpression() == null)
appendable.append(" ");
EList<JvmTypeReference> thrownExceptions = xtendExecutable.getExceptions();
if (thrownExceptions.isEmpty())
appendable.append("throws ");
else
appendable.append(", ");
for (int i = 0; i < exceptions.size(); i++) {
appendable.append(exceptions.get(i));
if (i != exceptions.size() - 1) {
appendable.append(", ");
}
}
if (xtendExecutable.getExpression() != null)
appendable.append(" ");
appendable.commitChanges();
}
}
});
}
use of org.eclipse.xtext.ui.editor.model.edit.ISemanticModification in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method surroundWithTryCatch.
@Fix(org.eclipse.xtext.xbase.validation.IssueCodes.UNHANDLED_EXCEPTION)
public void surroundWithTryCatch(final Issue issue, IssueResolutionAcceptor acceptor) {
if (issue.getData() == null || issue.getData().length <= 1) {
return;
}
IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
IXtextDocument xtextDocument = modificationContext.getXtextDocument();
if (xtextDocument == null) {
return;
}
if (isJvmConstructorCall(xtextDocument, issue)) {
return;
}
acceptor.accept(issue, "Surround with try/catch block", "Surround with try/catch block", "fix_indent.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
String[] issueData = issue.getData();
URI childURI = URI.createURI(issueData[issueData.length - 1]);
XtextResource xtextResource = (XtextResource) element.eResource();
List<JvmType> exceptions = getExceptions(issueData, xtextResource);
if (exceptions.size() > 0) {
EObject childThrowingException = xtextResource.getResourceSet().getEObject(childURI, true);
XExpression toBeSurrounded = findContainerExpressionInBlockExpression(childThrowingException);
IXtextDocument xtextDocument = context.getXtextDocument();
if (toBeSurrounded != null) {
ICompositeNode toBeSurroundedNode = NodeModelUtils.findActualNodeFor(toBeSurrounded);
if (toBeSurroundedNode == null)
throw new IllegalStateException("toBeSurroundedNode may not be null");
ITextRegion toBeSurroundedRegion = toBeSurroundedNode.getTextRegion();
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) childThrowingException.eResource(), toBeSurroundedRegion.getOffset(), toBeSurroundedRegion.getLength());
appendable.append("try {").increaseIndentation().newLine().append(xtextDocument.get(toBeSurroundedRegion.getOffset(), toBeSurroundedRegion.getLength())).decreaseIndentation().newLine();
for (JvmType exceptionType : exceptions) {
appendable.append("} catch (").append(exceptionType).append(" exc) {").increaseIndentation().newLine().append("throw new RuntimeException(\"auto-generated try/catch\", exc)").decreaseIndentation().newLine();
}
appendable.append("}");
appendable.commitChanges();
}
}
}
});
}
Aggregations