private void addTypeCastToExplicitReceiver( XAbstractFeatureCall featureCall, IModificationContext context, JvmType declaringType, EReference structuralFeature) throws BadLocationException { List<INode> nodes = NodeModelUtils.findNodesForFeature(featureCall, structuralFeature); if (nodes.isEmpty()) return; INode firstNode = IterableExtensions.head(nodes); INode lastNode = IterableExtensions.last(nodes); int offset = firstNode.getOffset(); int length = lastNode.getEndOffset() - offset; ReplacingAppendable appendable = appendableFactory.create( context.getXtextDocument(), (XtextResource) featureCall.eResource(), offset, length); appendable.append("("); ListIterator<INode> nodeIter = nodes.listIterator(); while (nodeIter.hasNext()) { String text = nodeIter.next().getText(); if (nodeIter.previousIndex() == 0) appendable.append(Strings.removeLeadingWhitespace(text)); else if (nodeIter.nextIndex() == nodes.size()) appendable.append(Strings.trimTrailingLineBreak(text)); else appendable.append(text); } appendable.append(" as "); appendable.append(declaringType); appendable.append(")"); appendable.commitChanges(); }
private void addTypeCastToImplicitReceiver( XFeatureCall featureCall, IModificationContext context, JvmType declaringType) throws BadLocationException { String receiver; if (featureCall.getImplicitReceiver() instanceof XAbstractFeatureCall) receiver = ((XAbstractFeatureCall) featureCall.getImplicitReceiver()).getFeature().getSimpleName(); else return; List<INode> nodes = NodeModelUtils.findNodesForFeature( featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE); if (nodes.isEmpty()) return; INode firstNode = IterableExtensions.head(nodes); int offset = firstNode.getOffset(); ReplacingAppendable appendable = appendableFactory.create( context.getXtextDocument(), (XtextResource) featureCall.eResource(), offset, 0); appendable.append("("); appendable.append(receiver); appendable.append(" as "); appendable.append(declaringType); appendable.append(")."); appendable.commitChanges(); }