int (final List<? extends IElementType> tokens, final boolean atStreamEdge, final TokenTextGetter getter) { if (tokens.isEmpty()) return 0; // 1. bind doc comment for (int idx = tokens.size() - 1; idx >= 0; idx--) { if (BasicJavaAstTreeUtil.is(tokens.get(idx), BASIC_DOC_COMMENT)) return idx; } // 2. bind plain comments int result = tokens.size(); for (int idx = tokens.size() - 1; idx >= 0; idx--) { final IElementType tokenType = tokens.get(idx); if (TokenSet.WHITE_SPACE.contains(tokenType)) { if (StringUtil.getLineBreakCount(getter.get(idx)) > 1) break; } else if (BASIC_JAVA_PLAIN_COMMENT_BIT_SET.contains(tokenType)) { if (atStreamEdge || (idx == 0 && myAfterEmptyImport) || (idx > 0 && TokenSet.WHITE_SPACE.contains(tokens.get(idx - 1)) && StringUtil.containsLineBreak(getter.get(idx - 1)))) { result = idx; } } else { break; } } return result; }