/**
   * Asks current builder to wrap one more remaining {@link Block code block} (if any).
   *
   * @return <code>true</code> if all blocks are wrapped; <code>false</code> otherwise
   */
  public boolean iteration() {
    if (myStates.isEmpty()) {
      return true;
    }

    State state = myStates.peek();
    doIteration(state);
    return myStates.isEmpty();
  }
  final void restoreValue(boolean pushed) {
    if (pushed) {
      --myLevel;
      if (myValueStack != null && !myValueStack.isEmpty()) {
        myValueStack.pop();
      }
    }

    if (myValueStack != null) {
      myValue = myValueStack.isEmpty() ? null : myValueStack.peek();
    }
  }
Example #3
0
 @Override
 public void emptyStack() {
   myCachedHash = null;
   while (!myStack.isEmpty() && !(myStack.peek() instanceof DfaControlTransferValue)) {
     myStack.pop();
   }
 }
  @Nullable
  public Resource getResource(String s, boolean flag) {
    final long started = myDebugTime ? System.nanoTime() : 0;

    try {
      int i;
      if (myCanUseCache) {
        Resource prevResource =
            myCache.iterateLoaders(s, flag ? checkedIterator : uncheckedIterator, s, this);
        if (prevResource != null) return prevResource;

        synchronized (myUrls) {
          if (myUrls.isEmpty()) return null;
        }

        i = myLoaders.size();
      } else {
        i = 0;
      }

      for (Loader loader; (loader = getLoader(i)) != null; i++) {
        if (myCanUseCache) {
          if (!myCache.loaderHasName(s, loader)) continue;
        }
        Resource resource = loader.getResource(s, flag);
        if (resource != null) {
          return resource;
        }
      }

      return null;
    } finally {
      if (myDebugTime) reportTime(started, s);
    }
  }
Example #5
0
  @Nullable
  public Resource getResource(String s, boolean flag) {
    final long started = startTiming();
    try {
      int i;
      if (myCanUseCache) {
        Resource prevResource =
            myCache.iterateLoaders(s, flag ? ourCheckedIterator : ourUncheckedIterator, s, this);
        if (prevResource != null) return prevResource;

        synchronized (myUrls) {
          if (myUrls.isEmpty()) return null;
        }

        i = myLoaders.size();
      } else {
        i = 0;
      }

      Loader loader;
      while ((loader = getLoader(i++)) != null) {
        if (myCanUseCache) {
          if (!myCache.loaderHasName(s, loader)) continue;
        }
        Resource resource = loader.getResource(s, flag);
        if (resource != null) {
          return resource;
        }
      }
    } finally {
      logTiming(this, started, s);
    }

    return null;
  }
Example #6
0
  @Nullable
  private synchronized Loader getLoader(int i) {
    while (myLoaders.size() < i + 1) {
      boolean lastOne;
      URL url;
      synchronized (myUrls) {
        if (myUrls.empty()) {
          if (myCanUseCache) myCache.nameSymbolsLoaded();
          return null;
        }
        url = myUrls.pop();
        lastOne = myUrls.isEmpty();
      }

      if (myLoadersMap.containsKey(url)) continue;

      Loader loader;
      try {
        loader = getLoader(url, myLoaders.size());
        if (loader == null) continue;
      } catch (IOException ioexception) {
        continue;
      }

      myLoaders.add(loader);
      myLoadersMap.put(url, loader);
      if (lastOne && myCanUseCache) {
        myCache.nameSymbolsLoaded();
      }
    }

    return myLoaders.get(i);
  }
Example #7
0
  public static String parseDiagnosedRanges(String text, List<DiagnosedRange> result) {
    Matcher matcher = RANGE_START_OR_END_PATTERN.matcher(text);

    Stack<DiagnosedRange> opened = new Stack<DiagnosedRange>();

    int offsetCompensation = 0;

    while (matcher.find()) {
      int effectiveOffset = matcher.start() - offsetCompensation;
      String matchedText = matcher.group();
      if ("<!>".equals(matchedText)) {
        opened.pop().setEnd(effectiveOffset);
      } else {
        Matcher diagnosticTypeMatcher = INDIVIDUAL_DIAGNOSTIC_PATTERN.matcher(matchedText);
        DiagnosedRange range = new DiagnosedRange(effectiveOffset);
        while (diagnosticTypeMatcher.find()) {
          range.addDiagnostic(diagnosticTypeMatcher.group());
        }
        opened.push(range);
        result.add(range);
      }
      offsetCompensation += matchedText.length();
    }

    assert opened.isEmpty() : "Stack is not empty";

    matcher.reset();
    return matcher.replaceAll("");
  }
