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

The following examples show how to use com.intellij.openapi.util.text.StringUtil#first() . 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: ProblemDescriptorUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static String extractHighlightedText(@Nonnull CommonProblemDescriptor descriptor, PsiElement psiElement) {
  if (psiElement == null || !psiElement.isValid()) return "";
  String ref = psiElement.getText();
  if (descriptor instanceof ProblemDescriptorBase) {
    TextRange textRange = ((ProblemDescriptorBase)descriptor).getTextRange();
    final TextRange elementRange = psiElement.getTextRange();
    if (textRange != null && elementRange != null) {
      textRange = textRange.shiftRight(-elementRange.getStartOffset());
      if (textRange.getStartOffset() >= 0 && textRange.getEndOffset() <= elementRange.getLength()) {
        ref = textRange.substring(ref);
      }
    }
  }
  ref = StringUtil.replaceChar(ref, '\n', ' ').trim();
  ref = StringUtil.first(ref, 100, true);
  return ref;
}
 
Example 2
Source File: UndoManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private Pair<String, String> getUndoOrRedoActionNameAndDescription(FileEditor editor, boolean undo) {
  String desc = isUndoOrRedoAvailable(editor, undo) ? doFormatAvailableUndoRedoAction(editor, undo) : null;
  if (desc == null) desc = "";
  String shortActionName = StringUtil.first(desc, 30, true);

  if (desc.isEmpty()) {
    desc = undo
           ? ActionsBundle.message("action.undo.description.empty")
           : ActionsBundle.message("action.redo.description.empty");
  }

  return Pair.create((undo ? ActionsBundle.message("action.undo.text", shortActionName)
                           : ActionsBundle.message("action.redo.text", shortActionName)).trim(),
                     (undo ? ActionsBundle.message("action.undo.description", desc)
                           : ActionsBundle.message("action.redo.description", desc)).trim());
}
 
Example 3
Source File: ContentChooser.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
  setIcon(myListEntryIcon);
  if (myUseIdeaEditor) {
    int max = list.getModel().getSize();
    String indexString = String.valueOf(index + 1);
    int count = String.valueOf(max).length() - indexString.length();
    char[] spaces = new char[count];
    Arrays.fill(spaces, ' ');
    String prefix = indexString + new String(spaces) + "  ";
    append(prefix, SimpleTextAttributes.GRAYED_ATTRIBUTES);
  }
  else if (UIUtil.isUnderGTKLookAndFeel()) {
    // Fix GTK background
    Color background = selected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground();
    UIUtil.changeBackGround(this, background);
  }
  String text = ((Item)value).shortText;

  FontMetrics metrics = list.getFontMetrics(list.getFont());
  int charWidth = metrics.charWidth('m');
  int maxLength = list.getParent().getParent().getWidth() * 3 / charWidth / 2;
  text = StringUtil.first(text, maxLength, true); // do not paint long strings
  append(text, SimpleTextAttributes.REGULAR_ATTRIBUTES);
}
 
Example 4
Source File: PassExecutorService.java    From consulo with Apache License 2.0 6 votes vote down vote up
static void log(ProgressIndicator progressIndicator, TextEditorHighlightingPass pass, @NonNls @Nonnull Object... info) {
  if (LOG.isDebugEnabled()) {
    CharSequence docText = pass == null || pass.getDocument() == null ? "" : ": '" + StringUtil.first(pass.getDocument().getCharsSequence(), 10, true) + "'";
    synchronized (PassExecutorService.class) {
      String infos = StringUtil.join(info, Functions.TO_STRING(), " ");
      String message = StringUtil.repeatSymbol(' ', getThreadNum() * 4) +
                       " " +
                       pass +
                       " " +
                       infos +
                       "; progress=" +
                       (progressIndicator == null ? null : progressIndicator.hashCode()) +
                       " " +
                       (progressIndicator == null ? "?" : progressIndicator.isCanceled() ? "X" : "V") +
                       docText;
      LOG.debug(message);
      //System.out.println(message);
    }
  }
}
 
