Java Code Examples for com.intellij.openapi.util.text.StringUtil#indexOf()

The following examples show how to use com.intellij.openapi.util.text.StringUtil#indexOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RunAnythingHelpItem.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void parseAndApplyStyleToParameters(@Nonnull SimpleColoredComponent component, @Nonnull String placeholder) {
  int lt = StringUtil.indexOf(placeholder, "<");
  if (lt == -1) {
    component.append(placeholder);
    return;
  }

  int gt = StringUtil.indexOf(placeholder, ">", lt);
  //appends leading
  component.append(gt > -1 ? placeholder.substring(0, lt) : placeholder);
  while (lt > -1 && gt > -1) {
    component.append(placeholder.substring(lt, gt + 1), SimpleTextAttributes.GRAY_ATTRIBUTES);

    lt = StringUtil.indexOf(placeholder, "<", gt);
    if (lt == -1) {
      component.append(placeholder.substring(gt + 1));
      break;
    }

    component.append(placeholder.substring(gt + 1, lt));
    gt = StringUtil.indexOf(placeholder, ">", lt);
  }
}
 
Example 2
Source File: ComparisonManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static List<Line> getLines(@Nonnull CharSequence text) {
  List<Line> lines = new ArrayList<>();

  int offset = 0;
  while (true) {
    int lineEnd = StringUtil.indexOf(text, '\n', offset);
    if (lineEnd != -1) {
      lines.add(new Line(text, offset, lineEnd, true));
      offset = lineEnd + 1;
    }
    else {
      lines.add(new Line(text, offset, text.length(), false));
      break;
    }
  }

  return lines;
}
 
Example 3
Source File: LanguageConsoleBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void documentChanged(DocumentEvent event) {
  DocumentEx document = getDocument();
  if (document.isInBulkUpdate()) {
    return;
  }

  if (document.getTextLength() > 0) {
    addLineSeparatorPainterIfNeed();
    int startDocLine = document.getLineNumber(event.getOffset());
    int endDocLine = document.getLineNumber(event.getOffset() + event.getNewLength());
    if (event.getOldLength() > event.getNewLength() || startDocLine != endDocLine || StringUtil.indexOf(event.getOldFragment(), '\n') != -1) {
      updateGutterSize(startDocLine, endDocLine);
    }
  }
  else if (event.getOldLength() > 0) {
    documentCleared();
  }
}
 
Example 4
Source File: EventLog.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void removeJavaNewLines(Document document, List<RangeMarker> lineSeparators, String indent, boolean hasHtml) {
  CharSequence text = document.getCharsSequence();
  int i = 0;
  while (true) {
    i = StringUtil.indexOf(text, '\n', i);
    if (i < 0) break;
    int j = i + 1;
    if (StringUtil.startsWith(text, j, indent)) {
      j += indent.length();
    }
    document.deleteString(i, j);
    if (!hasHtml) {
      lineSeparators.add(document.createRangeMarker(TextRange.from(i, 0)));
    }
  }
}
 
Example 5
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
private static void removeJavaNewLines(Document document, List<RangeMarker> lineSeparators, boolean hasHtml) {
    CharSequence text = document.getCharsSequence();
    int i = 0;
    while(true) {
        i = StringUtil.indexOf(text, '\n', i);
        if(i < 0) {
            break;
        }
        document.deleteString(i, i + 1);
        if(!hasHtml) {
            lineSeparators.add(document.createRangeMarker(TextRange.from(i, 0)));
        }
    }
}
 
Example 6
Source File: DocumentWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int countNewLinesIn(CharSequence text, int[] pos, int line) {
  int offsetInside = 0;
  for (int i = StringUtil.indexOf(text, '\n'); i != -1; i = StringUtil.indexOf(text, '\n', offsetInside)) {
    int curLine = ++pos[0];
    int lineLength = i + 1 - offsetInside;
    int offset = pos[1] += lineLength;
    offsetInside += lineLength;
    if (curLine == line) return offset;
  }
  pos[1] += text.length() - offsetInside;
  return -1;
}
 
Example 7
Source File: CommandLineArgumentEncoder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeArgument(@Nonnull StringBuilder builder) {
  StringUtil.escapeQuotes(builder);
  if (builder.length() == 0 || StringUtil.indexOf(builder, ' ') >= 0 || StringUtil.indexOf(builder, '|') >= 0) {
    // don't let a trailing backslash (if any) unintentionally escape the closing quote
    int numTrailingBackslashes = builder.length() - StringUtil.trimTrailing(builder, '\\').length();
    StringUtil.quote(builder);
    StringUtil.repeatSymbol(builder, '\\', numTrailingBackslashes);
  }
}
 
