Search in sources :

Example 66 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 67 with ITextRegion

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

the class DefaultFoldingRangeProvider method buildSignificantRegion.

protected ITextRegion buildSignificantRegion(ITextRegion significantRegion, INode node) {
    if (significantRegion == null || node == null) {
        return null;
    }
    int offset = significantRegion.getOffset();
    int endOffset = significantRegion.getOffset() + significantRegion.getLength();
    int startLine;
    int endLine;
    if (significantRegion instanceof ITextRegionWithLineInformation) {
        ITextRegionWithLineInformation lineInfoRegion = (ITextRegionWithLineInformation) significantRegion;
        startLine = lineInfoRegion.getLineNumber() + 1;
        endLine = lineInfoRegion.getEndLineNumber() + 1;
    } else {
        startLine = NodeModelUtils.getLineAndColumn(node, offset).getLine();
        endLine = NodeModelUtils.getLineAndColumn(node, endOffset).getLine();
    }
    if (startLine != endLine) {
        /*
			 * The Eclipse IDE can only use the significant region if it starts and ends on the same line.
			 * Therefore, we here calculate the index of the end of the line.
			 */
        for (int index = offset; index < endOffset; index++) {
            LineAndColumn lineInfo = NodeModelUtils.getLineAndColumn(node, index);
            if (lineInfo.getLine() != startLine) {
                return new TextRegion(offset, index - offset - 1);
            }
        }
    }
    return significantRegion;
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) LineAndColumn(org.eclipse.xtext.util.LineAndColumn)

Example 68 with ITextRegion

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

the class HoverService method createContext.

protected HoverContext createContext(Document document, XtextResource resource, int offset) {
    EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
    if (crossLinkedEObject != null) {
        if (crossLinkedEObject.eIsProxy()) {
            return null;
        }
        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;
        }
        ITextRegion leafRegion = leafNode.getTextRegion();
        return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
    }
    EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
    if (element == null) {
        return null;
    }
    ITextRegion region = locationInFileProvider.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)

Example 69 with ITextRegion

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

the class DefaultDocumentHighlightService method getDocumentHighlights.

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 selectedElement = offsetHelper.resolveElementAt(resource, offset);
    if (!isDocumentHighlightAvailableFor(selectedElement, resource, offset)) {
        return emptyList();
    }
    final Supplier<Document> docSupplier = Suppliers.memoize(() -> new Document(UNUSED_VERSION, docContent));
    Iterable<URI> targetURIs = getTargetURIs(selectedElement);
    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(selectedElement.eResource())) {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(selectedElement);
        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 70 with ITextRegion

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

the class DocumentExtensions method newLocation.

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

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