MCPcopy Index your code
hub / github.com/antlr/codebuff / indent

Method indent

src/org/antlr/codebuff/Formatter.java:266–314  ·  view source on GitHub ↗
(int indentCat, TerminalNode node)

Source from the content-addressed store, hash-verified

264 }
265
266 public void indent(int indentCat, TerminalNode node) {
267 int tokenIndexInStream = node.getSymbol().getTokenIndex();
268 List<Token> tokensOnPreviousLine = getTokensOnPreviousLine(testDoc.tokens, tokenIndexInStream, line);
269 Token firstTokenOnPrevLine = null;
270 if ( tokensOnPreviousLine.size()>0 ) {
271 firstTokenOnPrevLine = tokensOnPreviousLine.get(0);
272 }
273
274 if ( indentCat==CAT_INDENT ) {
275 if ( firstTokenOnPrevLine!=null ) { // if not on first line, we cannot indent
276 int indentedCol = firstTokenOnPrevLine.getCharPositionInLine()+indentSize;
277 charPosInLine = indentedCol;
278 output.append(Tool.spaces(indentedCol));
279 }
280 else {
281 // no prev token? ok, just indent from left edge
282 charPosInLine = indentSize;
283 output.append(Tool.spaces(indentSize));
284 }
285 return;
286 }
287 int[] deltaChild = Trainer.unindentcat(indentCat);
288 int deltaFromAncestor = deltaChild[0];
289 int childIndex = deltaChild[1];
290 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken(node);
291 ParserRuleContext ancestor = Trainer.getAncestor(earliestLeftAncestor, deltaFromAncestor);
292 Token start = null;
293 if ( ancestor==null ) {
294// System.err.println("Whoops. No ancestor at that delta");
295 }
296 else {
297 ParseTree child = ancestor.getChild(childIndex);
298 if ( child instanceof ParserRuleContext ) {
299 start = ((ParserRuleContext) child).getStart();
300 }
301 else if ( child instanceof TerminalNode ) {
302 start = ((TerminalNode) child).getSymbol();
303 }
304 else {
305 // uh oh.
306// System.err.println("Whoops. Tried to access invalid child");
307 }
308 }
309 if ( start!=null ) {
310 int indentCol = start.getCharPositionInLine()+indentSize;
311 charPosInLine = indentCol;
312 output.append(Tool.spaces(indentCol));
313 }
314 }
315
316 public void align(int alignOrIndent, TerminalNode node) {
317 int[] deltaChild = Trainer.triple(alignOrIndent);

Callers 1

processTokenMethod · 0.95

Calls 11

spacesMethod · 0.95
unindentcatMethod · 0.95
getAncestorMethod · 0.95
getTokenIndexMethod · 0.80
getCharPositionInLineMethod · 0.80
sizeMethod · 0.65
getMethod · 0.65
appendMethod · 0.45
getChildMethod · 0.45

Tested by

no test coverage detected