private static TextRange calculateLimitRange(
     final PsiFile file, final Document doc, final int line) {
   final int offset = doc.getLineStartOffset(line);
   if (offset > 0) {
     PsiMethod method =
         PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiMethod.class, false);
     if (method != null) {
       final TextRange elemRange = method.getTextRange();
       return new TextRange(
           doc.getLineNumber(elemRange.getStartOffset()),
           doc.getLineNumber(elemRange.getEndOffset()));
     }
   }
   return new TextRange(0, doc.getLineCount() - 1);
 }
  private static boolean shouldSkipLine(final PsiFile file, Document doc, int line) {
    final int start = doc.getLineStartOffset(line);
    final int end = doc.getLineEndOffset(line);
    final int _start = CharArrayUtil.shiftForward(doc.getCharsSequence(), start, " \n\t");
    if (_start >= end) {
      return true;
    }

    TextRange alreadyChecked = null;
    for (PsiElement elem = file.findElementAt(_start);
        elem != null
            && elem.getTextOffset() <= end
            && (alreadyChecked == null || !alreadyChecked.contains(elem.getTextRange()));
        elem = elem.getNextSibling()) {
      for (PsiElement _elem = elem; _elem.getTextOffset() >= _start; _elem = _elem.getParent()) {
        alreadyChecked = _elem.getTextRange();

        if (_elem instanceof PsiDeclarationStatement) {
          final PsiElement[] declared = ((PsiDeclarationStatement) _elem).getDeclaredElements();
          for (PsiElement declaredElement : declared) {
            if (declaredElement instanceof PsiVariable) {
              return false;
            }
          }
        }

        if (_elem instanceof PsiJavaCodeReferenceElement) {
          final PsiElement resolved = ((PsiJavaCodeReferenceElement) _elem).resolve();
          if (resolved instanceof PsiVariable) {
            return false;
          }
        }
      }
    }
    return true;
  }
  private static Pair<Set<String>, Set<TextWithImports>> findReferencedVars(
      Set<String> visibleVars, SourcePosition position) {
    final int line = position.getLine();
    if (line < 0) {
      return Pair.create(Collections.<String>emptySet(), Collections.<TextWithImports>emptySet());
    }
    final PsiFile positionFile = position.getFile();
    if (!positionFile.getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
      return Pair.create(visibleVars, Collections.<TextWithImports>emptySet());
    }

    final VirtualFile vFile = positionFile.getVirtualFile();
    final Document doc =
        vFile != null ? FileDocumentManager.getInstance().getDocument(vFile) : null;
    if (doc == null || doc.getLineCount() == 0 || line > (doc.getLineCount() - 1)) {
      return Pair.create(Collections.<String>emptySet(), Collections.<TextWithImports>emptySet());
    }

    final TextRange limit = calculateLimitRange(positionFile, doc, line);

    int startLine = Math.max(limit.getStartOffset(), line - 1);
    startLine = Math.min(startLine, limit.getEndOffset());
    while (startLine > limit.getStartOffset() && shouldSkipLine(positionFile, doc, startLine)) {
      startLine--;
    }
    final int startOffset = doc.getLineStartOffset(startLine);

    int endLine = Math.min(line + 2, limit.getEndOffset());
    while (endLine < limit.getEndOffset() && shouldSkipLine(positionFile, doc, endLine)) {
      endLine++;
    }
    final int endOffset = doc.getLineEndOffset(endLine);

    final TextRange lineRange = new TextRange(startOffset, endOffset);
    if (!lineRange.isEmpty()) {
      final int offset =
          CharArrayUtil.shiftForward(doc.getCharsSequence(), doc.getLineStartOffset(line), " \t");
      PsiElement element = positionFile.findElementAt(offset);
      if (element != null) {
        PsiMethod method = PsiTreeUtil.getNonStrictParentOfType(element, PsiMethod.class);
        if (method != null) {
          element = method;
        } else {
          PsiField field = PsiTreeUtil.getNonStrictParentOfType(element, PsiField.class);
          if (field != null) {
            element = field;
          } else {
            final PsiClassInitializer initializer =
                PsiTreeUtil.getNonStrictParentOfType(element, PsiClassInitializer.class);
            if (initializer != null) {
              element = initializer;
            }
          }
        }

        //noinspection unchecked
        if (element instanceof PsiCompiledElement) {
          return Pair.create(visibleVars, Collections.<TextWithImports>emptySet());
        } else {
          VariablesCollector collector =
              new VariablesCollector(visibleVars, adjustRange(element, lineRange));
          element.accept(collector);
          return Pair.create(collector.getVars(), collector.getExpressions());
        }
      }
    }
    return Pair.create(Collections.<String>emptySet(), Collections.<TextWithImports>emptySet());
  }