Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -72,8 +70,8 @@
public class ExtendedFormattingConfigBasedStream extends FormattingConfigBasedStream implements IDelegatingTokenStream {

private static final Logger LOGGER = LogManager.getLogger(ExtendedFormattingConfigBasedStream.class);
private final LinkedList<Integer> columnIndents = new LinkedList<Integer>(); // NOPMD LooseCoupling
private final Deque<Integer> initialIndents = new ArrayDeque<Integer>();
private final Stack<Integer> columnIndents = new Stack<Integer>(); // NOPMD LooseCoupling, ReplaceVectorWithList
private final Stack<Integer> initialIndents = new Stack<Integer>(); // NOPMD LooseCoupling, ReplaceVectorWithList
private final Set<NoFormatLocator> noFormatLocators = new HashSet<NoFormatLocator>();

private INode currentNode;
Expand Down Expand Up @@ -199,13 +197,25 @@ public LineEntry createLineEntry(final EObject grammarElement, final String valu
* @return
* top element or 0 if stack empty
*/
private int emptySafeStackPeek(final Deque<Integer> stack) {
if (!stack.isEmpty()) {
return stack.peekFirst();
private int emptySafeStackPeek(final Stack<Integer> stack) { // NOPMD LooseCoupling
if (!stack.empty()) {
return stack.peek();
}
return 0;
}

/**
* Popping an element from a stack of integers. Nothing happens if the stack is empty..
*
* @param stack
* stack of integers
*/
private void emptySafeStackPop(final Stack<Integer> stack) { // NOPMD LooseCoupling
if (!stack.empty()) {
stack.pop();
}
}

@Override
public Line createLine() {
return new ExtendedLine(this);
Expand Down Expand Up @@ -507,8 +517,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
boolean isSameIndentColumnLocator = candidateLocator instanceof FixedLocator
&& ((FixedLocator) candidateLocator).getColumn() == ((FixedLocator) locator).getColumn();
if (isSameIndentColumnLocator) {
boolean isOppositeToAfter = candidateLocator.getRight() != null && locator.getRight() == null;
boolean isOppositeToBefore = candidateLocator.getLeft() != null && locator.getLeft() == null;
boolean isOppositeToAfter = ((FixedLocator) candidateLocator).getRight() != null && ((FixedLocator) locator).getRight() == null;
boolean isOppositeToBefore = ((FixedLocator) candidateLocator).getLeft() != null && ((FixedLocator) locator).getLeft() == null;
if (isOppositeToAfter || isOppositeToBefore) {
return true;
}
Expand All @@ -523,8 +533,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
private void processColumnLocatorsBeforeComments() {
// workaround: xtext implementation assumes that when the comment occurs other locators (just before the comment) are not caught
if (last instanceof AbstractElement) {
columnIndents.pollFirst();
initialIndents.pollFirst();
emptySafeStackPop(columnIndents);
emptySafeStackPop(initialIndents);
}
}

Expand Down Expand Up @@ -563,21 +573,20 @@ public int compare(final ElementLocator o1, final ElementLocator o2) {
private void processColumnLocators(final List<ElementLocator> locators) {
Set<Integer> duplicatedColumnLocatorsIndicator = new HashSet<Integer>();
for (ElementLocator locator : locators) {
if (locator instanceof FixedLocator fixedLocator) {
Integer column = fixedLocator.getColumn();
if (locator.getRight() != null && !duplicatedColumnLocatorsIndicator.contains(column)) {
initialIndents.addFirst(indentationLevel);
columnIndents.addFirst(column);
duplicatedColumnLocatorsIndicator.add(column);
if (locator instanceof FixedLocator) {
if (locator.getRight() != null && !duplicatedColumnLocatorsIndicator.contains(((FixedLocator) locator).getColumn())) {
initialIndents.push(indentationLevel);
columnIndents.push(((FixedLocator) locator).getColumn());
duplicatedColumnLocatorsIndicator.add(((FixedLocator) locator).getColumn());
}
if (locator.getLeft() != null) {
int locatorIndexOnStack = columnIndents.lastIndexOf(column);
int locatorIndexOnStack = columnIndents.lastIndexOf(((FixedLocator) locator).getColumn());
if (locatorIndexOnStack != -1) {
columnIndents.remove(locatorIndexOnStack);
initialIndents.remove(locatorIndexOnStack);
} else {
columnIndents.pollFirst();
initialIndents.pollFirst();
emptySafeStackPop(columnIndents);
emptySafeStackPop(initialIndents);
}
}
}
Expand Down