Search in sources :

Example 1 with ITextRegionWithLineInformation

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

the class XbaseEditor method selectAndReveal.

@Override
protected void selectAndReveal(final int selectionStart, final int selectionLength, final int revealStart, final int revealLength) {
    try {
        reentrantCallFromSelf++;
        if (expectJavaSelection > 0) {
            try {
                ITrace traceToSource = getTraceStorage();
                if (traceToSource != null) {
                    IResource javaResource = typeRoot.getResource();
                    if (expectLineSelection && javaResource instanceof IStorage) {
                        if (isCompiledWithJSR45()) {
                            try {
                                String string = Files.readStreamIntoString(((IStorage) javaResource).getContents());
                                Document javaDocument = new Document(string);
                                int line = getLineInJavaDocument(javaDocument, selectionStart, selectionLength);
                                if (line != -1) {
                                    int startOffsetOfContents = getStartOffsetOfContentsInJava(javaDocument, line);
                                    if (startOffsetOfContents != -1) {
                                        ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(startOffsetOfContents, 0));
                                        if (bestSelection != null) {
                                            final ITextRegionWithLineInformation textRegion = bestSelection.getTextRegion();
                                            if (textRegion != null) {
                                                int lineToSelect = textRegion.getLineNumber();
                                                try {
                                                    IRegion lineInfo = getDocument().getLineInformation(lineToSelect);
                                                    super.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength(), lineInfo.getOffset(), lineInfo.getLength());
                                                    return;
                                                } catch (BadLocationException e) {
                                                    log.error(e.getMessage(), e);
                                                }
                                            }
                                        }
                                    }
                                }
                            } catch (BadLocationException e) {
                            // do nothing
                            } catch (CoreException e) {
                            // do nothing
                            }
                        }
                    } else if (selectionStart >= 0 && selectionLength >= 0) {
                        ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(selectionStart, selectionLength));
                        if (bestSelection != null) {
                            ILocationInResource bestReveal = bestSelection;
                            if (selectionStart != revealStart || selectionLength != revealLength) {
                                bestReveal = traceToSource.getBestAssociatedLocation(new TextRegion(revealStart, revealLength));
                                if (bestReveal == null) {
                                    bestReveal = bestSelection;
                                }
                            }
                            ITextRegion fixedSelection = bestSelection.getTextRegion();
                            if (fixedSelection != null) {
                                ITextRegion fixedReveal = bestReveal.getTextRegion();
                                if (fixedReveal == null) {
                                    fixedReveal = fixedSelection;
                                }
                                super.selectAndReveal(fixedSelection.getOffset(), fixedSelection.getLength(), fixedReveal.getOffset(), fixedReveal.getLength());
                                return;
                            }
                        }
                    }
                }
            } finally {
                expectLineSelection = false;
                expectJavaSelection--;
            }
        }
        super.selectAndReveal(selectionStart, selectionLength, revealStart, revealLength);
    } finally {
        reentrantCallFromSelf--;
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IStorage(org.eclipse.core.resources.IStorage) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) IRegion(org.eclipse.jface.text.IRegion) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITrace(org.eclipse.xtext.generator.trace.ITrace) IResource(org.eclipse.core.resources.IResource) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation in project xtext-xtend by eclipse.

the class XtendLocationInFileProvider method getSignificantTextRegion.

@Override
public ITextRegion getSignificantTextRegion(EObject element) {
    if (element instanceof RichStringLiteral) {
        ICompositeNode elementNode = findNodeFor(element);
        if (elementNode == null) {
            return ITextRegion.EMPTY_REGION;
        }
        ITextRegion result = ITextRegion.EMPTY_REGION;
        for (INode node : elementNode.getLeafNodes()) {
            if (isHidden(node)) {
                continue;
            }
            EObject grammarElement = node.getGrammarElement();
            if (!(grammarElement instanceof RuleCall)) {
                continue;
            }
            RuleCall ruleCall = (RuleCall) grammarElement;
            ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation();
            int offset = region.getOffset();
            int length = region.getLength();
            if (grammarAccess.getRICH_TEXTRule() == ruleCall.getRule()) {
                offset += 3;
                length -= 6;
            } else if (grammarAccess.getRICH_TEXT_STARTRule() == ruleCall.getRule()) {
                offset += 3;
                length -= 4;
            } else if (grammarAccess.getRICH_TEXT_ENDRule() == ruleCall.getRule()) {
                offset += 1;
                length -= 4;
            } else if (grammarAccess.getRICH_TEXT_INBETWEENRule() == ruleCall.getRule()) {
                offset += 1;
                length -= 2;
            } else if (grammarAccess.getCOMMENT_RICH_TEXT_ENDRule() == ruleCall.getRule()) {
                offset += 2;
                length -= 5;
            } else if (grammarAccess.getCOMMENT_RICH_TEXT_INBETWEENRule() == ruleCall.getRule()) {
                offset += 2;
                length -= 3;
            } else {
                continue;
            }
            result = result.merge(toZeroBasedRegion(new TextRegionWithLineInformation(offset, length, region.getLineNumber(), region.getEndLineNumber())));
        }
        return result;
    }
    return super.getSignificantTextRegion(element);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) RichStringLiteral(org.eclipse.xtend.core.xtend.RichStringLiteral) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) RuleCall(org.eclipse.xtext.RuleCall) TextRegionWithLineInformation(org.eclipse.xtext.util.TextRegionWithLineInformation) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation)

