1414import java .lang .reflect .Method ;
1515import java .lang .reflect .ParameterizedType ;
1616import java .lang .reflect .Type ;
17- import java .util .ArrayDeque ;
1817import java .util .Collection ;
1918import java .util .Collections ;
2019import java .util .Comparator ;
21- import java .util .Deque ;
2220import java .util .HashMap ;
2321import java .util .HashSet ;
24- import java .util .LinkedList ;
2522import java .util .List ;
2623import java .util .Map ;
2724import java .util .Set ;
25+ import java .util .Stack ;
2826
2927import org .apache .logging .log4j .LogManager ;
3028import org .apache .logging .log4j .Logger ;
7270public class ExtendedFormattingConfigBasedStream extends FormattingConfigBasedStream implements IDelegatingTokenStream {
7371
7472 private static final Logger LOGGER = LogManager .getLogger (ExtendedFormattingConfigBasedStream .class );
75- private final LinkedList <Integer > columnIndents = new LinkedList <Integer >(); // NOPMD LooseCoupling
76- private final Deque <Integer > initialIndents = new ArrayDeque <Integer >();
73+ private final Stack <Integer > columnIndents = new Stack <Integer >(); // NOPMD LooseCoupling, ReplaceVectorWithList
74+ private final Stack <Integer > initialIndents = new Stack <Integer >(); // NOPMD LooseCoupling, ReplaceVectorWithList
7775 private final Set <NoFormatLocator > noFormatLocators = new HashSet <NoFormatLocator >();
7876
7977 private INode currentNode ;
@@ -199,13 +197,25 @@ public LineEntry createLineEntry(final EObject grammarElement, final String valu
199197 * @return
200198 * top element or 0 if stack empty
201199 */
202- private int emptySafeStackPeek (final Deque <Integer > stack ) {
203- if (!stack .isEmpty ()) {
204- return stack .peekFirst ();
200+ private int emptySafeStackPeek (final Stack <Integer > stack ) { // NOPMD LooseCoupling
201+ if (!stack .empty ()) {
202+ return stack .peek ();
205203 }
206204 return 0 ;
207205 }
208206
207+ /**
208+ * Popping an element from a stack of integers. Nothing happens if the stack is empty..
209+ *
210+ * @param stack
211+ * stack of integers
212+ */
213+ private void emptySafeStackPop (final Stack <Integer > stack ) { // NOPMD LooseCoupling
214+ if (!stack .empty ()) {
215+ stack .pop ();
216+ }
217+ }
218+
209219 @ Override
210220 public Line createLine () {
211221 return new ExtendedLine (this );
@@ -507,8 +517,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
507517 boolean isSameIndentColumnLocator = candidateLocator instanceof FixedLocator
508518 && ((FixedLocator ) candidateLocator ).getColumn () == ((FixedLocator ) locator ).getColumn ();
509519 if (isSameIndentColumnLocator ) {
510- boolean isOppositeToAfter = candidateLocator .getRight () != null && locator .getRight () == null ;
511- boolean isOppositeToBefore = candidateLocator .getLeft () != null && locator .getLeft () == null ;
520+ boolean isOppositeToAfter = (( FixedLocator ) candidateLocator ) .getRight () != null && (( FixedLocator ) locator ) .getRight () == null ;
521+ boolean isOppositeToBefore = (( FixedLocator ) candidateLocator ) .getLeft () != null && (( FixedLocator ) locator ) .getLeft () == null ;
512522 if (isOppositeToAfter || isOppositeToBefore ) {
513523 return true ;
514524 }
@@ -523,8 +533,8 @@ public boolean containsOpposite(final List<ElementLocator> locators, final Eleme
523533 private void processColumnLocatorsBeforeComments () {
524534 // workaround: xtext implementation assumes that when the comment occurs other locators (just before the comment) are not caught
525535 if (last instanceof AbstractElement ) {
526- columnIndents . pollFirst ( );
527- initialIndents . pollFirst ( );
536+ emptySafeStackPop ( columnIndents );
537+ emptySafeStackPop ( initialIndents );
528538 }
529539 }
530540
@@ -563,21 +573,20 @@ public int compare(final ElementLocator o1, final ElementLocator o2) {
563573 private void processColumnLocators (final List <ElementLocator > locators ) {
564574 Set <Integer > duplicatedColumnLocatorsIndicator = new HashSet <Integer >();
565575 for (ElementLocator locator : locators ) {
566- if (locator instanceof FixedLocator fixedLocator ) {
567- Integer column = fixedLocator .getColumn ();
568- if (locator .getRight () != null && !duplicatedColumnLocatorsIndicator .contains (column )) {
569- initialIndents .addFirst (indentationLevel );
570- columnIndents .addFirst (column );
571- duplicatedColumnLocatorsIndicator .add (column );
576+ if (locator instanceof FixedLocator ) {
577+ if (locator .getRight () != null && !duplicatedColumnLocatorsIndicator .contains (((FixedLocator ) locator ).getColumn ())) {
578+ initialIndents .push (indentationLevel );
579+ columnIndents .push (((FixedLocator ) locator ).getColumn ());
580+ duplicatedColumnLocatorsIndicator .add (((FixedLocator ) locator ).getColumn ());
572581 }
573582 if (locator .getLeft () != null ) {
574- int locatorIndexOnStack = columnIndents .lastIndexOf (column );
583+ int locatorIndexOnStack = columnIndents .lastIndexOf ((( FixedLocator ) locator ). getColumn () );
575584 if (locatorIndexOnStack != -1 ) {
576585 columnIndents .remove (locatorIndexOnStack );
577586 initialIndents .remove (locatorIndexOnStack );
578587 } else {
579- columnIndents . pollFirst ( );
580- initialIndents . pollFirst ( );
588+ emptySafeStackPop ( columnIndents );
589+ emptySafeStackPop ( initialIndents );
581590 }
582591 }
583592 }
0 commit comments