|
24 | 24 | import java.util.Map; |
25 | 25 |
|
26 | 26 | public class CollectFeatures { |
| 27 | + public static final double MAX_CONTEXT_DIFF_THRESHOLD = 0.6; |
| 28 | + |
27 | 29 | // Feature values for pair on diff lines feature |
28 | 30 | public static final int NOT_PAIR = -1; |
29 | 31 | public static final int PAIR_ON_SAME_LINE = 0; |
30 | 32 | public static final int PAIR_ON_DIFF_LINE = 1; |
31 | 33 |
|
32 | 34 | // Categories for alignment/indentation |
33 | 35 | public static final int CAT_NO_ALIGNMENT = 0; |
34 | | - public static final int CAT_INDENT = 1; |
35 | | - public static final int CAT_ALIGN_WITH_ANCESTOR_FIRST_TOKEN = 2; |
36 | | - public static final int CAT_ALIGN_WITH_ANCESTORS_PARENT_FIRST_TOKEN = 3; |
37 | | - public static final int CAT_ALIGN_WITH_LIST_FIRST_ELEMENT = 4; |
38 | | - public static final int CAT_ALIGN_WITH_PAIR = 5; |
| 36 | + public static final int CAT_ALIGN_WITH_ANCESTOR_FIRST_TOKEN = 1; |
| 37 | + public static final int CAT_ALIGN_WITH_ANCESTORS_PARENT_FIRST_TOKEN = 2; |
| 38 | + public static final int CAT_ALIGN_WITH_LIST_FIRST_ELEMENT = 3; |
| 39 | + public static final int CAT_ALIGN_WITH_PAIR = 4; |
39 | 40 |
|
40 | | - public static final double MAX_CONTEXT_DIFF_THRESHOLD = 0.6; |
| 41 | + public static final int CAT_INDENT = 100; |
41 | 42 |
|
42 | 43 | public static final int INDEX_PREV2_TYPE = 0; |
43 | 44 | public static final int INDEX_PREV_TYPE = 1; |
44 | 45 | public static final int INDEX_PREV_RULE = 2; // what rule is prev token in? |
45 | 46 | public static final int INDEX_PREV_END_COLUMN = 3; |
46 | 47 | public static final int INDEX_PREV_EARLIEST_ANCESTOR = 4; |
47 | 48 | public static final int INDEX_TYPE = 5; |
48 | | - public static final int INDEX_FIRST_EL_OF_LIST = 6; |
| 49 | + public static final int INDEX_FIRST_EL_OF_LIST = 6; // TODO: I don't think we can detect first element of list |
49 | 50 | public static final int INDEX_MATCHING_TOKEN_DIFF_LINE = 7; |
50 | 51 | public static final int INDEX_FIRST_ON_LINE = 8; // a \n right before this token? |
51 | 52 | public static final int INDEX_RULE = 9; // what rule are we in? |
@@ -475,6 +476,7 @@ public static int[] getNodeFeatures(Map<Token, TerminalNode> tokenToNodeMap, |
475 | 476 | sumEndColAndAncestorWidth = prevTokenEndCharPos+earliestAncestorWidth; |
476 | 477 | } |
477 | 478 |
|
| 479 | + // TODO: I don't think we can detect first element of list |
478 | 480 | boolean startOfList = isFirstSiblingOfList(tokenToNodeMap, curToken); |
479 | 481 |
|
480 | 482 | boolean curTokenStartsNewLine = window.get(2).getLine()>window.get(1).getLine(); |
@@ -779,4 +781,18 @@ public static List<Token> getRealTokens(CommonTokenStream tokens) { |
779 | 781 | } |
780 | 782 | return real; |
781 | 783 | } |
| 784 | + |
| 785 | + /** Same as p.getParent() except we scan through chain rule nodes */ |
| 786 | + public static ParserRuleContext getParent(ParserRuleContext p) { |
| 787 | + if ( p==null ) return null; |
| 788 | + ParserRuleContext lastValidParent = p.getParent(); |
| 789 | + // now try to walk chain rules starting with the parent of the usual parent |
| 790 | + ParserRuleContext q = lastValidParent.getParent(); |
| 791 | + while ( q!=null && q.getChildCount()==1 ) { // while is a chain rule |
| 792 | + lastValidParent = q; |
| 793 | + q = q.getParent(); |
| 794 | + } |
| 795 | + return lastValidParent; |
| 796 | + } |
| 797 | + |
782 | 798 | } |
0 commit comments