private static Collection<GrVariable> collectUsedLocalVarsOrParamsDeclaredOutside(ExtractInfoHelper helper) {
    final Collection<GrVariable> result = new HashSet<>();

    final TextRange range = getRangeOfRefactoring(helper);
    final int start = range.getStartOffset();
    final int end = range.getEndOffset();

    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
      @Override
      public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
        final PsiElement resolved = ref.resolve();
        if ((resolved instanceof GrParameter || PsiUtil.isLocalVariable(resolved)) && resolved.isPhysical()) {
          final int offset = resolved.getTextRange().getStartOffset();
          //var is declared outside of selected code
          if (offset < start || end <= offset) {
            result.add((GrVariable)resolved);
          }
        }
      }
    };

    final GrStatement[] statements = helper.getStatements();
    for (GrStatement statement : statements) {
      statement.accept(visitor);
    }

    return result;
  }
private static Collection<GrVariable> collectUsedLocalVarsOrParamsDeclaredOutside(ExtractInfoHelper helper)