66import org .antlr .v4 .runtime .Token ;
77import org .antlr .v4 .runtime .WritableToken ;
88import org .antlr .v4 .runtime .misc .Interval ;
9+ import org .antlr .v4 .runtime .tree .ParseTree ;
910import org .antlr .v4 .runtime .tree .TerminalNode ;
1011
1112import java .util .List ;
1213import java .util .Map ;
1314import java .util .Vector ;
1415
15- import static org .antlr .codebuff .CollectFeatures .CAT_ALIGN_WITH_ANCESTORS_PARENT_FIRST_TOKEN ;
16- import static org .antlr .codebuff .CollectFeatures .CAT_ALIGN_WITH_ANCESTOR_FIRST_TOKEN ;
17- import static org .antlr .codebuff .CollectFeatures .CAT_ALIGN_WITH_LIST_FIRST_ELEMENT ;
18- import static org .antlr .codebuff .CollectFeatures .CAT_ALIGN_WITH_PAIR ;
16+ import static org .antlr .codebuff .CollectFeatures .CAT_ALIGN_WITH_ANCESTOR_CHILD ;
1917import static org .antlr .codebuff .CollectFeatures .CAT_INDENT ;
2018import static org .antlr .codebuff .CollectFeatures .CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ;
2119import static org .antlr .codebuff .CollectFeatures .CAT_NO_ALIGNMENT ;
2523import static org .antlr .codebuff .CollectFeatures .INDEX_FIRST_ON_LINE ;
2624import static org .antlr .codebuff .CollectFeatures .INDEX_PREV_END_COLUMN ;
2725import static org .antlr .codebuff .CollectFeatures .MAX_CONTEXT_DIFF_THRESHOLD ;
28- import static org .antlr .codebuff .CollectFeatures .earliestAncestorEndingWithToken ;
2926import static org .antlr .codebuff .CollectFeatures .earliestAncestorStartingWithToken ;
30- import static org .antlr .codebuff .CollectFeatures .getListSiblings ;
31- import static org .antlr .codebuff .CollectFeatures .getMatchingLeftSymbol ;
3227import static org .antlr .codebuff .CollectFeatures .getNodeFeatures ;
3328import static org .antlr .codebuff .CollectFeatures .getRealTokens ;
3429import static org .antlr .codebuff .CollectFeatures .getTokensOnPreviousLine ;
3530import static org .antlr .codebuff .CollectFeatures .indexTree ;
36- import static org .antlr .codebuff .CollectFeatures .isAlignedWithFirstSiblingOfList ;
3731
3832public class Formatter {
3933 public static final int INDENT_LEVEL = 4 ;
@@ -75,6 +69,7 @@ public Formatter(Corpus corpus, InputDocument doc, int tabSize) {
7569 wsClassifier = new CodekNNClassifier (corpus , FEATURES_INJECT_WS );
7670 alignClassifier = new CodekNNClassifier (corpus , FEATURES_ALIGN );
7771// k = (int)Math.sqrt(corpus.X.size());
72+ // k = 7;
7873 k = 11 ;
7974 this .tabSize = tabSize ;
8075 }
@@ -154,7 +149,6 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
154149
155150 TerminalNode node = tokenToNodeMap .get (curToken );
156151 ParserRuleContext parent = (ParserRuleContext )node .getParent ();
157- ParserRuleContext earliestRightAncestor = earliestAncestorEndingWithToken (parent , curToken );
158152
159153 switch ( align ) {
160154 case CAT_INDENT :
@@ -164,47 +158,43 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
164158 output .append (Tool .spaces (indentedCol ));
165159 }
166160 break ;
167- case CAT_ALIGN_WITH_ANCESTOR_FIRST_TOKEN :
168- if ( earliestRightAncestor !=null ) {
169- Token earliestRightAncestorStart = earliestRightAncestor .getStart ();
170- int linedUpCol = earliestRightAncestorStart .getCharPositionInLine ();
171- charPosInLine = linedUpCol ;
172- output .append (Tool .spaces (linedUpCol ));
173- }
174- break ;
175- case CAT_ALIGN_WITH_ANCESTORS_PARENT_FIRST_TOKEN :
176- if ( earliestRightAncestor !=null ) {
177- ParserRuleContext earliestAncestorParent = earliestRightAncestor .getParent ();
178- if ( earliestAncestorParent !=null ) {
179- Token earliestAncestorParentStart = earliestAncestorParent .getStart ();
180- int linedUpCol = earliestAncestorParentStart .getCharPositionInLine ();
181- charPosInLine = linedUpCol ;
182- output .append (Tool .spaces (linedUpCol ));
183- }
184- }
185- break ;
186- case CAT_ALIGN_WITH_LIST_FIRST_ELEMENT :
187- List <ParserRuleContext > listSiblings = getListSiblings (tokenToNodeMap , curToken );
188- if ( listSiblings !=null ) {
189- ParserRuleContext firstSibling = listSiblings .get (0 );
190- int linedUpCol = firstSibling .getStart ().getCharPositionInLine ();
191- charPosInLine = linedUpCol ;
192- output .append (Tool .spaces (linedUpCol ));
193- }
194- break ;
195- case CAT_ALIGN_WITH_PAIR :
196- TerminalNode matchingLeftSymbol = getMatchingLeftSymbol (doc , node );
197- int linedUpCol = matchingLeftSymbol .getSymbol ().getCharPositionInLine ();
198- charPosInLine = linedUpCol ;
199- output .append (Tool .spaces (linedUpCol ));
200- break ;
201161 case CAT_NO_ALIGNMENT :
202162 break ;
203163
204164 default :
205- if ( align >=CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
206- int deltaFromAncestor = align - CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ;
165+ if ( (align &0xFF )==CAT_ALIGN_WITH_ANCESTOR_CHILD ) {
166+ int [] deltaChild = CollectFeatures .unaligncat (align );
167+ int deltaFromAncestor = deltaChild [0 ];
168+ int childIndex = deltaChild [1 ];
207169 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (parent , curToken );
170+ if ( earliestLeftAncestor ==null ) {
171+ earliestLeftAncestor = parent ;
172+ }
173+ ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
174+ ParseTree child = ancestor .getChild (childIndex );
175+ Token start = null ;
176+ if ( child instanceof ParserRuleContext ) {
177+ start = ((ParserRuleContext ) child ).getStart ();
178+ }
179+ else if ( child instanceof TerminalNode ){
180+ start = ((TerminalNode )child ).getSymbol ();
181+ }
182+ else {
183+ // uh oh.
184+ System .err .println ("Whoops. Tried access invalid child" );
185+ }
186+ if ( start !=null ) {
187+ int indentCol = start .getCharPositionInLine ();
188+ charPosInLine = indentCol ;
189+ output .append (Tool .spaces (indentCol ));
190+ }
191+ }
192+ else if ( (align &0xFF )==CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN ) {
193+ int deltaFromAncestor = CollectFeatures .unindentcat (align );
194+ ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken (parent , curToken );
195+ if ( earliestLeftAncestor ==null ) {
196+ earliestLeftAncestor = parent ;
197+ }
208198 ParserRuleContext ancestor = CollectFeatures .getAncestor (earliestLeftAncestor , deltaFromAncestor );
209199 Token start = ancestor .getStart ();
210200 int indentCol = start .getCharPositionInLine () + INDENT_LEVEL ;
@@ -251,13 +241,12 @@ public TokenPositionAnalysis getTokenAnalysis(int[] features, int indexIntoRealT
251241 boolean prevIsWS = prevToken .getType ()==JavaLexer .WS ;
252242 int actualNL = Tool .count (prevToken .getText (), '\n' );
253243 int actualWS = Tool .count (prevToken .getText (), ' ' );
254- boolean actualAlign = isAlignedWithFirstSiblingOfList (tokenToNodeMap , tokens , curToken );
255244 String newlinePredictionString = String .format ("### line %d: predicted %d \\ n actual %s" ,
256245 originalCurToken .getLine (), injectNewline , prevIsWS ? actualNL : "none" );
257246 String alignPredictionString = String .format ("### line %d: predicted %s actual %s" ,
258247 originalCurToken .getLine (),
259248 alignWithPrevious ==1 ?"align" :"unaligned" ,
260- actualAlign ? "align" : "unaligned " );
249+ "? " );
261250 String wsPredictionString = String .format ("### line %d: predicted %d ' ' actual %s" ,
262251 originalCurToken .getLine (), ws , prevIsWS ? actualWS : "none" );
263252 if ( failsafeTriggered ) {
0 commit comments