Example #8
0
 @Override
 public synchronized void popState() {
   LOG.assertTrue(!myTextStack.isEmpty());
   String oldText = myTextStack.pop();
   double oldFraction = myFractionStack.remove(myFractionStack.size() - 1);
   String oldText2 = myText2Stack.pop();
   setText(oldText);
   setFraction(oldFraction);
   setText2(oldText2);
 }
 private void eofCleanup() {
   if (markingBlock) {
     blockMarker.error(message("ss.parsing.unclosed.statement", blockTokenText));
     markingBlock = false;
   }
   if (markingComment) {
     commentMarker.drop();
   }
   while (!statementsStack.isEmpty()) {
     statementsStack.pop().drop();
   }
 }
  @Override
  public final void action() throws Exception {
    if (LOG.isDebugEnabled()) {
      LOG.debug("trying " + this);
    }

    final SuspendContextImpl suspendContext = getSuspendContext();
    if (suspendContext == null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("skip processing - context is null " + this);
      }
      notifyCancelled();
      return;
    }

    if (suspendContext.myInProgress) {
      suspendContext.postponeCommand(this);
    } else {
      try {
        if (!suspendContext.isResumed()) {
          suspendContext.myInProgress = true;
          contextAction(suspendContext);
        } else {
          notifyCancelled();
        }
      } finally {
        suspendContext.myInProgress = false;
        if (suspendContext.isResumed()) {
          for (SuspendContextCommandImpl postponed = suspendContext.pollPostponedCommand();
              postponed != null;
              postponed = suspendContext.pollPostponedCommand()) {
            postponed.notifyCancelled();
          }
        } else {
          SuspendContextCommandImpl postponed = suspendContext.pollPostponedCommand();
          if (postponed != null) {
            final Stack<SuspendContextCommandImpl> stack = new Stack<>();
            while (postponed != null) {
              stack.push(postponed);
              postponed = suspendContext.pollPostponedCommand();
            }
            final DebuggerManagerThreadImpl managerThread =
                suspendContext.getDebugProcess().getManagerThread();
            while (!stack.isEmpty()) {
              managerThread.pushBack(stack.pop());
            }
          }
        }
      }
    }
  }
  private void setStructureViewSelection(@NotNull final String propertyName) {
    if (myStructureViewComponent.isDisposed()) {
      return;
    }
    JTree tree = myStructureViewComponent.getTree();
    if (tree == null) {
      return;
    }

    Object root = tree.getModel().getRoot();
    if (AbstractTreeUi.isLoadingChildrenFor(root)) {
      mySelectionChangeAlarm.cancelAllRequests();
      mySelectionChangeAlarm.addRequest(
          new Runnable() {
            @Override
            public void run() {
              mySelectionChangeAlarm.cancelAllRequests();
              setStructureViewSelection(propertyName);
            }
          },
          500);
      return;
    }

    Stack<TreeElement> toCheck = ContainerUtilRt.newStack();
    toCheck.push(myStructureViewComponent.getTreeModel().getRoot());

    while (!toCheck.isEmpty()) {
      TreeElement element = toCheck.pop();
      PsiElement value =
          element instanceof ResourceBundlePropertyStructureViewElement
              ? ((ResourceBundlePropertyStructureViewElement) element).getValue()
              : null;
      if (value instanceof IProperty
          && propertyName.equals(((IProperty) value).getUnescapedKey())) {
        myStructureViewComponent.select(value, true);
        selectionChanged();
        return;
      } else {
        for (TreeElement treeElement : element.getChildren()) {
          toCheck.push(treeElement);
        }
      }
    }
  }
  private void doIteration(@NotNull State state) {
    List<Block> subBlocks = state.parentBlock.getSubBlocks();
    final int subBlocksCount = subBlocks.size();
    int childBlockIndex = state.getIndexOfChildBlockToProcess();
    final Block block = subBlocks.get(childBlockIndex);
    if (state.previousBlock != null
        || (myCurrentWhiteSpace != null && myCurrentWhiteSpace.isIsFirstWhiteSpace())) {
      myCurrentSpaceProperty =
          (SpacingImpl) state.parentBlock.getSpacing(state.previousBlock, block);
    }

    boolean childBlockIsRightBlock = false;

    if (childBlockIndex == subBlocksCount - 1 && state.parentBlockIsRightBlock) {
      childBlockIsRightBlock = true;
    }

    final AbstractBlockWrapper wrapper =
        buildFrom(
            block,
            childBlockIndex,
            state.wrappedBlock,
            state.parentBlockWrap,
            state.parentBlock,
            childBlockIsRightBlock);
    registerExpandableIndents(block, wrapper);

    if (wrapper.getIndent() == null) {
      wrapper.setIndent((IndentImpl) block.getIndent());
    }
    if (!state.readOnly) {
      try {
        subBlocks.set(childBlockIndex, null); // to prevent extra strong refs during model building
      } catch (Throwable ex) {
        // read-only blocks
      }
    }

    if (state.childBlockProcessed(block, wrapper)) {
      while (!myStates.isEmpty() && myStates.peek().isProcessed()) {
        myStates.pop();
      }
    }
  }
