use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext 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;
}
use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext 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.IModificationContext in project xtext-xtend by eclipse.
the class CreateXtendTypeQuickfixes method newXtendAnnotationQuickfix.
protected void newXtendAnnotationQuickfix(final String typeName, final String explicitPackage, final XtextResource resource, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
String packageDescription = getPackageDescription(explicitPackage);
issueResolutionAcceptor.accept(issue, "Create Xtend annotation '" + typeName + "'" + packageDescription, "Opens the new Xtend annotation wizard to create the type '" + typeName + "'" + packageDescription, "xtend_file.png", new IModification() {
@Override
public void apply(/* @Nullable */
IModificationContext context) throws Exception {
runAsyncInDisplayThread(new Runnable() {
@Override
public void run() {
NewElementWizard newXtendAnnotationWizard = newXtendAnnotationWizardProvider.get();
WizardDialog dialog = createWizardDialog(newXtendAnnotationWizard);
NewXtendAnnotationWizardPage page = (NewXtendAnnotationWizardPage) newXtendAnnotationWizard.getStartingPage();
configureWizardPage(page, resource.getURI(), typeName, explicitPackage);
dialog.open();
}
});
}
});
}
use of org.eclipse.xtext.ui.editor.model.edit.IModificationContext 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.IModificationContext 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