Couple<TextRange> (TextRange range, String commentPrefix, String commentSuffix) { CommonCodeStyleSettings settings = getLanguageSettings(); CharSequence chars = myDocument.getCharsSequence(); int startOffset = range.getStartOffset(); boolean endsProperly = CharArrayUtil.regionMatches(chars, range.getEndOffset() - commentSuffix.length(), commentSuffix); TextRange initialPrefix = TextRange.create(startOffset, startOffset + commentPrefix.length()); TextRange prefix = expandRange(initialPrefix.getStartOffset(), initialPrefix.getEndOffset()); // is expanded only in cases when the comment prefix or suffix are the only characters in the line except whitespaces // so there's no need to remove block white space if (settings.BLOCK_COMMENT_ADD_SPACE && initialPrefix.equals(prefix)) { if (StringUtil.isChar(chars, prefix.getEndOffset(), ' ')) { prefix = prefix.grown(1); } } TextRange suffix; if (endsProperly) { TextRange initialSuffix = TextRange.create(range.getEndOffset() - commentSuffix.length(), range.getEndOffset()); suffix = expandRange(initialSuffix.getStartOffset(), initialSuffix.getEndOffset()); if (settings.BLOCK_COMMENT_ADD_SPACE && initialSuffix.equals(suffix)) { int suffixSpaceIdx = suffix.getStartOffset() - 1; if (prefix.getEndOffset() <= suffixSpaceIdx && StringUtil.isChar(chars, suffixSpaceIdx, ' ')) { suffix = TextRange.create(suffixSpaceIdx, suffix.getEndOffset()); } } } else { suffix = new TextRange(range.getEndOffset(), range.getEndOffset()); } return Couple.of(prefix, suffix); }