Example #13
0
  @SuppressWarnings("HardCodedStringLiteral")
  public String toString() {
    StringBuilder result = new StringBuilder();
    result.append('<');
    if (myEphemeral) {
      result.append("ephemeral, ");
    }

    for (EqClass set : getNonTrivialEqClasses()) {
      result.append(set);
    }

    if (!myDistinctClasses.isEmpty()) {
      result.append("\n  distincts: ");
      List<String> distincts = new ArrayList<>();
      for (UnorderedPair<EqClass> pair : getDistinctClassPairs()) {
        distincts.add("{" + pair.first + ", " + pair.second + "}");
      }
      Collections.sort(distincts);
      result.append(StringUtil.join(distincts, " "));
    }

    if (!myStack.isEmpty()) {
      result.append("\n  stack: ").append(StringUtil.join(myStack, ","));
    }
    if (!myVariableStates.isEmpty()) {
      result.append("\n  vars: ");
      for (Map.Entry<DfaVariableValue, DfaVariableState> entry : myVariableStates.entrySet()) {
        result
            .append("[")
            .append(entry.getKey())
            .append("->")
            .append(entry.getValue())
            .append("] ");
      }
    }
    if (!myUnknownVariables.isEmpty()) {
      result.append("\n  unknowns: ").append(new HashSet<>(myUnknownVariables));
    }
    result.append('>');
    return result.toString();
  }
