use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext 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.model.edit.IModificationContext 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);
}
}
});
}
});
}
}
}
use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext 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.ui.editor.model.edit.IModificationContext 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();
}
});
}
}
}
Aggregations