private void includeSelected(boolean recurse) {
   final ArrayList<PackageSet> selected = getSelectedSets(recurse);
   if (selected == null || selected.isEmpty()) return;
   for (PackageSet set : selected) {
     if (myCurrentScope == null) {
       myCurrentScope = set;
     } else if (myCurrentScope instanceof InvalidPackageSet) {
       myCurrentScope =
           StringUtil.isEmpty(myCurrentScope.getText())
               ? set
               : new UnionPackageSet(myCurrentScope, set);
     } else {
       final boolean[] append = {true};
       final PackageSet simplifiedScope =
           processComplementaryScope(myCurrentScope, set, true, append);
       if (!append[0]) {
         myCurrentScope = simplifiedScope;
       } else {
         myCurrentScope =
             simplifiedScope != null ? new UnionPackageSet(simplifiedScope, set) : set;
       }
     }
   }
   rebuild(true);
 }
  @Nullable
  static PackageSet processComplementaryScope(
      @NotNull PackageSet current, PackageSet added, boolean checkComplementSet, boolean[] append) {
    final String text = added.getText();
    if (current instanceof ComplementPackageSet
        && Comparing.strEqual(
            ((ComplementPackageSet) current).getComplementarySet().getText(), text)) {
      if (checkComplementSet) {
        append[0] = false;
      }
      return null;
    }
    if (Comparing.strEqual(current.getText(), text)) {
      if (!checkComplementSet) {
        append[0] = false;
      }
      return null;
    }

    if (current instanceof UnionPackageSet) {
      final PackageSet left =
          processComplementaryScope(
              ((UnionPackageSet) current).getFirstSet(), added, checkComplementSet, append);
      final PackageSet right =
          processComplementaryScope(
              ((UnionPackageSet) current).getSecondSet(), added, checkComplementSet, append);
      if (left == null) return right;
      if (right == null) return left;
      return new UnionPackageSet(left, right);
    }

    if (current instanceof IntersectionPackageSet) {
      final PackageSet left =
          processComplementaryScope(
              ((IntersectionPackageSet) current).getFirstSet(), added, checkComplementSet, append);
      final PackageSet right =
          processComplementaryScope(
              ((IntersectionPackageSet) current).getSecondSet(), added, checkComplementSet, append);
      if (left == null) return right;
      if (right == null) return left;
      return new IntersectionPackageSet(left, right);
    }

    return current;
  }
 public void reset(PackageSet packageSet, @Nullable Runnable runnable) {
   myCurrentScope = packageSet;
   myPatternField.setText(myCurrentScope == null ? "" : myCurrentScope.getText());
   rebuild(false, runnable, false, 0);
 }