Example #14
0
  private static void divideInsideAndOutside(
      @NotNull PsiFile root,
      int startOffset,
      int endOffset,
      @NotNull TextRange range,
      @NotNull List<PsiElement> inside,
      @NotNull List<PsiElement> outside,
      boolean includeParents) {
    final int currentOffset = root.getTextRange().getStartOffset();
    final Condition<PsiElement>[] filters = Extensions.getExtensions(CollectHighlightsUtil.EP_NAME);

    int offset = currentOffset;

    final TIntStack starts = new TIntStack(STARTING_TREE_HEIGHT);
    starts.push(startOffset);
    final Stack<PsiElement> elements = new Stack<PsiElement>(STARTING_TREE_HEIGHT);
    final Stack<PsiElement> children = new Stack<PsiElement>(STARTING_TREE_HEIGHT);
    PsiElement element = root;

    PsiElement child = PsiUtilBase.NULL_PSI_ELEMENT;
    while (true) {
      ProgressManager.checkCanceled();

      for (Condition<PsiElement> filter : filters) {
        if (!filter.value(element)) {
          assert child == PsiUtilBase.NULL_PSI_ELEMENT;
          child = null; // do not want to process children
          break;
        }
      }

      boolean startChildrenVisiting;
      if (child == PsiUtilBase.NULL_PSI_ELEMENT) {
        startChildrenVisiting = true;
        child = element.getFirstChild();
      } else {
        startChildrenVisiting = false;
      }

      if (child == null) {
        if (startChildrenVisiting) {
          // leaf element
          offset += element.getTextLength();
        }

        int start = starts.pop();
        if (startOffset <= start && offset <= endOffset) {
          if (range.containsRange(start, offset)) {
            inside.add(element);
          } else {
            outside.add(element);
          }
        }

        if (elements.isEmpty()) break;
        element = elements.pop();
        child = children.pop();
      } else {
        // composite element
        if (offset > endOffset) break;
        children.push(child.getNextSibling());
        starts.push(offset);
        elements.push(element);
        element = child;
        child = PsiUtilBase.NULL_PSI_ELEMENT;
      }
    }

    if (includeParents) {
      PsiElement parent =
          !outside.isEmpty()
              ? outside.get(outside.size() - 1)
              : !inside.isEmpty()
                  ? inside.get(inside.size() - 1)
                  : CollectHighlightsUtil.findCommonParent(root, startOffset, endOffset);
      while (parent != null && parent != root) {
        parent = parent.getParent();
        if (parent != null) outside.add(parent);
      }
    }
  }
  private void afterConsume(PsiBuilder builder, IElementType type) {
    if (type.equals(SS_VAR) && markingVar) {
      varMarker.done(NAMED_VAR);
      markingVar = false;
      identifierMarker = null;
    }

    if (markingTheme && type.equals(SS_THEME_VAR)) {
      themeMarker.done(SS_THEME_DIR);
    }

    if (type.equals(SS_STRING) && markingTheme) {
      PsiBuilder.Marker fullTheme = themeMarker.precede();
      fullTheme.done(SS_THEME_FILE_PATH);
      markingTheme = false;
      themeMarker = null;
    }

    if (type.equals(SS_IDENTIFIER)) {
      if (identifierMarker == null) identifierMarker = varMarker.precede();
      else identifierMarker = identifierMarker.precede();
      identifierMarker.done(SS_FIELD_REFERENCE);
    }

    if (type.equals(SS_COMMENT_END)) {
      commentMarker.done(SS_COMMENT_STATEMENT);
      markingComment = false;
    }

    if (markingBlock && type.equals(SS_BLOCK_END)) {
      blockMarker.done(blockType);
      markingBlock = false;
      if (BLOCK_STATEMENTS.contains(blockType)) {
        blockStack.push(Pair.create(blockMarker, blockTokenText));
        statementsStack.push(builder.mark());
      } else if (blockType.equals(SS_ELSE_IF_STATEMENT) || blockType.equals(SS_ELSE_STATEMENT)) {
        if (!statementsStack.isEmpty()) {
          statementsStack.pop().doneBefore(SS_STATEMENTS, blockMarker);
          statementsStack.push(builder.mark());
        }
      } else if (blockType.equals(SS_BLOCK_END_STATEMENT)) {
        PsiBuilder.Marker statementMarker = null;
        if (!statementsStack.isEmpty()) {
          statementMarker = statementsStack.pop();
          statementMarker.doneBefore(SS_STATEMENTS, blockMarker);
        }
        if (!blockStack.isEmpty()) {
          Pair<PsiBuilder.Marker, String> blockLevel = blockStack.peek();
          String endString = "end_" + blockLevel.getSecond();
          if (endString.equals(blockTokenText)) {
            PsiBuilder.Marker blockMarker = blockLevel.getFirst().precede();
            blockMarker.done(SS_BLOCK_STATEMENT);
            blockStack.pop();
          } else {
            blockStack.pop();
            if (statementMarker != null) statementMarker.drop();
          }
        }
      }
    }
    if (markingBlock && !blockStartTokens.contains(type)) {
      blockMarker.drop();
      markingBlock = false;
    }
  }
  @RequiredReadAction
  private static boolean processInheritors(
      @NotNull final Processor<DotNetTypeDeclaration> consumer,
      @NotNull final String baseVmQName,
      @NotNull final SearchScope searchScope,
      @NotNull final SearchParameters parameters) {

    if (DotNetTypes.System.Object.equals(baseVmQName)) {
      return AllTypesSearch.search(
              searchScope, parameters.getProject(), parameters.getNameCondition())
          .forEach(
              new Processor<DotNetTypeDeclaration>() {
                @Override
                public boolean process(final DotNetTypeDeclaration aClass) {
                  ProgressIndicatorProvider.checkCanceled();
                  final String qname1 =
                      ApplicationManager.getApplication()
                          .runReadAction(
                              new Computable<String>() {
                                @Override
                                @Nullable
                                public String compute() {
                                  return aClass.getVmQName();
                                }
                              });
                  return DotNetTypes.System.Object.equals(qname1)
                      || consumer.process(parameters.myTransformer.fun(aClass));
                }
              });
    }

    final Ref<String> currentBase = Ref.create(null);
    final Stack<String> stack = new Stack<String>();
    // there are two sets for memory optimization: it's cheaper to hold FQN than PsiClass
    final Set<String> processedFqns =
        new THashSet<String>(); // FQN of processed classes if the class has one

    final Processor<DotNetTypeDeclaration> processor =
        new Processor<DotNetTypeDeclaration>() {
          @Override
          public boolean process(final DotNetTypeDeclaration candidate) {
            ProgressIndicatorProvider.checkCanceled();

            final Ref<Boolean> result = new Ref<Boolean>();
            final Ref<String> vmQNameRef = new Ref<String>();
            ApplicationManager.getApplication()
                .runReadAction(
                    new Runnable() {
                      @Override
                      public void run() {
                        vmQNameRef.set(candidate.getVmQName());
                        if (parameters.isCheckInheritance() || parameters.isCheckDeep()) {
                          if (!candidate.isInheritor(currentBase.get(), false)) {
                            result.set(true);
                            return;
                          }
                        }

                        if (PsiSearchScopeUtil.isInScope(searchScope, candidate)) {
                          final String name = candidate.getName();
                          if (name != null
                              && parameters.getNameCondition().value(name)
                              && !consumer.process(parameters.myTransformer.fun(candidate))) {
                            result.set(false);
                          }
                        }
                      }
                    });
            if (!result.isNull()) {
              return result.get();
            }

            if (parameters.isCheckDeep() && !isSealed(candidate)) {
              stack.push(vmQNameRef.get());
            }

            return true;
          }
        };
    stack.push(baseVmQName);

    final GlobalSearchScope projectScope = GlobalSearchScope.allScope(parameters.getProject());
    while (!stack.isEmpty()) {
      ProgressIndicatorProvider.checkCanceled();

      String vmQName = stack.pop();

      if (!processedFqns.add(vmQName)) {
        continue;
      }

      currentBase.set(vmQName);
      if (!DirectTypeInheritorsSearch.search(parameters.getProject(), vmQName, projectScope, false)
          .forEach(processor)) {
        return false;
      }
    }
    return true;
  }
  @SuppressWarnings("unchecked")
  private static <E extends ArrangementEntry> void doArrange(Context<E> context) {
    // The general idea is to process entries bottom-up where every processed group belongs to the
    // same parent. We may not bother
    // with entries text ranges then. We use a list and a stack for achieving that than.
    //
    // Example:
    //            Entry1              Entry2
    //            /    \              /    \
    //      Entry11   Entry12    Entry21  Entry22
    //
    //    --------------------------
    //    Stage 1:
    //      list: Entry1 Entry2    <-- entries to process
    //      stack: [0, 0, 2]       <-- holds current iteration info at the following format:
    //                                 (start entry index at the auxiliary list (inclusive); current
    // index; end index (exclusive))
    //    --------------------------
    //    Stage 2:
    //      list: Entry1 Entry2 Entry11 Entry12
    //      stack: [0, 1, 2]
    //             [2, 2, 4]
    //    --------------------------
    //    Stage 3:
    //      list: Entry1 Entry2 Entry11 Entry12
    //      stack: [0, 1, 2]
    //             [2, 3, 4]
    //    --------------------------
    //    Stage 4:
    //      list: Entry1 Entry2 Entry11 Entry12
    //      stack: [0, 1, 2]
    //             [2, 4, 4]
    //    --------------------------
    //      arrange 'Entry11 Entry12'
    //    --------------------------
    //    Stage 5:
    //      list: Entry1 Entry2
    //      stack: [0, 1, 2]
    //    --------------------------
    //    Stage 6:
    //      list: Entry1 Entry2 Entry21 Entry22
    //      stack: [0, 2, 2]
    //             [2, 2, 4]
    //    --------------------------
    //    Stage 7:
    //      list: Entry1 Entry2 Entry21 Entry22
    //      stack: [0, 2, 2]
    //             [2, 3, 4]
    //    --------------------------
    //    Stage 8:
    //      list: Entry1 Entry2 Entry21 Entry22
    //      stack: [0, 2, 2]
    //             [2, 4, 4]
    //    --------------------------
    //      arrange 'Entry21 Entry22'
    //    --------------------------
    //    Stage 9:
    //      list: Entry1 Entry2
    //      stack: [0, 2, 2]
    //    --------------------------
    //      arrange 'Entry1 Entry2'

    List<ArrangementEntryWrapper<E>> entries = new ArrayList<ArrangementEntryWrapper<E>>();
    Stack<StackEntry> stack = new Stack<StackEntry>();
    entries.addAll(context.wrappers);
    stack.push(new StackEntry(0, context.wrappers.size()));
    while (!stack.isEmpty()) {
      StackEntry stackEntry = stack.peek();
      if (stackEntry.current >= stackEntry.end) {
        List<ArrangementEntryWrapper<E>> subEntries =
            entries.subList(stackEntry.start, stackEntry.end);
        if (subEntries.size() > 1) {
          doArrange(subEntries, context);
        }
        subEntries.clear();
        stack.pop();
      } else {
        ArrangementEntryWrapper<E> wrapper = entries.get(stackEntry.current++);
        List<ArrangementEntryWrapper<E>> children = wrapper.getChildren();
        if (!children.isEmpty()) {
          entries.addAll(children);
          stack.push(new StackEntry(stackEntry.end, children.size()));
        }
      }
    }
  }
 @Nullable
 private DefaultArrangementEntry getCurrent() {
   return myStack.isEmpty() ? null : myStack.peek();
 }