Example 8
Source File: XmlStringUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String wrapInCDATA(@Nonnull String str) {
  StringBuilder sb = new StringBuilder();
  int cur = 0, len = str.length();
  while (cur < len) {
    int next = StringUtil.indexOf(str, CDATA_END, cur);
    sb.append(CDATA_START).append(str.subSequence(cur, next = next < 0 ? len : next + 1)).append(CDATA_END);
    cur = next;
  }
  return sb.toString();
}
 
Example 9
Source File: FileUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isHashBangLine(CharSequence firstCharsIfText, String marker) {
  if (firstCharsIfText == null) {
    return false;
  }
  final int lineBreak = StringUtil.indexOf(firstCharsIfText, '\n');
  if (lineBreak < 0) {
    return false;
  }
  String firstLine = firstCharsIfText.subSequence(0, lineBreak).toString();
  return firstLine.startsWith("#!") && firstLine.contains(marker);
}
 
Example 10
Source File: EventLog.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void indentNewLines(DocumentImpl logDoc, List<RangeMarker> lineSeparators, RangeMarker afterTitle, boolean hasHtml, String indent) {
  if (!hasHtml) {
    int i = -1;
    while (true) {
      i = StringUtil.indexOf(logDoc.getText(), '\n', i + 1);
      if (i < 0) {
        break;
      }
      lineSeparators.add(logDoc.createRangeMarker(i, i + 1));
    }
  }
  if (!lineSeparators.isEmpty() && afterTitle != null && afterTitle.isValid()) {
    lineSeparators.add(afterTitle);
  }
  int nextLineStart = -1;
  for (RangeMarker separator : lineSeparators) {
    if (separator.isValid()) {
      int start = separator.getStartOffset();
      if (start == nextLineStart) {
        continue;
      }

      logDoc.replaceString(start, separator.getEndOffset(), "\n" + indent);
      nextLineStart = start + 1 + indent.length();
      while (nextLineStart < logDoc.getTextLength() && Character.isWhitespace(logDoc.getCharsSequence().charAt(nextLineStart))) {
        logDoc.deleteString(nextLineStart, nextLineStart + 1);
      }
    }
  }
}
 
Example 11
Source File: FileDocumentManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Document createDocument(@Nonnull CharSequence text, @Nonnull VirtualFile file) {
  boolean acceptSlashR = file instanceof LightVirtualFile && StringUtil.indexOf(text, '\r') >= 0;
  boolean freeThreaded = Boolean.TRUE.equals(file.getUserData(AbstractFileViewProvider.FREE_THREADED));
  DocumentImpl document = (DocumentImpl)((EditorFactoryImpl)EditorFactory.getInstance()).createDocument(text, acceptSlashR, freeThreaded);
  document.documentCreatedFrom(file);
  return document;
}
 
Example 12
Source File: FoldingModelSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void onDocumentChanged(@Nonnull DocumentEvent e) {
  if (StringUtil.indexOf(e.getOldFragment(), '\n') != -1 ||
      StringUtil.indexOf(e.getNewFragment(), '\n') != -1) {
    for (int i = 0; i < myCount; i++) {
      if (myEditors[i].getDocument() == e.getDocument()) {
        myShouldUpdateLineNumbers[i] = true;
      }
    }
  }
}
 
Example 13
Source File: ConsoleLog.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
private static void indentNewLines(DocumentImpl logDoc, List<RangeMarker> lineSeparators, RangeMarker afterTitle, boolean hasHtml, String indent) {
    if(!hasHtml) {
        int i = -1;
        while(true) {
            i = StringUtil.indexOf(logDoc.getText(), '\n', i + 1);
            if(i < 0) {
                break;
            }
            lineSeparators.add(logDoc.createRangeMarker(i, i + 1));
        }
    }
    if(!lineSeparators.isEmpty() && afterTitle != null && afterTitle.isValid()) {
        lineSeparators.add(afterTitle);
    }
    int nextLineStart = -1;
    for(RangeMarker separator : lineSeparators) {
        if(separator.isValid()) {
            int start = separator.getStartOffset();
            if(start == nextLineStart) {
                continue;
            }

            logDoc.replaceString(start, separator.getEndOffset(), "\n" + indent);
            nextLineStart = start + 1 + indent.length();
            while(nextLineStart < logDoc.getTextLength() && Character.isWhitespace(logDoc.getCharsSequence().charAt(nextLineStart))) {
                logDoc.deleteString(nextLineStart, nextLineStart + 1);
            }
        }
    }
}
 
