Search in sources :

Example 96 with ITextRegion

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

the class DefaultFoldingRegionProvider method computeObjectFolding.

/**
 * @since 2.8
 */
protected void computeObjectFolding(EObject eObject, IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor, boolean initiallyFolded) {
    ITextRegion region = locationInFileProvider.getFullTextRegion(eObject);
    if (region != null) {
        ITextRegion significant = locationInFileProvider.getSignificantTextRegion(eObject);
        if (significant == null)
            throw new NullPointerException("significant region may not be null");
        int offset = region.getOffset();
        ((IFoldingRegionAcceptorExtension<ITextRegion>) foldingRegionAcceptor).accept(offset, region.getLength(), initiallyFolded, significant);
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 97 with ITextRegion

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

the class DefaultFoldingRegionProvider method computeCommentFolding.

/**
 * @since 2.8
 */
protected void computeCommentFolding(IXtextDocument xtextDocument, IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor, ITypedRegion typedRegion, boolean initiallyFolded) throws BadLocationException {
    int offset = typedRegion.getOffset();
    int length = typedRegion.getLength();
    Matcher matcher = getTextPatternInComment().matcher(xtextDocument.get(offset, length));
    if (matcher.find()) {
        TextRegion significant = new TextRegion(offset + matcher.start(), 0);
        ((IFoldingRegionAcceptorExtension<ITextRegion>) foldingRegionAcceptor).accept(offset, length, initiallyFolded, significant);
    } else {
        ((IFoldingRegionAcceptorExtension<ITextRegion>) foldingRegionAcceptor).accept(offset, length, initiallyFolded);
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Matcher(java.util.regex.Matcher)

Example 98 with ITextRegion

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

the class AbstractEObjectHover method getXtextElementAt.

/**
 * Call this method only from within an IUnitOfWork
 */
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource, final int offset) {
    // check for cross reference
    EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
    if (crossLinkedEObject != null) {
        if (!crossLinkedEObject.eIsProxy()) {
            IParseResult parseResult = resource.getParseResult();
            if (parseResult != 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) {
                    ITextRegion leafRegion = leafNode.getTextRegion();
                    return Tuples.create(crossLinkedEObject, (IRegion) new Region(leafRegion.getOffset(), leafRegion.getLength()));
                }
            }
        }
    } else {
        EObject o = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
        if (o != null) {
            ITextRegion region = locationInFileProvider.getSignificantTextRegion(o);
            final IRegion region2 = new Region(region.getOffset(), region.getLength());
            if (TextUtilities.overlaps(region2, new Region(offset, 0)))
                return Tuples.create(o, region2);
        }
    }
    return null;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.xtext.util.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IParseResult(org.eclipse.xtext.parser.IParseResult) IRegion(org.eclipse.jface.text.IRegion)

Example 99 with ITextRegion

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

the class TraceBasedOpenerContributor method collectOpeners.

private void collectOpeners(IEclipseTrace trace, ITextRegion region, IAcceptor<FileOpener> acceptor) {
    Iterable<? extends ILocationInEclipseResource> locations = null;
    if (region != null)
        locations = trace.getAllAssociatedLocations(region);
    if (locations == null || Iterables.isEmpty(locations))
        locations = trace.getAllAssociatedLocations();
    Map<IStorage, ITextRegion> result = Maps.newHashMap();
    for (ILocationInEclipseResource location : locations) {
        IStorage storage = location.getPlatformResource();
        if (storage != null) {
            ITextRegion old = result.put(storage, location.getTextRegion());
            if (old != null) {
                ITextRegion merged = old.merge(location.getTextRegion());
                result.put(storage, merged);
            }
        }
    }
    for (Map.Entry<IStorage, ITextRegion> e : result.entrySet()) {
        IStorage storage = e.getKey();
        ITextRegion textRegion = e.getValue();
        acceptor.accept(createStorageBasedTextEditorOpener(storage, textRegion));
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) IStorage(org.eclipse.core.resources.IStorage) Map(java.util.Map)

Example 100 with ITextRegion

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

the class ParameterContextInformationProvider method getContextInformation.

@Override
public void getContextInformation(ContentAssistContext context, IContextInformationAcceptor acceptor) {
    XExpression containerCall = getContainerCall(eObjectAtOffsetHelper.resolveContainedElementAt(context.getResource(), context.getOffset()));
    LightweightTypeReferenceFactory factory = proposalProvider.getTypeConverter(context.getResource());
    if (containerCall != null) {
        ICompositeNode containerCallNode = NodeModelUtils.findActualNodeFor(containerCall);
        ITextRegion containerCallRegion = containerCallNode.getTextRegion();
        if (containerCallRegion.getOffset() > context.getOffset() || containerCallRegion.getOffset() + containerCallRegion.getLength() < context.getOffset())
            return;
        JvmIdentifiableElement calledFeature = getCalledFeature(containerCall);
        if (calledFeature instanceof JvmExecutable) {
            if (getParameterListOffset(containerCall) > context.getOffset())
                return;
            ParameterData parameterData = new ParameterData();
            IScope scope = getScope(containerCall);
            QualifiedName qualifiedName = QualifiedName.create(getCalledFeatureName(containerCall));
            boolean candidatesFound = false;
            for (IEObjectDescription element : scope.getElements(qualifiedName)) {
                if (element instanceof IIdentifiableElementDescription) {
                    IIdentifiableElementDescription featureDescription = (IIdentifiableElementDescription) element;
                    JvmIdentifiableElement featureCandidate = featureDescription.getElementOrProxy();
                    if (featureCandidate instanceof JvmExecutable) {
                        JvmExecutable executable = (JvmExecutable) featureCandidate;
                        if (!executable.getParameters().isEmpty()) {
                            StyledString styledString = new StyledString();
                            proposalProvider.appendParameters(styledString, executable, featureDescription.getNumberOfIrrelevantParameters(), factory);
                            parameterData.addOverloaded(styledString.toString(), executable.isVarArgs());
                            candidatesFound = true;
                        }
                    }
                }
            }
            if (candidatesFound) {
                StyledString displayString = proposalProvider.getStyledDisplayString((JvmExecutable) calledFeature, true, 0, qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(calledFeature)), calledFeature.getSimpleName(), factory);
                ParameterContextInformation parameterContextInformation = new ParameterContextInformation(parameterData, displayString.toString(), getParameterListOffset(containerCall), context.getOffset());
                acceptor.accept(parameterContextInformation);
            }
        }
    }
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription) StyledString(org.eclipse.jface.viewers.StyledString) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) LightweightTypeReferenceFactory(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceFactory) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IScope(org.eclipse.xtext.scoping.IScope) XExpression(org.eclipse.xtext.xbase.XExpression)

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