use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class DocumentExtensions method newFullLocation.
/**
* Returns with the {@link Location location} that represents the {@link ILocationInFileProvider#getFullTextRegion
* full text region} of the argument.
*
* @since 2.16
*/
public Location newFullLocation(EObject object) {
Resource resource = object.eResource();
ITextRegion textRegion = locationInFileProvider.getFullTextRegion(object);
return newLocation(resource, textRegion);
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class ContentAssistContextFactory method doComputePrefix.
public boolean doComputePrefix(ICompositeNode node, StringBuilder result) {
List<ILeafNode> hiddens = Lists.newArrayListWithCapacity(2);
for (INode child : node.getChildren()) {
if (child instanceof ICompositeNode) {
if (!doComputePrefix((ICompositeNode) child, result))
return false;
} else {
ILeafNode leaf = (ILeafNode) child;
ITextRegion leafRegion = leaf.getTextRegion();
if (leafRegion.getOffset() > completionOffset)
return false;
if (leaf.isHidden()) {
if (result.length() != 0)
hiddens.add((ILeafNode) child);
} else {
Iterator<ILeafNode> iter = hiddens.iterator();
while (iter.hasNext()) {
result.append(iter.next().getText());
}
hiddens.clear();
result.append(getNodeTextUpToCompletionOffset(leaf));
if (leafRegion.getOffset() + leafRegion.getLength() > completionOffset)
return false;
}
}
}
return true;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class ProposalConflictHelper method existsConflict.
@Override
public boolean existsConflict(String proposal, ContentAssistContext context) {
// hidden node between lastCompleteNode and currentNode?
INode lastCompleteNode = context.getLastCompleteNode();
ITextRegion replaceRegion = context.getReplaceRegion();
int nodeEnd = lastCompleteNode.getEndOffset();
if (nodeEnd < replaceRegion.getOffset())
return false;
return existsConflict(lastCompleteNode, replaceRegion.getOffset(), proposal, context);
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class SerializerTestHelper 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-core by eclipse.
the class Bug306325Test method performTestWithEncoding.
protected void performTestWithEncoding(String encoding) throws UnsupportedEncodingException, IOException {
String model = "�cl�p� M�d�l�ng Pr�j�kt";
byte[] utfBytes = model.getBytes(encoding);
XtextResource resource = createXtextResource();
resource.load(new ByteArrayInputStream(utfBytes), Collections.singletonMap(XtextResource.OPTION_ENCODING, encoding));
Model root = (Model) resource.getContents().get(0);
EList<Word> words = root.getWords();
assertEquals(3, words.size());
String[] lexemes = model.split(" ");
ILocationInFileProvider locationInFileProvider = getInjector().getInstance(ILocationInFileProvider.class);
ITextRegion location0 = locationInFileProvider.getSignificantTextRegion(words.get(0));
assertEquals(0, location0.getOffset());
assertEquals(lexemes[0].length(), location0.getLength());
ITextRegion location1 = locationInFileProvider.getSignificantTextRegion(words.get(1));
assertEquals(lexemes[0].length() + 1, location1.getOffset());
assertEquals(lexemes[1].length(), location1.getLength());
ITextRegion location2 = locationInFileProvider.getSignificantTextRegion(words.get(2));
assertEquals(lexemes[0].length() + lexemes[1].length() + 2, location2.getOffset());
assertEquals(lexemes[2].length(), location2.getLength());
}
Aggregations