use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method addModifierAccordingToTypename.
@Fix(IssueCodes.MODIFIER_DOES_NOT_MATCH_TYPENAME)
public void addModifierAccordingToTypename(final Issue issue, IssueResolutionAcceptor acceptor) {
String label = "Add missing abstract modifier.";
String description = "Add the abstract modifier to match naming conventions for the " + "type name. Delete final modifier if necessary.";
String image = null;
acceptor.accept(issue, label, description, image, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
if (element instanceof XtendClass) {
int index = 0;
List<String> modifiers = ((XtendClass) element).getModifiers();
boolean modExist = !modifiers.isEmpty();
// remove final modifier if present, as type cannot be both final and abstract
modifiers.remove("final");
// add abstract
if (modifiers.contains("public") || modifiers.contains("package")) {
index++;
}
if (modExist) {
modifiers.add(index, "abstract");
} else {
// use existent method to add abstract modifier, if modifier list is empty, to prevent wrong insertion
internalDoAddAbstractKeyword(element, context);
}
}
}
});
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix 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.ui.editor.quickfix.Fix 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.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method fixPackageName.
@Fix(IssueCodes.WRONG_PACKAGE)
public void fixPackageName(final Issue issue, IssueResolutionAcceptor acceptor) {
if (issue.getData() != null && issue.getData().length == 1) {
final String expectedPackage = issue.getData()[0];
acceptor.accept(issue, "Change package declaration to '" + expectedPackage + "'", "Change package declaration to '" + expectedPackage + "'", "package_obj.gif", new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XtendFile file = (XtendFile) element;
String newPackageName = isEmpty(expectedPackage) ? null : expectedPackage;
String oldPackageName = file.getPackage();
for (EObject obj : file.eResource().getContents()) {
if (obj instanceof JvmDeclaredType) {
JvmDeclaredType type = (JvmDeclaredType) obj;
String typePackage = type.getPackageName();
if (Objects.equal(typePackage, oldPackageName) || typePackage != null && typePackage.startsWith(oldPackageName + ".")) {
type.internalSetIdentifier(null);
type.setPackageName(newPackageName);
}
}
}
file.setPackage(newPackageName);
}
});
}
}
use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method fixFileName.
@Fix(IssueCodes.WRONG_FILE)
public void fixFileName(final Issue issue, IssueResolutionAcceptor acceptor) {
if (issue.getData() != null && issue.getData().length == 1) {
final String expectedFileName = issue.getData()[0];
final IFile iFile = projectUtil.findFileStorage(issue.getUriToProblem(), true);
final IPath pathToMoveTo = iFile.getParent().getFullPath().append(expectedFileName).addFileExtension(iFile.getFileExtension());
if (!iFile.getWorkspace().getRoot().exists(pathToMoveTo)) {
final String label = "Rename file to '" + expectedFileName + ".xtend'";
acceptor.accept(issue, label, label, "xtend_file.png", new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
runAsyncInDisplayThread(new Runnable() {
@Override
public void run() {
try {
iFile.move(pathToMoveTo, IResource.KEEP_HISTORY, null);
} catch (CoreException e) {
logger.error(e);
}
}
});
}
});
}
}
}
Aggregations