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

The following examples show how to use com.intellij.openapi.util.text.StringUtil#isWhiteSpace() . 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: HLint.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
/**
 * Fallback to a crude guess if the json output is not available from hlint.
 */
public int getOffsetEndFallback(int offsetStart, String fileText) {
    int width = 0;
    int nonWhiteSpaceToFind = WHITESPACE_REGEX.matcher(from).replaceAll("").length();
    int nonWhiteSpaceFound = 0;
    while (offsetStart + width < fileText.length()) {
        final char c = fileText.charAt(offsetStart + width);
        if (StringUtil.isLineBreak(c)) {
            break;
        }
        if (!StringUtil.isWhiteSpace(c)) {
            ++nonWhiteSpaceFound;
        }
        ++width;
        if (nonWhiteSpaceFound >= nonWhiteSpaceToFind) {
            break;
        }
    }
    return offsetStart + width;
}
 
Example 2
Source File: HungryBackspaceAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredWriteAction
@Override
public void executeWriteAction(@Nonnull Editor editor, Caret caret, DataContext dataContext) {
  final Document document = editor.getDocument();
  final int caretOffset = editor.getCaretModel().getOffset();
  if (caretOffset < 1) {
    return;
  }

  final SelectionModel selectionModel = editor.getSelectionModel();
  final CharSequence text = document.getCharsSequence();
  final char c = text.charAt(caretOffset - 1);
  if (!selectionModel.hasSelection() && StringUtil.isWhiteSpace(c)) {
    int startOffset = CharArrayUtil.shiftBackward(text, caretOffset - 2, "\t \n") + 1;
    document.deleteString(startOffset, caretOffset);
  }
  else {
    final EditorActionHandler handler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE);
    handler.execute(editor, caret, dataContext);
  }
}
 
Example 3
Source File: CustomFoldingSurroundDescriptor.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static boolean isWhiteSpaceWithLineFeed(@Nullable PsiElement element) {
  if (element == null) {
    return false;
  }
  if (element instanceof PsiWhiteSpace) {
    return element.textContains('\n');
  }
  final ASTNode node = element.getNode();
  if (node == null) {
    return false;
  }
  final CharSequence text = node.getChars();
  boolean lineFeedFound = false;
  for (int i = 0; i < text.length(); i++) {
    final char c = text.charAt(i);
    if (!StringUtil.isWhiteSpace(c)) {
      return false;
    }
    lineFeedFound |= c == '\n';
  }
  return lineFeedFound;
}
 
Example 4
Source File: GeneratedParserUtilBase.java    From intellij-latte with MIT License 5 votes vote down vote up
public boolean prefixMatches(@NotNull String prefix, @NotNull String variant) {
	boolean matches = new CamelHumpMatcher(prefix, false).prefixMatches(variant.replace(' ', '_'));
	if (matches && StringUtil.isWhiteSpace(prefix.charAt(prefix.length() - 1))) {
		return StringUtil.startsWithIgnoreCase(variant, prefix);
	}
	return matches;
}
 