Example 14
Source File: DesktopEditorImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void changedUpdate(DocumentEvent e) {
  myDocumentChangeInProgress = false;
  if (myDocument.isInBulkUpdate()) return;

  if (myErrorStripeNeedsRepaint) {
    myMarkupModel.repaint(e.getOffset(), e.getOffset() + e.getNewLength());
    myErrorStripeNeedsRepaint = false;
  }

  setMouseSelectionState(MOUSE_SELECTION_STATE_NONE);

  if (getGutterComponentEx().getCurrentAccessibleLine() != null) {
    escapeGutterAccessibleLine(e.getOffset(), e.getOffset() + e.getNewLength());
  }

  validateSize();

  int startLine = offsetToLogicalLine(e.getOffset());
  int endLine = offsetToLogicalLine(e.getOffset() + e.getNewLength());

  boolean painted = false;
  if (myDocument.getTextLength() > 0) {
    if (startLine != endLine || StringUtil.indexOf(e.getOldFragment(), '\n') != -1) {
      myGutterComponent.clearLineToGutterRenderersCache();
    }

    if (countLineFeeds(e.getOldFragment()) != countLineFeeds(e.getNewFragment())) {
      // Lines removed. Need to repaint till the end of the screen
      repaintToScreenBottom(startLine);
      painted = true;
    }
  }

  updateCaretCursor();
  if (!painted) {
    repaintLines(startLine, endLine);
  }

  if (myRestoreScrollingPosition && !Boolean.TRUE.equals(getUserData(DISABLE_CARET_POSITION_KEEPING))) {
    myScrollingPositionKeeper.restorePosition(true);
  }
}
 
Example 15
Source File: MinusculeMatcherImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int matchingDegree(@Nonnull String name, boolean valueStartCaseMatch, @Nullable FList<? extends TextRange> fragments) {
  if (fragments == null) return Integer.MIN_VALUE;
  if (fragments.isEmpty()) return 0;

  final TextRange first = fragments.getHead();
  boolean startMatch = first.getStartOffset() == 0;
  boolean valuedStartMatch = startMatch && valueStartCaseMatch;

  int matchingCase = 0;
  int p = -1;

  int skippedHumps = 0;
  int nextHumpStart = 0;
  boolean humpStartMatchedUpperCase = false;
  for (TextRange range : fragments) {
    for (int i = range.getStartOffset(); i < range.getEndOffset(); i++) {
      boolean afterGap = i == range.getStartOffset() && first != range;
      boolean isHumpStart = false;
      while (nextHumpStart <= i) {
        if (nextHumpStart == i) {
          isHumpStart = true;
        }
        else if (afterGap) {
          skippedHumps++;
        }
        nextHumpStart = nextWord(name, nextHumpStart);
      }

      char c = name.charAt(i);
      p = StringUtil.indexOf(myPattern, c, p + 1, myPattern.length, false);
      if (p < 0) {
        break;
      }

      if (isHumpStart) {
        humpStartMatchedUpperCase = c == myPattern[p] && isUpperCase[p];
      }

      matchingCase += evaluateCaseMatching(valuedStartMatch, p, humpStartMatchedUpperCase, i, afterGap, isHumpStart, c);
    }
  }

  int startIndex = first.getStartOffset();
  boolean afterSeparator = StringUtil.indexOfAny(name, myHardSeparators, 0, startIndex) >= 0;
  boolean wordStart = startIndex == 0 || NameUtilCore.isWordStart(name, startIndex) && !NameUtilCore.isWordStart(name, startIndex - 1);
  boolean finalMatch = fragments.get(fragments.size() - 1).getEndOffset() == name.length();

  return (wordStart ? 1000 : 0) + matchingCase + -fragments.size() + -skippedHumps * 10 + (afterSeparator ? 0 : 2) + (startMatch ? 1 : 0) + (finalMatch ? 1 : 0);
}
 
Example 16
Source File: DiffString.java    From consulo with Apache License 2.0 4 votes vote down vote up
public int indexOf(char c) {
  return StringUtil.indexOf(this, c);
}
 