Example #19
0
 private SignatureVisitor signatureVisitor() {
   return !visitors.isEmpty() ? visitors.peek() : signatureVisitor;
 }
Example #20
0
  public static StringBuffer addDiagnosticMarkersToText(
      @NotNull final PsiFile psiFile,
      @NotNull Collection<Diagnostic> diagnostics,
      @NotNull Map<Diagnostic, TextDiagnostic> diagnosticToExpectedDiagnostic,
      @NotNull Function<PsiFile, String> getFileText) {
    String text = getFileText.fun(psiFile);
    StringBuffer result = new StringBuffer();
    diagnostics =
        Collections2.filter(
            diagnostics,
            new Predicate<Diagnostic>() {
              @Override
              public boolean apply(Diagnostic diagnostic) {
                return psiFile.equals(diagnostic.getPsiFile());
              }
            });
    if (!diagnostics.isEmpty()) {
      List<DiagnosticDescriptor> diagnosticDescriptors =
          getSortedDiagnosticDescriptors(diagnostics);

      Stack<DiagnosticDescriptor> opened = new Stack<DiagnosticDescriptor>();
      ListIterator<DiagnosticDescriptor> iterator = diagnosticDescriptors.listIterator();
      DiagnosticDescriptor currentDescriptor = iterator.next();

      for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        while (!opened.isEmpty() && i == opened.peek().end) {
          closeDiagnosticString(result);
          opened.pop();
        }
        while (currentDescriptor != null && i == currentDescriptor.start) {
          openDiagnosticsString(result, currentDescriptor, diagnosticToExpectedDiagnostic);
          if (currentDescriptor.getEnd() == i) {
            closeDiagnosticString(result);
          } else {
            opened.push(currentDescriptor);
          }
          if (iterator.hasNext()) {
            currentDescriptor = iterator.next();
          } else {
            currentDescriptor = null;
          }
        }
        result.append(c);
      }

      if (currentDescriptor != null) {
        assert currentDescriptor.start == text.length();
        assert currentDescriptor.end == text.length();
        openDiagnosticsString(result, currentDescriptor, diagnosticToExpectedDiagnostic);
        opened.push(currentDescriptor);
      }

      while (!opened.isEmpty() && text.length() == opened.peek().end) {
        closeDiagnosticString(result);
        opened.pop();
      }

      assert opened.isEmpty() : "Stack is not empty: " + opened;
    } else {
      result.append(text);
    }
    return result;
  }