Search in sources :

Example 1 with ElementLocator

use of org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator in project xtext-core by eclipse.

the class FormattingConfigBasedStream method collectLocators.

protected Set<ElementLocator> collectLocators(EObject ele) {
    Set<ElementLocator> result = Sets.newHashSet(activeRangeLocators);
    Collection<ElementLocator> loc = Sets.newHashSet();
    if (ele instanceof AbstractElement)
        for (ElementPattern pattern : matcher.matchNext((AbstractElement) ele)) loc.add(pattern.getLocator());
    if ((last instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) last)) || (ele instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) ele)))
        loc = collectLocatorsForComments(loc, last, ele);
    last = ele;
    for (ElementLocator locator : loc) if (locator.getType() == LocatorType.RANGE && !activeRangeLocators.add(locator))
        activeRangeLocators.remove(locator);
    result.addAll(loc);
    for (ElementLocator locator : result) {
        if (locator instanceof IndentationLocatorStart)
            indentationLevel++;
        else if (locator instanceof IndentationLocatorEnd)
            indentationLevel--;
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ElementLocator(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator) ElementPattern(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementPattern) AbstractRule(org.eclipse.xtext.AbstractRule) IndentationLocatorStart(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorStart) IndentationLocatorEnd(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorEnd)

Example 2 with ElementLocator

use of org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator in project xtext-core by eclipse.

the class FormattingConfigBasedStream method collectLocatorsForComments.

protected Set<ElementLocator> collectLocatorsForComments(Collection<ElementLocator> semanticLocators, EObject left, EObject right) {
    Set<ElementLocator> result = Sets.newHashSet();
    for (ElementLocator semantic : semanticLocators) if (semantic instanceof IndentationLocatorStart || semantic instanceof IndentationLocatorEnd || (semantic.getRight() != null && semantic.getRight() == right) || (semantic.getLeft() != null && semantic.getLeft() == left))
        result.add(semantic);
    if (left != null)
        result.addAll(cfg.getLocatorsForCommentTokensAfter(left));
    if (right != null) {
        List<ElementLocator> leadingElementLocators = cfg.getLocatorsForCommentTokensBefore(right);
        for (Iterator<ElementLocator> i = result.iterator(); i.hasNext(); ) {
            ElementLocator locator = i.next();
            if (locator.getType() == LocatorType.BETWEEN && !leadingElementLocators.contains(locator))
                i.remove();
        }
        for (Iterator<ElementLocator> i = leadingElementLocators.iterator(); i.hasNext(); ) {
            if (i.next().getType() == LocatorType.BETWEEN)
                i.remove();
        }
        result.addAll(leadingElementLocators);
    }
    return result;
}
Also used : ElementLocator(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator) IndentationLocatorStart(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorStart) IndentationLocatorEnd(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorEnd)

Example 3 with ElementLocator

use of org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator in project xtext-core by eclipse.

the class FormattingConfigBasedStream method addLineEntry.

protected void addLineEntry(EObject grammarElement, String value, boolean isHidden) throws IOException {
    Pair<Integer, RuleCall> hiddenTokenDefCall1 = findTopmostHiddenTokenDef();
    Set<ElementLocator> locators = collectLocators(grammarElement);
    // System.out.println(locators + " --> " + value.replaceAll("\n", "\\n") + " -> '"
    // + (preservedWS != null ? preservedWS.replaceAll("\n", "\\n") : "") + "'");
    Pair<Integer, RuleCall> hiddenTokenDefCall2 = findTopmostHiddenTokenDef();
    ParserRule hiddenTokenDef = null;
    if (hiddenTokenDefCall1 != null && hiddenTokenDefCall2 != null) {
        if (hiddenTokenDefCall1.getFirst() < hiddenTokenDefCall2.getFirst())
            hiddenTokenDef = (ParserRule) hiddenTokenDefCall1.getSecond().getRule();
        else
            hiddenTokenDef = (ParserRule) hiddenTokenDefCall2.getSecond().getRule();
    }
    LineEntry e = createLineEntry(grammarElement, value, true, locators, preservedWS, indentationLevel, hiddenTokenDef);
    preservedWS = null;
    if (currentLine == null)
        currentLine = createLine();
    Line newLine = currentLine.add(e);
    if (newLine != null)
        currentLine = newLine;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) ElementLocator(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

ElementLocator (org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator)3 IndentationLocatorEnd (org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorEnd)2 IndentationLocatorStart (org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorStart)2 AbstractElement (org.eclipse.xtext.AbstractElement)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 ParserRule (org.eclipse.xtext.ParserRule)1 RuleCall (org.eclipse.xtext.RuleCall)1 ElementPattern (org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementPattern)1