@@ -26,6 +26,14 @@ public class CollectFeatures {
2626 public static final double MAX_CONTEXT_DIFF_THRESHOLD = 0.13 ;
2727 public static final double MAX_CONTEXT_DIFF_THRESHOLD2 = 0.50 ;
2828
29+ /** When computing child indexes, we use this value for any child list
30+ * element other than the first one. If a parent has just one X child,
31+ * we use the actual child index. If parent has two or more X children,
32+ * and we are not the first X, use CHILD_INDEX_LIST_ELEMENT. If first
33+ * of two or more X children, use actual child index.
34+ */
35+ public static final int CHILD_INDEX_LIST_ELEMENT = 1_111_111_111 ;
36+
2937 // Feature values for pair on diff lines feature
3038 public static final int NOT_PAIR = -1 ;
3139 public static final int PAIR_ON_SAME_LINE = 0 ;
@@ -253,7 +261,7 @@ public int getAlignmentCategory(TerminalNode node, Token curToken, int columnDel
253261 // at a newline, are we aligned with a prior sibling (in a list) etc...
254262 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (node , curToken );
255263 Pair <ParserRuleContext , Integer > pair =
256- earliestAncestorWithChildStartingAtCharPos (earliestLeftAncestor . getParent () , curToken );
264+ earliestAncestorWithChildStartingAtCharPos (earliestLeftAncestor , curToken );
257265 if ( pair !=null ) {
258266 int deltaFromLeftAncestor = getDeltaToAncestor (earliestLeftAncestor , pair .a );
259267 aligned = aligncat (deltaFromLeftAncestor , pair .b );
@@ -482,13 +490,13 @@ public static int[] getNodeFeatures(Map<Token, TerminalNode> tokenToNodeMap,
482490 matchingSymbolOnDiffLine ,
483491 curTokenStartsNewLine ? 1 : 0 ,
484492 rulealt (earliestLeftAncestor .getRuleIndex (),earliestLeftAncestor .getAltNumber ()),
485- rulealt (earliestLeftAncestorParent .getRuleIndex (), earliestLeftAncestorParent .getAltNumber ()),
493+ earliestLeftAncestorParent != null ? rulealt (earliestLeftAncestorParent .getRuleIndex (), earliestLeftAncestorParent .getAltNumber ()) : - 1 ,
486494 getChildIndex (earliestLeftAncestor ),
487- earliestLeftAncestorParent2 !=null ? rulealt (earliestLeftAncestorParent2 .getRuleIndex (), earliestLeftAncestorParent2 .getAltNumber ()) : 0 ,
495+ earliestLeftAncestorParent2 !=null ? rulealt (earliestLeftAncestorParent2 .getRuleIndex (), earliestLeftAncestorParent2 .getAltNumber ()) : - 1 ,
488496 getChildIndex (earliestLeftAncestorParent ),
489- earliestLeftAncestorParent3 !=null ? rulealt (earliestLeftAncestorParent3 .getRuleIndex (), earliestLeftAncestorParent3 .getAltNumber ()) : 0 ,
497+ earliestLeftAncestorParent3 !=null ? rulealt (earliestLeftAncestorParent3 .getRuleIndex (), earliestLeftAncestorParent3 .getAltNumber ()) : - 1 ,
490498 getChildIndex (earliestLeftAncestorParent2 ),
491- earliestLeftAncestorParent4 !=null ? rulealt (earliestLeftAncestorParent4 .getRuleIndex (), earliestLeftAncestorParent4 .getAltNumber ()) : 0 ,
499+ earliestLeftAncestorParent4 !=null ? rulealt (earliestLeftAncestorParent4 .getRuleIndex (), earliestLeftAncestorParent4 .getAltNumber ()) : - 1 ,
492500 getChildIndex (earliestLeftAncestorParent3 ),
493501
494502 // info
@@ -507,6 +515,7 @@ public static int getMatchingSymbolOnDiffLine(InputDocument doc,
507515 {
508516 TerminalNode matchingLeftNode = getMatchingLeftSymbol (doc , node );
509517 if (matchingLeftNode != null ) {
518+ // System.out.println(node.getPayload()+" matches with "+matchingLeftNode.getSymbol());
510519 int matchingLeftTokenLine = matchingLeftNode .getSymbol ().getLine ();
511520 return matchingLeftTokenLine != line ? PAIR_ON_DIFF_LINE : PAIR_ON_SAME_LINE ;
512521 }
@@ -559,15 +568,6 @@ public static List<Integer> viableLeftTokenTypes(ParserRuleContext node,
559568 return newPairs ;
560569 }
561570
562- public static Token findAlignedToken (List <Token > tokens , Token leftEdgeToken ) {
563- for (Token t : tokens ) {
564- if ( t .getCharPositionInLine () == leftEdgeToken .getCharPositionInLine () ) {
565- return t ;
566- }
567- }
568- return null ;
569- }
570-
571571 /** Search backwards from tokIndex into 'tokens' stream and get all on-channel
572572 * tokens on previous line with respect to token at tokIndex.
573573 * return empty list if none found. First token in returned list is
@@ -761,12 +761,20 @@ public static ParserRuleContext getParent(TerminalNode p) {
761761 return parentClosure ((ParserRuleContext )p .getParent ());
762762 }
763763
764- public static int getChildIndex (ParseTree t ) {
764+ public static int getChildIndex (ParserRuleContext t ) {
765765 if ( t ==null ) return -1 ;
766- ParseTree parent = t .getParent ();
766+ ParserRuleContext parent = t .getParent ();
767767 if ( parent ==null ) {
768768 return -1 ;
769769 }
770+ // we know we have a parent now
771+ // check to see if we are 2nd or beyond element in a sibling list
772+ List <ParserRuleContext > siblings = parent .getRuleContexts (t .getClass ());
773+ if ( siblings .size ()>1 && siblings .indexOf (t )>0 ) {
774+ return CHILD_INDEX_LIST_ELEMENT ;
775+ }
776+ // Either first of sibling list or not in a list.
777+ // Figure out which child index t is of parent
770778 for (int i = 0 ; i <parent .getChildCount (); i ++) {
771779 if ( parent .getChild (i )==t ) {
772780 return i ;
0 commit comments