Example 3 with ITextRegionWithLineInformation

use of org.eclipse.xtext.util.ITextRegionWithLineInformation in project xtext-xtend by eclipse.

the class CompilerTraceTest method merge.

public AbstractTraceRegion merge(final List<AbstractTraceRegion> regions) {
    boolean _isEmpty = regions.isEmpty();
    boolean _not = (!_isEmpty);
    Assert.assertTrue(_not);
    int _size = regions.size();
    boolean _greaterThan = (_size > 1);
    if (_greaterThan) {
        ITextRegionWithLineInformation rootLocation = ITextRegionWithLineInformation.EMPTY_REGION;
        ITextRegionWithLineInformation associated = ITextRegionWithLineInformation.EMPTY_REGION;
        for (final AbstractTraceRegion child : regions) {
            {
                int _myOffset = child.getMyOffset();
                int _myLength = child.getMyLength();
                int _myLineNumber = child.getMyLineNumber();
                int _myEndLineNumber = child.getMyEndLineNumber();
                TextRegionWithLineInformation _textRegionWithLineInformation = new TextRegionWithLineInformation(_myOffset, _myLength, _myLineNumber, _myEndLineNumber);
                rootLocation = rootLocation.merge(_textRegionWithLineInformation);
                ILocationData childAssociation = child.getMergedAssociatedLocation();
                if ((childAssociation != null)) {
                    associated = associated.merge(childAssociation);
                }
            }
        }
        final RootTraceRegionForTesting root = new RootTraceRegionForTesting(rootLocation, associated);
        for (final AbstractTraceRegion child_1 : regions) {
            child_1.setParent(root);
        }
        return root;
    } else {
        return IterableExtensions.<AbstractTraceRegion>head(regions);
    }
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) RootTraceRegionForTesting(org.eclipse.xtend.core.tests.compiler.RootTraceRegionForTesting) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) TextRegionWithLineInformation(org.eclipse.xtext.util.TextRegionWithLineInformation) ILocationData(org.eclipse.xtext.generator.trace.ILocationData)

Example 4 with ITextRegionWithLineInformation

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

the class DefaultHierarchyNodeLocationProvider method toTextRegionWithLineInformation.

protected ITextRegionWithLineInformation toTextRegionWithLineInformation(final EObject obj, final ITextRegion textRegion) {
    if ((textRegion == null)) {
        return ITextRegionWithLineInformation.EMPTY_REGION;
    }
    if ((textRegion instanceof ITextRegionWithLineInformation)) {
        return ((ITextRegionWithLineInformation) textRegion);
    }
    final ICompositeNode node = NodeModelUtils.getNode(obj);
    if ((node == null)) {
        int _offset = textRegion.getOffset();
        int _length = textRegion.getLength();
        return new TextRegionWithLineInformation(_offset, _length, 0, 0);
    }
    int _line = NodeModelUtils.getLineAndColumn(node, textRegion.getOffset()).getLine();
    final int startLine = (_line - 1);
    int _offset_1 = textRegion.getOffset();
    int _length_1 = textRegion.getLength();
    int _plus = (_offset_1 + _length_1);
    int _line_1 = NodeModelUtils.getLineAndColumn(node, _plus).getLine();
    final int endLine = (_line_1 - 1);
    int _offset_2 = textRegion.getOffset();
    int _length_2 = textRegion.getLength();
    return new TextRegionWithLineInformation(_offset_2, _length_2, startLine, endLine);
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) TextRegionWithLineInformation(org.eclipse.xtext.util.TextRegionWithLineInformation) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation)

Example 5 with ITextRegionWithLineInformation

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

the class DiagnosticConverterImpl method getLocationForNode.

protected IssueLocation getLocationForNode(INode node) {
    ITextRegionWithLineInformation nodeRegion = node.getTextRegionWithLineInformation();
    IssueLocation result = new IssueLocation();
    result.lineNumber = nodeRegion.getLineNumber();
    result.offset = nodeRegion.getOffset();
    result.column = NodeModelUtils.getLineAndColumn(node, result.offset).getColumn();
    result.length = nodeRegion.getLength();
    return result;
}
Also used : ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation)

Aggregations

ITextRegionWithLineInformation (org.eclipse.xtext.util.ITextRegionWithLineInformation)29 Test (org.junit.Test)14 TextRegionWithLineInformation (org.eclipse.xtext.util.TextRegionWithLineInformation)5 AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)4 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)4 Mode (org.eclipse.xtext.resource.locationprovidertest.Mode)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)3 INode (org.eclipse.xtext.nodemodel.INode)3 Bus (org.eclipse.xtext.resource.locationprovidertest.Bus)3 Processor (org.eclipse.xtext.resource.locationprovidertest.Processor)3 TextRegion (org.eclipse.xtext.util.TextRegion)3 IStorage (org.eclipse.core.resources.IStorage)2 EObject (org.eclipse.emf.ecore.EObject)2 ILocationInResource (org.eclipse.xtext.generator.trace.ILocationInResource)2 LocationData (org.eclipse.xtext.generator.trace.LocationData)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 ILocationInEclipseResource (org.eclipse.xtext.ui.generator.trace.ILocationInEclipseResource)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1