String (Project project, PsiFile file, Editor editor, String text, RawText rawText) { Language language = file.getLanguage(); Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language); if (commenter == null) return text; String lineCommentPrefix = commenter.getLineCommentPrefix(); if (lineCommentPrefix == null) return text; Document document = editor.getDocument(); int offset = editor.getSelectionModel().getSelectionStart(); if (DocumentUtil.isAtLineEnd(offset, editor.getDocument()) && text.startsWith("\n")) return text; int lineStartOffset = DocumentUtil.getLineStartOffset(offset, document); CharSequence chars = document.getImmutableCharSequence(); int firstNonWsLineOffset = CharArrayUtil.shiftForward(chars, lineStartOffset, " \t"); if (offset < (firstNonWsLineOffset + lineCommentPrefix.length()) || !CharArrayUtil.regionMatches(chars, firstNonWsLineOffset, lineCommentPrefix)) { return text; } CodeStyleSettings codeStyleSettings = CodeStyle.getSettings(file); String lineStartReplacement = "\n" + chars.subSequence(lineStartOffset, firstNonWsLineOffset + lineCommentPrefix.length()) + (codeStyleSettings.getCommonSettings(language).LINE_COMMENT_ADD_SPACE ? " " : ""); return StringUtil.trimTrailing(text, '\n').replace("\n", lineStartReplacement); }