Skip to content

Commit ec327d1

Browse files
committed
allow tokens to align on non-first-on-line tokens.
1 parent 206e937 commit ec327d1

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

java/src/org/antlr/codebuff/CollectFeatures.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,11 @@ public int getAlignmentCategory(TerminalNode node, Token curToken, int columnDel
288288
if ( pair!=null ) {
289289
int deltaFromLeftAncestor = getDeltaToAncestor(earliestLeftAncestor, pair.a);
290290
aligned = aligncat(deltaFromLeftAncestor, pair.b);
291-
// System.out.printf("ALIGN %s %d %x\n", JavaParser.ruleNames[pair.a.getRuleIndex()], pair.b, aligned);
292-
}
291+
// System.out.printf("ALIGN %s %s %d %x %s\n",
292+
// curToken,
293+
// doc.parser.getRuleNames()[pair.a.getRuleIndex()],
294+
// pair.b, aligned, doc.fileName);
295+
}
293296
else if ( columnDelta!=0 ) {
294297
int indentedFromPos = curToken.getCharPositionInLine()-Formatter.INDENT_LEVEL;
295298
ParserRuleContext indentParent =
@@ -368,18 +371,16 @@ public Pair<ParserRuleContext,Integer> earliestAncestorWithChildStartingAtCharPo
368371
// check all children of p to see if one of them starts at charpos
369372
for (int i = 0; i<p.getChildCount(); i++) {
370373
ParseTree child = p.getChild(i);
374+
Token start;
371375
if ( child instanceof ParserRuleContext ) {
372-
ParserRuleContext c = (ParserRuleContext)child;
373-
if ( c.getStart().getTokenIndex()<t.getTokenIndex() && isFirstOnLine(c.getStart()) && c.getStart().getCharPositionInLine()==charpos ) {
374-
return new Pair<>(p,i);
375-
}
376+
start = ((ParserRuleContext) child).getStart();
376377
}
377378
else { // must be token
378-
TerminalNode c = (TerminalNode)child;
379-
// check that we aren't aligned with self or element *after* us
380-
if ( c.getSymbol().getTokenIndex()<t.getTokenIndex() && isFirstOnLine(c.getSymbol()) && c.getSymbol().getCharPositionInLine()==charpos ) {
381-
return new Pair<>(p,i);
382-
}
379+
start = ((TerminalNode)child).getSymbol();
380+
}
381+
// check that we aren't aligned with self or element *after* us
382+
if ( start.getTokenIndex()<t.getTokenIndex() && start.getCharPositionInLine()==charpos ) {
383+
return new Pair<>(p,i);
383384
}
384385
}
385386
p = p.getParent();

java/src/org/antlr/codebuff/Tool.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
*
3131
* Testing:
3232
*
33-
* Tool -antlr ../grammars /Users/parrt/antlr/code/grammars-v4/clojure/Clojure.g4
33+
* Tool -antlr ../grammars /Users/parrt/antlr/code/grammars-v4/clojure/Clojure.g4
34+
* Tool -java ../samples/stringtemplate4 src/org/antlr/codebuff/Tool.java
3435
*/
3536
public class Tool {
3637
public static boolean showFileNames = false;

0 commit comments

Comments
 (0)