Search in sources :

Example 31 with ITextRegion

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

the class AbstractParseTreeConstructor method write.

protected ITextRegion write(AbstractToken token, WsMergerStream out, ITextRegion location) throws IOException {
    ITextRegion currentLocation = location;
    INode node = token.getNode();
    if (node != null) {
        currentLocation = currentLocation.merge(node.getTextRegion());
    }
    if (!token.getTokensForSemanticChildren().isEmpty()) {
        for (AbstractToken t : token.getTokensForSemanticChildren()) currentLocation = write(t, out, currentLocation);
    } else {
        if (token instanceof CommentToken)
            out.writeComment((ILeafNode) node);
        else {
            String val = token.serialize(node);
            if (val != null) {
                if (token instanceof AssignmentToken)
                    out.writeSemantic(((AssignmentToken) token).getAssignmentElement(), val, node);
                else
                    out.writeSemantic(token.getGrammarElement(), val, node);
            }
        }
    }
    return currentLocation;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 32 with ITextRegion

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

the class AbstractParseTreeConstructor method serializeSubtree.

@Override
public TreeConstructionReport serializeSubtree(EObject object, ITokenStream out) throws IOException {
    TreeConstructionReportImpl report = createReport(object);
    AbstractToken root = serialize(object, report);
    Set<ICompositeNode> roots = Sets.newHashSet();
    Map<EObject, AbstractToken> obj2token = Maps.newHashMap();
    collectRootsAndEObjects(root, obj2token, roots);
    // dump("", root);
    Map<ILeafNode, EObject> comments = commentAssociater.associateCommentsWithSemanticEObjects(object, roots);
    for (ICompositeNode r : roots) assignNodesByMatching(obj2token, r, comments);
    WsMergerStream wsout = new WsMergerStream(out);
    // dump("", root);
    // System.out.println(EmfFormatter.objToStr(roots.iterator().next(),
    // ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_LENGTH,
    // ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_OFFSET,
    // ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_LINE, ParsetreePackage.Literals.ABSTRACT_NODE__PARENT));
    ITextRegion previousLocation = ITextRegion.EMPTY_REGION;
    initStream(root, wsout);
    previousLocation = write(root, wsout, previousLocation);
    wsout.flush();
    report.setPreviousLocation(previousLocation);
    return report;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 33 with ITextRegion

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

the class TokenRegionProvider method getTokenReplaceRegion.

public ReplaceRegion getTokenReplaceRegion(String changedText, ReplaceRegion replaceRegion) {
    int lengthDelta = replaceRegion.getText().length() - replaceRegion.getLength();
    ITextRegion tokenRegion = getTokenRegion(changedText, new TextRegion(replaceRegion.getOffset(), replaceRegion.getText().length()));
    if (tokenRegion.getOffset() == replaceRegion.getOffset() && tokenRegion.getLength() == replaceRegion.getText().length())
        return replaceRegion;
    else
        return new ReplaceRegion(tokenRegion.getOffset(), tokenRegion.getLength() - lengthDelta, changedText.substring(tokenRegion.getOffset(), tokenRegion.getOffset() + tokenRegion.getLength()));
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 34 with ITextRegion

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

the class TokenRegionProvider method getTokenRegion.

public ITextRegion getTokenRegion(String text, ITextRegion region) {
    Lexer lexer = lexerProvider.get();
    lexer.setCharStream(new ANTLRStringStream(text));
    int currentStart = 0;
    int currentEnd = 0;
    CommonToken nextToken = (CommonToken) lexer.nextToken();
    int regionStart = region.getOffset();
    int regionEnd = regionStart + region.getLength();
    while (nextToken != Token.EOF_TOKEN && currentEnd <= regionStart) {
        currentStart = nextToken.getStartIndex();
        currentEnd = nextToken.getStopIndex() + 1;
        nextToken = (CommonToken) lexer.nextToken();
    }
    // nextToken is either EOF or the first token that follows the start of the given region
    while (nextToken != Token.EOF_TOKEN && currentEnd < regionEnd) {
        currentEnd = nextToken.getStopIndex() + 1;
        nextToken = (CommonToken) lexer.nextToken();
    }
    if (currentStart != regionStart || currentEnd != regionEnd)
        return new TextRegion(currentStart, currentEnd - currentStart);
    else
        return region;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) Lexer(org.eclipse.xtext.parser.antlr.Lexer) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) CommonToken(org.antlr.runtime.CommonToken)

Example 35 with ITextRegion

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

the class AbstractTraceRegionToString method render.

protected List<String> render(AbstractTraceRegionToString.File file, int width) {
    try {
        String text = file.uri == null ? getLocalText() : getRemoteText(file.uri);
        ITextRegion localOrRemoteFrame = file.uri == null ? getLocalFrame() : getRemoteFrame(file.uri);
        ITextRegion frame = localOrRemoteFrame == null ? new TextRegion(0, text.length()) : localOrRemoteFrame;
        int last = frame.getOffset();
        StringBuilder result = new StringBuilder();
        for (Map.Entry<Integer, Collection<Insert>> e : sortBy(toList(index(filter(file.inserts, i -> i.offset >= frame.getOffset() && i.offset <= (frame.getOffset() + frame.getLength())), i -> i.offset).asMap().entrySet()), i -> i.getKey())) {
            int offset = e.getKey().intValue();
            result.append(text.substring(last, offset));
            result.append(render(e.getValue(), width));
            last = offset;
        }
        int end = frame.getOffset() + frame.getLength();
        if (last < end) {
            result.append(text.substring(last, end));
        }
        return CharStreams.readLines(new StringReader(result.toString()));
    } catch (IOException t) {
        throw new RuntimeIOException(t);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Collection(java.util.Collection) Strings(org.eclipse.xtext.util.Strings) Set(java.util.Set) TextRegion(org.eclipse.xtext.util.TextRegion) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) IterableExtensions.filter(org.eclipse.xtext.xbase.lib.IterableExtensions.filter) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) List(java.util.List) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) ITextRegion(org.eclipse.xtext.util.ITextRegion) StringReader(java.io.StringReader) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) CharStreams(com.google.common.io.CharStreams) Iterables.concat(com.google.common.collect.Iterables.concat) Map(java.util.Map) LinkedHashSet(java.util.LinkedHashSet) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IOException(java.io.IOException) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) ITextRegion(org.eclipse.xtext.util.ITextRegion) StringReader(java.io.StringReader) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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