void (PsiElement last, boolean commentHere, List<PsiComment> comments) { try { final String keyword = myOptions.getKeyword(); final LinkedHashSet<CommentRange> found = new LinkedHashSet<>(); Document doc = null; if (!StringUtil.isEmpty(keyword)) { Pattern pattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE); doc = FileDocumentManager.getInstance().getDocument(getFile().getVirtualFile()); for (int i = 0; i < comments.size(); i++) { PsiComment comment = comments.get(i); String text = comment.getText(); Matcher match = pattern.matcher(text); if (match.find()) { found.add(getLineCopyrightComments(comments, doc, i, comment)); } } } // Default insertion point to just before user chosen marker (package, import, class) PsiElement point = last; if (commentHere && !comments.isEmpty() && langOpts.isRelativeBefore()) { // Insert before first comment within this section of code. point = comments.get(0); } if (commentHere && found.size() == 1) { CommentRange range = found.iterator().next(); // Is the comment in the right place? if (langOpts.isRelativeBefore() && range.getFirst() == comments.get(0) || !langOpts.isRelativeBefore() && range.getLast() == comments.get(comments.size() - 1)) { // Check to see if current copyright comment matches new one. String newComment = getCommentText("", ""); resetCommentText(); String oldComment = getCommentText(doc, range); if (!allowToReplaceRegexp(oldComment)) return; if (newComment.trim().equals(oldComment)) { if (!getLanguageOptions().isAddBlankAfter()) { // TODO - do we need option to remove blank line after? return; // Nothing to do since the comment is the same } int totalNewline = 0; PsiElement next = getNextSibling(range.getLast()); while (next != null && totalNewline <= 1) { final String text = next.getText(); if (!StringUtil.isEmptyOrSpaces(text)) { break; } totalNewline += countNewline(text); } if (totalNewline > 1) { return; } point = range.getFirst(); } else if (!newComment.isEmpty()) { int start = range.getFirst().getTextRange().getStartOffset(); int end = range.getLast().getTextRange().getEndOffset(); addAction(new CommentAction(CommentAction.ACTION_REPLACE, start, end)); return; } } } for (CommentRange range : found) { // Remove the old copyright int start = range.getFirst().getTextRange().getStartOffset(); int end = range.getLast().getTextRange().getEndOffset(); // If this is the only comment then remove the whitespace after unless there is none before if (range.getFirst() == comments.get(0) && range.getLast() == comments.get(comments.size() - 1)) { int startLen = 0; if (getPreviousSibling(range.getFirst()) instanceof PsiWhiteSpace) { startLen = countNewline(getPreviousSibling(range.getFirst()).getText()); } int endLen = 0; if (getNextSibling(range.getLast()) instanceof PsiWhiteSpace) { endLen = countNewline(getNextSibling(range.getLast()).getText()); } if (startLen == 1 && getPreviousSibling(range.getFirst()).getTextRange().getStartOffset() > 0) { start = getPreviousSibling(range.getFirst()).getTextRange().getStartOffset(); } else if (endLen > 0) { end = getNextSibling(range.getLast()).getTextRange().getEndOffset(); } } // If this is the first or middle comment then remove the whitespace after the comment else if (getNextSibling(range.getLast()) instanceof PsiWhiteSpace) { end = getNextSibling(range.getLast()).getTextRange().getEndOffset(); } if (!allowToReplaceRegexp(getCommentText(doc, range))) continue; addAction(new CommentAction(CommentAction.ACTION_DELETE, start, end)); } // Finally add the comment if user chose this section. if (commentHere) { String suffix = "\n"; if (point != last && getPreviousSibling(point) != null && getPreviousSibling(point) instanceof PsiWhiteSpace) { suffix = getPreviousSibling(point).getText(); if (countNewline(suffix) == 1) { suffix = '\n' + suffix; } } if (point != last && getPreviousSibling(point) == null) { suffix = "\n\n"; } if (getLanguageOptions().isAddBlankAfter() && countNewline(suffix) == 1) { suffix += "\n"; } String prefix = ""; if(getLanguageOptions().isAddBlankBefore()){ prefix += "\n"; } if (getPreviousSibling(point) != null) { if (getPreviousSibling(point) instanceof PsiComment) { prefix += "\n\n"; } if (getPreviousSibling(point) instanceof PsiWhiteSpace && getPreviousSibling(getPreviousSibling(point)) != null && getPreviousSibling(getPreviousSibling(point)) instanceof PsiComment) { String ws = getPreviousSibling(point).getText(); int cnt = countNewline(ws); if (cnt == 1) { prefix += "\n"; } } } int pos = 0; if (point != null) { final TextRange textRange = point.getTextRange(); if (textRange != null) { pos = textRange.getStartOffset(); } } addAction(new CommentAction(pos, prefix, suffix)); } } catch (PatternSyntaxException ignore) { } catch (ProcessCanceledException e) { throw e; } catch (Exception e) { LOG.error(e); } }