use of org.eclipse.xtext.ui.editor.model.edit.IModification 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.IModification in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method removeUnusedPrivateMember.
@Fix(IssueCodes.UNUSED_PRIVATE_MEMBER)
public void removeUnusedPrivateMember(final Issue issue, IssueResolutionAcceptor acceptor) {
// use the same label, description and image
// to be able to use the quickfixes (issue resolution) in batch mode
String label = "Remove member.";
String description = "Remove the unused private member.";
String image = "delete_edit.png";
acceptor.accept(issue, label, description, image, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
final IXtextDocument document = context.getXtextDocument();
document.modify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
final EObject element = state.getEObject(issue.getUriToProblem().fragment());
final ICompositeNode elementNode = NodeModelUtils.findActualNodeFor(element);
final Position elementPosition = new Position(elementNode.getOffset(), elementNode.getLength());
document.addPosition(elementPosition);
if (element instanceof XtendField) {
final JvmField field = associations.getJvmField((XtendField) element);
final List<Position> assignments = EcoreUtil2.eAllContentsAsList(element.eResource()).stream().filter(XAssignment.class::isInstance).map(XAssignment.class::cast).filter(assignment -> field == assignment.getFeature()).map(assignment -> {
final ICompositeNode assignmentNode = NodeModelUtils.findActualNodeFor(assignment);
final int offset = assignmentNode.getOffset();
final XExpression assignmentValue = assignment.getValue();
int length = assignmentNode.getLength();
if (expressionHelper.hasSideEffects(assignmentValue)) {
length -= NodeModelUtils.findActualNodeFor(assignmentValue).getLength();
}
return new Position(offset, length);
}).collect(Collectors.toList());
for (final Position assignment : assignments) {
document.addPosition(assignment);
}
for (final Position assignment : assignments) {
document.replace(assignment.getOffset(), assignment.getLength(), "");
}
}
document.replace(elementPosition.getOffset(), elementPosition.getLength(), "");
}
});
organizeImportsHandler.doOrganizeImports(document);
}
});
}
use of org.eclipse.xtext.ui.editor.model.edit.IModification in project xtext-xtend by eclipse.
the class CodeBuilderQuickfix method addQuickfix.
public void addQuickfix(final ICodeBuilder builder, final String label, final Issue issue, final IssueResolutionAcceptor acceptor) {
boolean _isValid = builder.isValid();
if (_isValid) {
IModification _switchResult = null;
boolean _matched = false;
if (builder instanceof ICodeBuilder.Xtend) {
_matched = true;
_switchResult = this.getXtendModification(((ICodeBuilder.Xtend) builder));
}
if (!_matched) {
if (builder instanceof ICodeBuilder.Java) {
_matched = true;
_switchResult = this.getJavaModification(((ICodeBuilder.Java) builder));
}
}
final IModification modification = _switchResult;
acceptor.accept(issue, label, builder.getPreview(), builder.getImage(), modification);
}
}
use of org.eclipse.xtext.ui.editor.model.edit.IModification in project xtext-xtend by eclipse.
the class CodeBuilderQuickfix method getJavaModification.
protected IModification getJavaModification(final ICodeBuilder.Java builder) {
final IModification _function = (IModificationContext it) -> {
final IType type = builder.getIType();
char _charAt = ".".charAt(0);
final ImportManager importManager = new ImportManager(true, _charAt);
final StringBuilderBasedAppendable content = new StringBuilderBasedAppendable(importManager);
builder.build(content);
final Consumer<String> _function_1 = (String it_1) -> {
try {
ICompilationUnit _compilationUnit = type.getCompilationUnit();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_compilationUnit.createImport(it_1, null, _nullProgressMonitor);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
};
importManager.getImports().forEach(_function_1);
Object _switchResult = null;
boolean _matched = false;
if (builder instanceof JavaFieldBuilder) {
_matched = true;
String _string = content.toString();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_switchResult = type.createField(_string, null, true, _nullProgressMonitor);
}
if (!_matched) {
if (builder instanceof JavaConstructorBuilder) {
_matched = true;
String _string = content.toString();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
}
}
if (!_matched) {
if (builder instanceof JavaMethodBuilder) {
_matched = true;
String _string = content.toString();
NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
_switchResult = type.createMethod(_string, null, true, _nullProgressMonitor);
}
}
if (!_matched) {
_switchResult = null;
}
final Object element = ((Object) _switchResult);
if ((element != null)) {
JdtHyperlink _jdtHyperlink = new JdtHyperlink();
final Procedure1<JdtHyperlink> _function_2 = (JdtHyperlink it_1) -> {
it_1.setJavaElement(((IMember) element));
it_1.open();
};
ObjectExtensions.<JdtHyperlink>operator_doubleArrow(_jdtHyperlink, _function_2);
}
};
return _function;
}
use of org.eclipse.xtext.ui.editor.model.edit.IModification in project xtext-xtend by eclipse.
the class CodeBuilderQuickfix method getXtendModification.
protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
final IModification _function = (IModificationContext it) -> {
final XtendTypeDeclaration xtendClass = builder.getXtendType();
final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
if ((!(editor instanceof XtextEditor))) {
return;
}
final XtextEditor xtextEditor = ((XtextEditor) editor);
final IXtextDocument document = xtextEditor.getDocument();
final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
ReplacingAppendable _xblockexpression = null;
{
int offset = builder.getInsertOffset(resource);
wrapper.set(Integer.valueOf(offset));
final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
int _indentationLevel = builder.getIndentationLevel();
int _plus = (_indentationLevel + typeIndentation);
it_1.baseIndentationLevel = _plus;
it_1.ensureEmptyLinesAround = true;
};
DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
_xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
}
return _xblockexpression;
};
final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
Integer offset = wrapper.get();
builder.build(appendable);
appendable.commitChanges();
xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
};
return _function;
}
Aggregations