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

The following examples show how to use com.intellij.openapi.util.text.StringUtil#countChars() . 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: DocStringFormatter.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** For now, present docstring almost verbatim, with a minimal indentation handling. */
public static String formatDocString(StringLiteral docstring, DocStringOwner owner) {
  // TODO: Handle Google python docstring style specifically.
  // (see https://google.github.io/styleguide/pyguide.html#Comments)
  String raw = docstring.getStringContents();
  String[] lines = raw.split("\n");
  StringBuilder output = new StringBuilder();
  output.append("<pre>");

  int initialIndent = -1;
  for (String line : lines) {
    if (initialIndent == -1 && line.startsWith(" ")) {
      initialIndent = StringUtil.countChars(line, ' ', 0, true);
    }
    line = trimStart(line, initialIndent);
    if (!line.isEmpty()) {
      output.append(line);
    }
    output.append("<br>");
  }
  output.append("</pre>");
  return output.toString();
}
 
Example 2
Source File: ParagraphFillHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiElement getFirstElement(@Nonnull final PsiElement element) {
  final IElementType elementType = element.getNode().getElementType();
  PsiElement prevSibling = element.getPrevSibling();
  PsiElement result = element;
  while (prevSibling != null && (prevSibling.getNode().getElementType().equals(elementType) ||
                                 (atWhitespaceToken(prevSibling) &&
                                  StringUtil.countChars(prevSibling.getText(), '\n') <= 1))) {
    String text = prevSibling.getText();
    final String prefix = getPrefix(element);
    final String postfix = getPostfix(element);
    text = StringUtil.trimStart(text.trim(), prefix.trim());
    text = StringUtil.trimEnd(text, postfix);

    if (prevSibling.getNode().getElementType().equals(elementType) &&
        StringUtil.isEmptyOrSpaces(text)) {
      break;
    }
    if (prevSibling.getNode().getElementType().equals(elementType))
      result = prevSibling;
    prevSibling = prevSibling.getPrevSibling();
  }
  return result;
}
 
Example 3
Source File: ParagraphFillHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiElement getLastElement(@Nonnull final PsiElement element) {
  final IElementType elementType = element.getNode().getElementType();
  PsiElement nextSibling = element.getNextSibling();
  PsiElement result = element;
  while (nextSibling != null && (nextSibling.getNode().getElementType().equals(elementType) ||
                                 (atWhitespaceToken(nextSibling) &&
                                  StringUtil.countChars(nextSibling.getText(), '\n') <= 1))) {
    String text = nextSibling.getText();
    final String prefix = getPrefix(element);
    final String postfix = getPostfix(element);
    text = StringUtil.trimStart(text.trim(), prefix.trim());
    text = StringUtil.trimEnd(text, postfix);

    if (nextSibling.getNode().getElementType().equals(elementType) &&
        StringUtil.isEmptyOrSpaces(text)) {
      break;
    }
    if (nextSibling.getNode().getElementType().equals(elementType))
      result = nextSibling;
    nextSibling = nextSibling.getNextSibling();
  }
  return result;
}
 
Example 4
Source File: CreateDirectoryOrPackageHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private Boolean suggestCreatingFileInstead(String subDirName) {
  Boolean createFile = false;
  if (StringUtil.countChars(subDirName, '.') == 1 && Registry.is("ide.suggest.file.when.creating.filename.like.directory")) {
    FileType fileType = findFileTypeBoundToName(subDirName);
    if (fileType != null) {
      String message = "The name you entered looks like a file name. Do you want to create a file named " + subDirName + " instead?";
      int ec = Messages.showYesNoCancelDialog(myProject, message, "File Name Detected", "&Yes, create file", "&No, create " + (myIsDirectory ? "directory" : "packages"),
                                              CommonBundle.getCancelButtonText(), TargetAWT.to(fileType.getIcon()));
      if (ec == Messages.CANCEL) {
        createFile = null;
      }
      if (ec == Messages.YES) {
        createFile = true;
      }
    }
  }
  return createFile;
}
 
Example 5
Source File: PackageEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isBetterMatchForPackageThan(@javax.annotation.Nullable PackageEntry entry, @Nonnull String packageName, boolean isStatic) {
  if (isStatic() != isStatic || !matchesPackageName(packageName)) return false;
  if (entry == null) {
    return true;
  }
  if (entry.isStatic() != isStatic) return false;
  if (entry.isWithSubpackages() != isWithSubpackages()) {
    return !isWithSubpackages();
  }
  if (entry == ALL_OTHER_IMPORTS_ENTRY || entry == ALL_OTHER_STATIC_IMPORTS_ENTRY) return true;
  if (this == ALL_OTHER_IMPORTS_ENTRY || this == ALL_OTHER_STATIC_IMPORTS_ENTRY) return false;
  return StringUtil.countChars(entry.getPackageName(), '.') < StringUtil.countChars(getPackageName(), '.');
}
 
Example 6
Source File: BasicGutterContentProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static int getMarkerCount(int line, @Nonnull Document document) {
  return StringUtil.countChars(document.getImmutableCharSequence(), EVAL_IN_MARKER.charAt(0), document.getLineStartOffset(line), true);
}