Search in sources :

Example 71 with ITextRegion

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);
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource)

Example 72 with ITextRegion

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;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 73 with ITextRegion

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);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 74 with ITextRegion

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);
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) Pair(org.eclipse.xtext.util.Pair)

Example 75 with ITextRegion

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());
}
Also used : Word(org.eclipse.xtext.parser.encoding.encodingTest.Word) ByteArrayInputStream(java.io.ByteArrayInputStream) ITextRegion(org.eclipse.xtext.util.ITextRegion) Model(org.eclipse.xtext.parser.encoding.encodingTest.Model)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)122 TextRegion (org.eclipse.xtext.util.TextRegion)44 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)20 INode (org.eclipse.xtext.nodemodel.INode)19 XtextResource (org.eclipse.xtext.resource.XtextResource)17 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)15 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)9 XExpression (org.eclipse.xtext.xbase.XExpression)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 IParseResult (org.eclipse.xtext.parser.IParseResult)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)7 IRegion (org.eclipse.jface.text.IRegion)6 ITextSelection (org.eclipse.jface.text.ITextSelection)6 Collection (java.util.Collection)5 ArrayList (java.util.ArrayList)4 TextSelection (org.eclipse.jface.text.TextSelection)4