Example 17
Source File: TypoTolerantMatcher.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int matchingDegree(@Nonnull String name, boolean valueStartCaseMatch, @Nullable FList<? extends TextRange> fragments) {
  if (fragments == null) return Integer.MIN_VALUE;
  if (fragments.isEmpty()) return 0;

  final TextRange first = fragments.getHead();
  boolean startMatch = first.getStartOffset() == 0;
  boolean valuedStartMatch = startMatch && valueStartCaseMatch;

  int errors = 0;
  int matchingCase = 0;
  int p = -1;

  int skippedHumps = 0;
  int nextHumpStart = 0;
  boolean humpStartMatchedUpperCase = false;
  for (TextRange range : fragments) {
    for (int i = range.getStartOffset(); i < range.getEndOffset(); i++) {
      boolean afterGap = i == range.getStartOffset() && first != range;
      boolean isHumpStart = false;
      while (nextHumpStart <= i) {
        if (nextHumpStart == i) {
          isHumpStart = true;
        }
        else if (afterGap) {
          skippedHumps++;
        }
        nextHumpStart = nextWord(name, nextHumpStart);
      }

      char c = name.charAt(i);
      p = StringUtil.indexOf(myPattern, c, p + 1, myPattern.length, false);
      if (p < 0) {
        break;
      }

      if (isHumpStart) {
        humpStartMatchedUpperCase = c == myPattern[p] && isUpperCase[p];
      }

      matchingCase += evaluateCaseMatching(valuedStartMatch, p, humpStartMatchedUpperCase, i, afterGap, isHumpStart, c);
    }

    errors += 2000.0 * Math.pow(1.0 * ((Range)range).getErrorCount() / range.getLength(), 2);
  }

  int startIndex = first.getStartOffset();
  boolean afterSeparator = StringUtil.indexOfAny(name, myHardSeparators, 0, startIndex) >= 0;
  boolean wordStart = startIndex == 0 || NameUtilCore.isWordStart(name, startIndex) && !NameUtilCore.isWordStart(name, startIndex - 1);
  boolean finalMatch = fragments.get(fragments.size() - 1).getEndOffset() == name.length();

  return (wordStart ? 1000 : 0) + matchingCase + -fragments.size() + -skippedHumps * 10 + -errors + (afterSeparator ? 0 : 2) + (startMatch ? 1 : 0) + (finalMatch ? 1 : 0);
}
 
Example 18
Source File: FileReferenceSet.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected int findSeparatorOffset(@Nonnull CharSequence sequence, int startingFrom) {
  return StringUtil.indexOf(sequence, getSeparatorString(), startingFrom);
}
 
Example 19
Source File: ChunkExtractor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public TextChunk[] createTextChunks(@Nonnull UsageInfo2UsageAdapter usageInfo2UsageAdapter,
                                    @Nonnull CharSequence chars,
                                    int start,
                                    int end,
                                    boolean selectUsageWithBold,
                                    @Nonnull List<TextChunk> result) {
  final Lexer lexer = myHighlighter.getHighlightingLexer();
  final SyntaxHighlighterOverEditorHighlighter highlighter = myHighlighter;

  LOG.assertTrue(start <= end);

  int i = StringUtil.indexOf(chars, '\n', start, end);
  if (i != -1) end = i;

  if (myDocumentStamp != myDocument.getModificationStamp()) {
    highlighter.restart(chars);
    myDocumentStamp = myDocument.getModificationStamp();
  }
  else if (lexer.getTokenType() == null || lexer.getTokenStart() > start) {
    highlighter.resetPosition(0);  // todo restart from nearest position with initial state
  }

  boolean isBeginning = true;

  for (; lexer.getTokenType() != null; lexer.advance()) {
    int hiStart = lexer.getTokenStart();
    int hiEnd = lexer.getTokenEnd();

    if (hiStart >= end) break;

    hiStart = Math.max(hiStart, start);
    hiEnd = Math.min(hiEnd, end);
    if (hiStart >= hiEnd) {
      continue;
    }

    if (isBeginning) {
      String text = chars.subSequence(hiStart, hiEnd).toString();
      if (text.trim().isEmpty()) continue;
    }
    isBeginning = false;
    IElementType tokenType = lexer.getTokenType();
    TextAttributesKey[] tokenHighlights = highlighter.getTokenHighlights(tokenType);

    processIntersectingRange(usageInfo2UsageAdapter, chars, hiStart, hiEnd, tokenHighlights, selectUsageWithBold, result);
  }

  return result.toArray(new TextChunk[result.size()]);
}
 
Example 20
Source File: DirectoryPathMatcher.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean containsChar(CharSequence name, char c) {
  return StringUtil.indexOf(name, c, 0, name.length(), false) >= 0;
}