private static void trimCommaIfNeeded(Document document, PsiFile file, int line) {
    int offset = document.getLineEndOffset(line);
    if (doTrimComma(document, offset + 1, offset)) return;

    PsiElement element = file.findElementAt(offset - 1);
    int forward = getForwardLineNumber(document, element);
    int backward = getBackwardLineNumber(document, element);
    if (forward < 0 || backward < 0) return;
    doTrimComma(document, document.getLineEndOffset(forward) - 1, document.getLineEndOffset(backward));
  }
private static void trimCommaIfNeeded(Document document, PsiFile file, int line)