use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.
the class ExtractMethodRefactoring method getMethodBodyWithRenamedParameters.
protected String getMethodBodyWithRenamedParameters(ITextRegion expressionsRegion) throws BadLocationException {
String expressionsAsString = document.get(expressionsRegion.getOffset(), expressionsRegion.getLength());
List<ReplaceRegion> parameterRenames = newArrayList();
for (final String parameterName : externalFeatureCalls.keySet()) {
ParameterInfo parameter = find(parameterInfos, new Predicate<ParameterInfo>() {
@Override
public boolean apply(ParameterInfo info) {
return equal(info.getOldName(), parameterName);
}
});
if (parameter.isRenamed()) {
for (XFeatureCall featureCall : externalFeatureCalls.get(parameterName)) {
ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, -1);
parameterRenames.add(new ReplaceRegion(textRegion, parameter.getNewName()));
}
}
}
sort(parameterRenames, new Comparator<ReplaceRegion>() {
@Override
public int compare(ReplaceRegion o1, ReplaceRegion o2) {
return o2.getOffset() - o1.getOffset();
}
});
StringBuffer buffer = new StringBuffer(expressionsAsString);
for (ReplaceRegion parameterRename : parameterRenames) {
buffer.replace(parameterRename.getOffset() - expressionsRegion.getOffset(), parameterRename.getEndOffset() - expressionsRegion.getOffset(), parameterRename.getText());
}
expressionsAsString = buffer.toString();
return expressionsAsString;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.
the class XtendExpressionUtil method getRichStringPartTextRegion.
protected ITextRegion getRichStringPartTextRegion(EObject element) {
if (element instanceof RichStringLiteral) {
return locationInFileProvider.getSignificantTextRegion(element);
}
ICompositeNode elementNode = NodeModelUtils.getNode(element);
ITextRegion totalTextRegion = elementNode.getTotalTextRegion();
int offset = totalTextRegion.getOffset() - 1;
int length = totalTextRegion.getLength() + 2;
ILeafNode nextNode = NodeModelUtils.findLeafNodeAtOffset(elementNode.getRootNode(), elementNode.getEndOffset());
while (nextNode != null && nextNode.isHidden()) {
length += nextNode.getLength();
INode nextSibling = nextNode.getNextSibling();
if (nextSibling instanceof ILeafNode) {
nextNode = (ILeafNode) nextSibling;
} else {
nextNode = null;
}
}
return new TextRegion(offset, length);
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class FormatterTester method assertReplacementsAreInRegion.
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
Set<ITextReplacement> invalid = Sets.newHashSet();
ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
if (allowed.contains(r))
continue ALLOWED;
invalid.add(r);
}
if (!invalid.isEmpty()) {
String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class SerializerTester method assertSerializeWithoutNodeModel.
public void assertSerializeWithoutNodeModel(EObject semanticObject) {
try {
EObject parsed;
if (semanticObject.eResource().getContents().contains(semanticObject)) {
List<Pair<EObject, ICompositeNode>> nodes = detachNodeModel(semanticObject);
String serialized = serializeWithoutNodeModel(semanticObject);
parsed = parseHelper.parse(serialized, semanticObject.eResource().getResourceSet());
reattachNodes(nodes);
} else {
INode oldNode = NodeModelUtils.getNode(semanticObject);
String oldtext = oldNode.getRootNode().getText();
String oldURI = semanticObject.eResource().getURIFragment(semanticObject);
List<Pair<EObject, ICompositeNode>> nodes = detachNodeModel(semanticObject);
String serialized = serializeWithoutNodeModel(semanticObject);
ITextRegion oldRegion = oldNode.getTextRegion();
String newtext = oldtext.substring(0, oldRegion.getOffset()) + serialized + oldtext.substring(oldRegion.getOffset() + oldRegion.getLength());
EObject newmodel = parseHelper.parse(newtext, semanticObject.eResource().getResourceSet());
parsed = newmodel.eResource().getEObject(oldURI);
reattachNodes(nodes);
}
EcoreUtil.resolveAll(parsed);
Assert.assertTrue(parsed.eResource().getErrors().toString(), parsed.eResource().getErrors().isEmpty());
parsed.eResource().getResourceSet().getResources().remove(parsed.eResource());
assertEqualWithEmfFormatter(semanticObject, parsed);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-eclipse by eclipse.
the class DefaultFoldingRegionAcceptor method accept.
/**
* @since 2.8
*/
@Override
public void accept(int offset, int length, boolean initiallyFolded, ITextRegion significantRegion) {
IRegion position = getLineRegion(offset, length);
try {
if (xtextDocument != null && significantRegion != null) {
int firstLine = xtextDocument.getLineOfOffset(significantRegion.getOffset());
int lastLine = xtextDocument.getLineOfOffset(significantRegion.getOffset() + significantRegion.getLength());
if (firstLine != lastLine) {
int endOffset = xtextDocument.getLineOffset(firstLine) + xtextDocument.getLineLength(firstLine);
significantRegion = new TextRegion(significantRegion.getOffset(), endOffset - significantRegion.getOffset());
}
}
} catch (BadLocationException e) {
}
FoldedPosition foldingRegion = newFoldedPosition(position, significantRegion);
if (foldingRegion != null) {
if (foldingRegion instanceof DefaultFoldedPosition) {
((DefaultFoldedPosition) foldingRegion).setInitiallyFolded(initiallyFolded);
}
result.add(foldingRegion);
}
}
Aggregations