Example 5
Source File: GotoActionModel.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static String cutName(String name, String shortcutText, JList list, JPanel panel, SimpleColoredComponent nameComponent) {
  if (!list.isShowing() || list.getWidth() <= 0) {
    return StringUtil.first(name, 60, true); //fallback to previous behaviour
  }
  int freeSpace = calcFreeSpace(list, panel, nameComponent, shortcutText);

  if (freeSpace <= 0) {
    return name;
  }

  FontMetrics fm = nameComponent.getFontMetrics(nameComponent.getFont());
  int strWidth = fm.stringWidth(name);
  if (strWidth <= freeSpace) {
    return name;
  }

  int cutSymbolIndex = (int)((((double)freeSpace - fm.stringWidth("...")) / strWidth) * name.length());
  cutSymbolIndex = Integer.max(1, cutSymbolIndex);
  name = name.substring(0, cutSymbolIndex);
  while (fm.stringWidth(name + "...") > freeSpace && name.length() > 1) {
    name = name.substring(0, name.length() - 1);
  }

  return name.trim() + "...";
}
 
Example 6
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiBuilderImpl(@Nullable Project project,
                       @Nullable PsiFile containingFile,
                       @Nonnull LanguageVersion languageVersion,
                       @Nonnull ParserDefinition parserDefinition,
                       @Nonnull Lexer lexer,
                       @Nullable CharTable charTable,
                       @Nonnull CharSequence text,
                       @Nullable ASTNode originalTree,
                       @Nullable CharSequence lastCommittedText,
                       @Nullable MyTreeStructure parentLightTree,
                       @Nullable Object parentCachingNode) {
  myProject = project;
  myFile = containingFile;
  myLanguageVersion = languageVersion;
  myText = text;
  myTextArray = CharArrayUtil.fromSequenceWithoutCopying(text);
  myLexer = lexer;

  myWhitespaces = parserDefinition.getWhitespaceTokens(languageVersion);
  myComments = parserDefinition.getCommentTokens(languageVersion);
  myCharTable = charTable;
  myOriginalTree = originalTree;
  myLastCommittedText = lastCommittedText;
  if ((originalTree == null) != (lastCommittedText == null)) {
    throw new IllegalArgumentException("originalTree and lastCommittedText must be null/notnull together but got: originalTree=" +
                                       originalTree +
                                       "; lastCommittedText=" +
                                       (lastCommittedText == null ? null : "'" + StringUtil.first(lastCommittedText, 80, true) + "'"));
  }
  myParentLightTree = parentLightTree;
  myOffset = parentCachingNode instanceof LazyParseableToken ? ((LazyParseableToken)parentCachingNode).getStartOffset() : 0;

  cacheLexemes(parentCachingNode);
}
 
Example 7
Source File: DocumentCommitThread.java    From consulo with Apache License 2.0 5 votes vote down vote up
@NonNls
@Override
public String toString() {
  Document document = getDocument();
  String indicatorInfo = isCanceled() ? " (Canceled: " + ((UserDataHolder)indicator).getUserData(CANCEL_REASON) + ")" : "";
  String removedInfo = dead ? " (dead)" : "";
  String reasonInfo = " task reason: " +
                      StringUtil.first(String.valueOf(reason), 180, true) +
                      (isStillValid() ? "" : "; changed: old seq=" + modificationSequence + ", new seq=" + ((DocumentEx)document).getModificationSequence());
  String contextInfo = " Context: " + myCreationContext;
  return System.identityHashCode(this) + "; " + indicatorInfo + removedInfo + contextInfo + reasonInfo;
}
 
Example 8
Source File: ScratchFileCreationHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiFile parseHeader(@Nonnull Project project, @Nonnull Language language, @Nonnull String text) {
  LanguageFileType fileType = language.getAssociatedFileType();
  CharSequence fileSnippet = StringUtil.first(text, 10 * 1024, false);
  PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
  return fileFactory.createFileFromText(PathUtil.makeFileName("a", fileType == null ? "" : fileType.getDefaultExtension()), language, fileSnippet);
}