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

Method getAlignmentCategory

src/org/antlr/codebuff/Trainer.java:289–373  ·  view source on GitHub ↗
(InputDocument doc, TerminalNode node, int indentSize)

Source from the content-addressed store, hash-verified

287
288 // at a newline, are we aligned with a prior sibling (in a list) etc...
289 public static int getAlignmentCategory(InputDocument doc, TerminalNode node, int indentSize) {
290 Pair<Integer,Integer> alignInfo = null;
291 Pair<Integer,Integer> indentInfo = null;
292
293 Token curToken = node.getSymbol();
294
295 // at a newline, are we aligned with a prior sibling (in a list) etc...
296 ParserRuleContext earliestLeftAncestor = earliestAncestorStartingWithToken(node);
297 Pair<ParserRuleContext, Integer> alignPair =
298 earliestAncestorWithChildStartingAtCharPos(earliestLeftAncestor, curToken, curToken.getCharPositionInLine());
299// String[] ruleNames = doc.parser.getRuleNames();
300// Token prevToken = doc.tokens.getPreviousRealToken(curToken.getTokenIndex());
301 if ( alignPair!=null ) {
302 int deltaFromLeftAncestor = getDeltaToAncestor(earliestLeftAncestor, alignPair.a);
303 alignInfo = new Pair<>(deltaFromLeftAncestor, alignPair.b);
304// int ruleIndex = pair.a.getRuleIndex();
305// System.out.printf("ALIGN %s %s i=%d %s %s\n",
306// curToken,
307// ruleNames[ruleIndex],
308// pair.b, alignInfo, doc.fileName);
309 }
310
311 // perhaps we are indented as well?
312 int tokenIndexInStream = node.getSymbol().getTokenIndex();
313 List<Token> tokensOnPreviousLine = getTokensOnPreviousLine(doc.tokens, tokenIndexInStream, curToken.getLine());
314 Token firstTokenOnPrevLine = null;
315 int columnDelta = 0;
316 if ( tokensOnPreviousLine.size()>0 ) {
317 firstTokenOnPrevLine = tokensOnPreviousLine.get(0);
318 columnDelta = curToken.getCharPositionInLine() - firstTokenOnPrevLine.getCharPositionInLine();
319 }
320
321 Pair<ParserRuleContext, Integer> indentPair = null;
322 if ( columnDelta!=0 ) {
323 int indentedFromPos = curToken.getCharPositionInLine()-indentSize;
324 indentPair = earliestAncestorWithChildStartingAtCharPos(earliestLeftAncestor, curToken, indentedFromPos);
325 if ( indentPair==null ) {
326 // try with 2 indents (commented out for now; can't encode how many indents in directive)
327// indentedFromPos = curToken.getCharPositionInLine()-2*indentSize;
328// pair = earliestAncestorWithChildStartingAtCharPos(earliestLeftAncestor, curToken, indentedFromPos);
329 }
330 if ( indentPair!=null ) {
331 int deltaFromLeftAncestor = getDeltaToAncestor(earliestLeftAncestor, indentPair.a);
332 indentInfo = new Pair<>(deltaFromLeftAncestor, indentPair.b);
333// int ruleIndex = pair.a.getRuleIndex();
334// System.out.printf("INDENT %s %s i=%d %s %s\n",
335// curToken,
336// ruleNames[ruleIndex],
337// pair.b, indentInfo, doc.fileName);
338 }
339 }
340
341 /*
342 I tried reducing all specific and alignment operations to the generic
343 "indent/align from first token on previous line" directives but
344 the contexts were not sufficiently precise. method bodies got doubly
345 indented when there is a throws clause etc... Might be worth pursuing
346 in the future if I can increase context information regarding

Callers 2

getTokenAnalysisMethod · 0.95

Calls 11

getDeltaToAncestorMethod · 0.95
aligncatMethod · 0.95
indentcatMethod · 0.95
getCharPositionInLineMethod · 0.80
getTokenIndexMethod · 0.80
sizeMethod · 0.65
getMethod · 0.65
getLineMethod · 0.45

Tested by

no test coverage detected