use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable 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.xbase.ui.contentassist.ReplacingAppendable 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();
}
}
}
});
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method doOverrideMethods.
protected void doOverrideMethods(final Issue issue, IssueResolutionAcceptor acceptor, String label, final String[] operationUris) {
acceptor.accept(issue, label, label, "fix_indent.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XtendTypeDeclaration clazz = (XtendTypeDeclaration) element;
JvmGenericType inferredType = (JvmGenericType) associations.getInferredType(clazz);
ResolvedFeatures resolvedOperations = overrideHelper.getResolvedFeatures(inferredType);
IXtextDocument document = context.getXtextDocument();
final int offset = insertionOffsets.getNewMethodInsertOffset(null, clazz);
int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, (XtextResource) clazz.eResource());
final int indentationToUse = clazz.getMembers().isEmpty() ? currentIndentation + 1 : currentIndentation;
ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) clazz.eResource(), offset, 0, new OptionalParameters() {
{
ensureEmptyLinesAround = true;
baseIndentationLevel = indentationToUse;
}
});
boolean isFirst = true;
for (String operationUriAsString : operationUris) {
URI operationURI = URI.createURI(operationUriAsString);
EObject overridden = clazz.eResource().getResourceSet().getEObject(operationURI, true);
if (overridden instanceof JvmOperation) {
if (!isFirst)
appendable.newLine().newLine();
isFirst = false;
superMemberImplementor.appendOverrideFunction(clazz, resolvedOperations.getResolvedOperation((JvmOperation) overridden), appendable);
}
}
appendable.commitChanges();
}
});
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method addConstuctorFromSuper.
@Fix(IssueCodes.MISSING_CONSTRUCTOR)
public void addConstuctorFromSuper(final Issue issue, IssueResolutionAcceptor acceptor) {
if (issue.getData() != null) {
for (int i = 0; i < issue.getData().length; i += 2) {
final URI constructorURI = URI.createURI(issue.getData()[i]);
String javaSignature = issue.getData()[i + 1];
String xtendSignature = "new" + javaSignature.substring(javaSignature.indexOf('('));
acceptor.accept(issue, "Add constructor " + xtendSignature, "Add constructor " + xtendSignature, "fix_indent.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XtendClass clazz = (XtendClass) element;
JvmGenericType inferredType = associations.getInferredType(clazz);
ResolvedFeatures features = overrideHelper.getResolvedFeatures(inferredType);
ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) clazz.eResource(), insertionOffsets.getNewConstructorInsertOffset(null, clazz), 0, new OptionalParameters() {
{
ensureEmptyLinesAround = true;
baseIndentationLevel = 1;
}
});
EObject constructor = clazz.eResource().getResourceSet().getEObject(constructorURI, true);
if (constructor instanceof JvmConstructor) {
superMemberImplementor.appendConstructorFromSuper(clazz, new ResolvedConstructor((JvmConstructor) constructor, features.getType()), appendable);
}
appendable.commitChanges();
}
});
}
}
}
use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-xtend by eclipse.
the class ImplementSuperMemberAssistTest method testExistingStaticImport.
@Test
public void testExistingStaticImport() throws Exception {
ICompletionProposal[] proposals = newBuilder().append("import static java.util.Collection.*" + "class SpecialList extends java.util.ArrayList { removeAll").computeCompletionProposals();
assertEquals(1, proposals.length);
ImportOrganizingProposal proposal = (ImportOrganizingProposal) proposals[0];
ReplacingAppendable appendable = proposal.getAppendable();
RewritableImportSection importSection = appendable.getImportSection();
List<ReplaceRegion> imports = importSection.rewrite();
assertEquals(1, imports.size());
assertEquals("import java.util.Collection", imports.get(0).getText().trim());
}
Aggregations