Example 5
Source File: StatementParsing.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
private static boolean isAwait(CharSequence sequence)
{
	if(sequence.length() < AWAIT_KEYWORD.length())
	{
		return false;
	}

	// check for await
	for(int i = 0; i < AWAIT_KEYWORD.length(); i++)
	{
		char expectedChar = AWAIT_KEYWORD.charAt(i);
		char actualChar = sequence.charAt(i);
		if(actualChar != expectedChar)
		{
			return false;
		}
	}

	if(sequence.length() == AWAIT_KEYWORD.length())
	{
		return true;
	}

	for(int i = AWAIT_KEYWORD.length(); i < sequence.length(); i++)
	{
		char c = sequence.charAt(i);
		if(!StringUtil.isWhiteSpace(c))
		{
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: AbstractIndentingBackspaceHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeCharDeleted(char c, PsiFile file, Editor editor) {
  myEnabled = false;
  if (editor.isColumnMode() || !StringUtil.isWhiteSpace(c)) {
    return;
  }
  SmartBackspaceMode mode = getBackspaceMode(file.getLanguage());
  if (mode != myMode) {
    return;
  }
  doBeforeCharDeleted(c, file, editor);
  myEnabled = true;
}
 
Example 7
Source File: DiffString.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isWhiteSpace(char c) {
  return StringUtil.isWhiteSpace(c);
}
 
Example 8
Source File: FixDocCommentAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a comment if possible.
 * <p>
 * It's assumed that this method {@link PsiDocumentManager#commitDocument(Document) syncs} all PSI-document
 * changes during the processing.
 *
 * @param anchor    target element for which a comment should be generated
 * @param editor    target editor
 * @param commenter commenter to use
 * @param project   current project
 */
private static void generateComment(@Nonnull PsiElement anchor,
                                    @Nonnull Editor editor,
                                    @Nonnull CodeDocumentationProvider documentationProvider,
                                    @Nonnull CodeDocumentationAwareCommenter commenter,
                                    @Nonnull Project project) {
  Document document = editor.getDocument();
  int commentStartOffset = anchor.getTextRange().getStartOffset();
  int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset));
  if (lineStartOffset > 0 && lineStartOffset < commentStartOffset) {
    // Example:
    //    void test1() {
    //    }
    //    void test2() {
    //       <offset>
    //    }
    // We want to insert the comment at the start of the line where 'test2()' is declared.
    int nonWhiteSpaceOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), commentStartOffset - 1, " \t");
    commentStartOffset = Math.max(nonWhiteSpaceOffset, lineStartOffset);
  }

  int commentBodyRelativeOffset = 0;
  int caretOffsetToSet = -1;
  StringBuilder buffer = new StringBuilder();
  String commentPrefix = commenter.getDocumentationCommentPrefix();
  if (commentPrefix != null) {
    buffer.append(commentPrefix).append("\n");
    commentBodyRelativeOffset += commentPrefix.length() + 1;
  }

  String linePrefix = commenter.getDocumentationCommentLinePrefix();
  if (linePrefix != null) {
    buffer.append(linePrefix);
    commentBodyRelativeOffset += linePrefix.length();
    caretOffsetToSet = commentStartOffset + commentBodyRelativeOffset;
  }
  buffer.append("\n");
  commentBodyRelativeOffset++;

  String commentSuffix = commenter.getDocumentationCommentSuffix();
  if (commentSuffix != null) {
    buffer.append(commentSuffix).append("\n");
  }

  if (buffer.length() <= 0) {
    return;
  }

  document.insertString(commentStartOffset, buffer);
  PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
  docManager.commitDocument(document);

  Pair<PsiElement, PsiComment> pair = documentationProvider.parseContext(anchor);
  if (pair == null || pair.second == null) {
    return;
  }

  String stub = documentationProvider.generateDocumentationContentStub(pair.second);
  CaretModel caretModel = editor.getCaretModel();
  if (stub != null) {
    int insertionOffset = commentStartOffset + commentBodyRelativeOffset;
    document.insertString(insertionOffset, stub);
    docManager.commitDocument(document);
    pair = documentationProvider.parseContext(anchor);
  }

  if (caretOffsetToSet >= 0) {
    caretModel.moveToOffset(caretOffsetToSet);
    editor.getSelectionModel().removeSelection();
  }

  if (pair == null || pair.second == null) {
    return;
  }

  int start = Math.min(calcStartReformatOffset(pair.first), calcStartReformatOffset(pair.second));
  int end = pair.second.getTextRange().getEndOffset();

  reformatCommentKeepingEmptyTags(anchor.getContainingFile(), project, start, end);
  editor.getCaretModel().moveToOffset(document.getLineEndOffset(document.getLineNumber(editor.getCaretModel().getOffset())));

  int caretOffset = caretModel.getOffset();
  if (caretOffset > 0 && caretOffset <= document.getTextLength()) {
    char c = document.getCharsSequence().charAt(caretOffset - 1);
    if (!StringUtil.isWhiteSpace(c)) {
      document.insertString(caretOffset, " ");
      caretModel.moveToOffset(caretOffset + 1);
    }
  }
}