use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.
the class XtendHoverInEditorTest method testHoverOfReferencedElement.
@Test
public void testHoverOfReferencedElement() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* Hello Foo");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("class Foo {}");
_builder.newLine();
final String contentFoo = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("class Bar extends Foo {}");
_builder_1.newLine();
final String contentBar = _builder_1.toString();
final IFile fileFoo = this.helper.createFile("Foo.xtend", contentFoo);
final IFile fileBar = this.helper.createFile("Bar.xtend", contentBar);
this._syncUtil.waitForBuild(null);
final XtextEditor editor = this.helper.openEditor(fileBar);
ISourceViewer _internalSourceViewer = editor.getInternalSourceViewer();
Region _region = new Region(19, 1);
Object _hoverInfo2 = ((ITextHoverExtension2) this.hoverer).getHoverInfo2(((ITextViewer) _internalSourceViewer), _region);
final XtextBrowserInformationControlInput info = ((XtextBrowserInformationControlInput) _hoverInfo2);
Assert.assertTrue(info.getHtml().contains("Hello Foo"));
final XtextEditor fooEditor = this.helper.openEditor(fileFoo);
IXtextDocument _document = fooEditor.getDocument();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("/**");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* Hello BAZ");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("*/");
_builder_2.newLine();
_builder_2.append("class Foo {}");
_builder_2.newLine();
_document.set(_builder_2.toString());
this._syncUtil.waitForReconciler(fooEditor);
this._syncUtil.waitForReconciler(editor);
ISourceViewer _internalSourceViewer_1 = editor.getInternalSourceViewer();
Region _region_1 = new Region(19, 1);
Object _hoverInfo2_1 = ((ITextHoverExtension2) this.hoverer).getHoverInfo2(((ITextViewer) _internalSourceViewer_1), _region_1);
final XtextBrowserInformationControlInput info2 = ((XtextBrowserInformationControlInput) _hoverInfo2_1);
Assert.assertFalse(info2.getHtml().contains("Hello Foo"));
Assert.assertTrue(info2.getHtml().contains("Hello BAZ"));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.
the class XtendQuickfixProvider method internalDoAddAbstractKeyword.
protected void internalDoAddAbstractKeyword(EObject element, IModificationContext context) throws BadLocationException {
if (element instanceof XtendFunction) {
element = element.eContainer();
}
if (element instanceof XtendClass) {
XtendClass clazz = (XtendClass) element;
IXtextDocument document = context.getXtextDocument();
ICompositeNode clazzNode = NodeModelUtils.findActualNodeFor(clazz);
if (clazzNode == null)
throw new IllegalStateException("Cannot determine node for clazz " + clazz.getName());
int offset = -1;
for (ILeafNode leafNode : clazzNode.getLeafNodes()) {
if (leafNode.getText().equals("class")) {
offset = leafNode.getOffset();
break;
}
}
ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) clazz.eResource(), offset, 0);
appendable.append("abstract ");
appendable.commitChanges();
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument 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.IXtextDocument 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.IXtextDocument 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