Search in sources :

Example 46 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class RegionDiffFormatter method collectRegionsToFormat.

protected Collection<ITextRegion> collectRegionsToFormat(ITextRegionAccessDiff regions) {
    List<ITextRegion> result = Lists.newArrayList();
    for (ITextSegmentDiff diff : regions.getRegionDifferences()) {
        int offset = diff.getModifiedFirstRegion().getOffset();
        int length = diff.getModifiedLastRegion().getEndOffset() - offset;
        ITextSegment region = regions.regionForOffset(offset, length);
        result.add(region);
    }
    return result;
}
Also used : ITextSegmentDiff(org.eclipse.xtext.formatting2.regionaccess.ITextSegmentDiff) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 47 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class DefaultDocumentHighlightService method getDocumentHighlights.

@Override
public List<DocumentHighlight> getDocumentHighlights(final XtextResource resource, final int offset) {
    if (resource == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Resource was null.");
        }
        return emptyList();
    }
    final URI uri = resource.getURI();
    if (offset < 0) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Invalid offset argument. Offset must be a non-negative integer for resource: " + uri);
        }
        return emptyList();
    }
    final IParseResult parseResult = resource.getParseResult();
    if (parseResult == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Parse result was null for resource: " + uri);
        }
        return emptyList();
    }
    final ICompositeNode rootNode = parseResult.getRootNode();
    final String docContent = rootNode.getText();
    final int docLength = docContent.length();
    if (offset >= docLength) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Offset exceeds document lenght. Document was " + docLength + " and offset was: " + offset + " for resource: " + uri);
        }
        return emptyList();
    }
    final EObject selectedElemnt = offsetHelper.resolveElementAt(resource, offset);
    if (!isDocumentHighlightAvailableFor(selectedElemnt, resource, offset)) {
        return emptyList();
    }
    final Supplier<Document> docSupplier = Suppliers.memoize(() -> new Document(UNUSED_VERSION, docContent));
    Iterable<URI> targetURIs = getTargetURIs(selectedElemnt);
    if (!(targetURIs instanceof TargetURIs)) {
        final TargetURIs result = targetURIsProvider.get();
        result.addAllURIs(targetURIs);
        targetURIs = result;
    }
    final Builder<DocumentHighlight> resultBuilder = ImmutableList.<DocumentHighlight>builder();
    final Acceptor acceptor = (Acceptor2) (source, sourceURI, eReference, index, targetOrProxy, targetURI) -> {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(source, eReference, index);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Read));
        }
    };
    referenceFinder.findReferences((TargetURIs) targetURIs, resource, acceptor, new NullProgressMonitor());
    if (resource.equals(selectedElemnt.eResource())) {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(selectedElemnt);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Write));
        }
    }
    return FluentIterable.from(resultBuilder.build()).toSortedList(comparator);
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Acceptor(org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor) TargetURIs(org.eclipse.xtext.findReferences.TargetURIs) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 48 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class DocumentExtensions method newLocation.

public Location newLocation(final EObject object) {
    final Resource resource = object.eResource();
    final ITextRegion textRegion = this.locationInFileProvider.getSignificantTextRegion(object);
    return this.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 49 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class HoverService method createContext.

protected HoverContext createContext(final Document document, final XtextResource resource, final int offset) {
    final EObject crossLinkedEObject = this._eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
    if ((crossLinkedEObject != null)) {
        boolean _eIsProxy = crossLinkedEObject.eIsProxy();
        if (_eIsProxy) {
            return null;
        }
        final IParseResult parseResult = resource.getParseResult();
        if ((parseResult == null)) {
            return null;
        }
        ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
        if ((((leafNode != null) && leafNode.isHidden()) && (leafNode.getOffset() == offset))) {
            leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), (offset - 1));
        }
        if ((leafNode == null)) {
            return null;
        }
        final ITextRegion leafRegion = leafNode.getTextRegion();
        return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
    }
    final EObject element = this._eObjectAtOffsetHelper.resolveElementAt(resource, offset);
    if ((element == null)) {
        return null;
    }
    final ITextRegion region = this._iLocationInFileProvider.getSignificantTextRegion(element);
    return new HoverContext(document, resource, offset, region, element);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) HoverContext(org.eclipse.xtext.ide.server.hover.HoverContext)

Example 50 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)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)59 TextRegion (org.eclipse.xtext.util.TextRegion)24 Test (org.junit.Test)14 INode (org.eclipse.xtext.nodemodel.INode)11 EObject (org.eclipse.emf.ecore.EObject)9 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)9 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)9 Collection (java.util.Collection)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 CommonToken (org.antlr.runtime.CommonToken)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)3 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)3 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 CoreException (org.eclipse.core.runtime.CoreException)2 URI (org.eclipse.emf.common.util.URI)2