From 9cbad7d279e17d2df1be988aeab849a06b59f683 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Thu, 9 Sep 2010 15:34:25 +0400 Subject: [PATCH] fixed byBlock commenting whitespace skipping in case of OuterLanguageElement (PY-1558) --- platform/lang-api/lang-api.iml | 1 + .../com/intellij/psi/util/PsiTreeUtil.java | 151 ++++++++++++------ .../CommentByBlockCommentHandler.java | 126 ++++++++++----- 3 files changed, 188 insertions(+), 90 deletions(-) diff --git a/platform/lang-api/lang-api.iml b/platform/lang-api/lang-api.iml index 7a4cdeb5f60d..2f7d0d01747c 100644 --- a/platform/lang-api/lang-api.iml +++ b/platform/lang-api/lang-api.iml @@ -10,6 +10,7 @@ + diff --git a/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java b/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java index ff6393c01359..52823604a978 100644 --- a/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java +++ b/platform/lang-api/src/com/intellij/psi/util/PsiTreeUtil.java @@ -15,6 +15,7 @@ */ package com.intellij.psi.util; +import com.google.common.collect.Lists; import com.intellij.lang.Language; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Key; @@ -23,6 +24,7 @@ import com.intellij.psi.*; import com.intellij.psi.scope.PsiScopeProcessor; import com.intellij.psi.search.PsiElementProcessor; import com.intellij.psi.stubs.StubBase; +import com.intellij.psi.templateLanguages.OuterLanguageElement; import com.intellij.util.ArrayUtil; import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; @@ -44,9 +46,10 @@ public class PsiTreeUtil { /** * Checks whether one element in the psi tree is under another. + * * @param ancestor parent candidate. false will be returned if ancestor is null. - * @param element child candidate - * @param strict whether return true if ancestor and parent are the same. + * @param element child candidate + * @param strict whether return true if ancestor and parent are the same. * @return true if element has ancestor as its parent somewhere in the hierarchy and false otherwise. */ public static boolean isAncestor(@Nullable PsiElement ancestor, @NotNull PsiElement element, boolean strict) { @@ -67,11 +70,13 @@ public class PsiTreeUtil { parent = parent.getParent(); } } + /** * Checks whether one element in the psi tree is under another in {@link com.intellij.psi.PsiElement#getContext()} hierarchy. + * * @param ancestor parent candidate. false will be returned if ancestor is null. - * @param element child candidate - * @param strict whether return true if ancestor and parent are the same. + * @param element child candidate + * @param strict whether return true if ancestor and parent are the same. * @return true if element has ancestor as its parent somewhere in the hierarchy and false otherwise. */ public static boolean isContextAncestor(@Nullable PsiElement ancestor, @NotNull PsiElement element, boolean strict) { @@ -91,7 +96,7 @@ public class PsiTreeUtil { @Nullable public static PsiElement findCommonParent(@NotNull List elements) { - if (elements.isEmpty()) return null; + if (elements.isEmpty()) return null; PsiElement toReturn = null; for (PsiElement element : elements) { if (element == null) continue; @@ -104,7 +109,7 @@ public class PsiTreeUtil { @Nullable public static PsiElement findCommonParent(@NotNull PsiElement... elements) { - if (elements.length == 0) return null; + if (elements.length == 0) return null; PsiElement toReturn = null; for (PsiElement element : elements) { if (element == null) continue; @@ -118,7 +123,7 @@ public class PsiTreeUtil { @Nullable public static PsiElement findCommonParent(@NotNull PsiElement element1, @NotNull PsiElement element2) { // optimization - if(element1 == element2) return element1; + if (element1 == element2) return element1; final PsiFile containingFile = element1.getContainingFile(); final PsiElement topLevel = containingFile == element2.getContainingFile() ? containingFile : null; @@ -148,7 +153,7 @@ public class PsiTreeUtil { @Nullable public static PsiElement findCommonContext(@NotNull PsiElement... elements) { - if (elements.length == 0) return null; + if (elements.length == 0) return null; PsiElement toReturn = elements[0]; for (int i = 1; i < elements.length; i++) { toReturn = findCommonContext(toReturn, elements[i]); @@ -161,7 +166,7 @@ public class PsiTreeUtil { @Nullable public static PsiElement findCommonContext(@NotNull PsiElement element1, @NotNull PsiElement element2) { // optimization - if(element1 == element2) return element1; + if (element1 == element2) return element1; final PsiFile containingFile = element1.getContainingFile(); final PsiElement topLevel = containingFile == element2.getContainingFile() ? containingFile : null; @@ -189,19 +194,27 @@ public class PsiTreeUtil { return parents; } - @Nullable public static T findChildOfType(@NotNull final PsiElement element, @NotNull final Class aClass) { + @Nullable + public static T findChildOfType(@NotNull final PsiElement element, @NotNull final Class aClass) { return findChildOfType(element, aClass, true); } - @Nullable public static T findChildOfType(@NotNull final PsiElement element, @NotNull final Class aClass, final boolean strict) { + @Nullable + public static T findChildOfType(@NotNull final PsiElement element, + @NotNull final Class aClass, + final boolean strict) { return findChildOfAnyType(element, strict, aClass); } - @Nullable public static T findChildOfAnyType(@NotNull final PsiElement element, @NotNull final Class... classes) { + @Nullable + public static T findChildOfAnyType(@NotNull final PsiElement element, @NotNull final Class... classes) { return findChildOfAnyType(element, true, classes); } - @Nullable public static T findChildOfAnyType(@NotNull final PsiElement element, final boolean strict, @NotNull final Class... classes) { + @Nullable + public static T findChildOfAnyType(@NotNull final PsiElement element, + final boolean strict, + @NotNull final Class... classes) { PsiElementProcessor.FindElement processor = new PsiElementProcessor.FindElement() { @Override public boolean execute(PsiElement each) { @@ -219,22 +232,25 @@ public class PsiTreeUtil { return (T)processor.getFoundElement(); } - @Nullable public static T getChildOfType(@NotNull PsiElement element, @NotNull Class aClass) { - for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){ + @Nullable + public static T getChildOfType(@NotNull PsiElement element, @NotNull Class aClass) { + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (instanceOf(aClass, child)) return (T)child; } return null; } - @NotNull public static T getRequiredChildOfType(@NotNull PsiElement element, @NotNull Class aClass) { + @NotNull + public static T getRequiredChildOfType(@NotNull PsiElement element, @NotNull Class aClass) { final T child = getChildOfType(element, aClass); - assert child != null: "Missing required child of type " + aClass.getName(); + assert child != null : "Missing required child of type " + aClass.getName(); return child; } - @Nullable public static T[] getChildrenOfType(@NotNull PsiElement element, @NotNull Class aClass) { + @Nullable + public static T[] getChildrenOfType(@NotNull PsiElement element, @NotNull Class aClass) { List result = null; - for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){ + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (instanceOf(aClass, child)) { if (result == null) result = new SmartList(); result.add((T)child); @@ -243,9 +259,10 @@ public class PsiTreeUtil { return result == null ? null : ArrayUtil.toObjectArray(result, aClass); } - @NotNull public static List getChildrenOfTypeAsList(@NotNull PsiElement element, @NotNull Class aClass) { + @NotNull + public static List getChildrenOfTypeAsList(@NotNull PsiElement element, @NotNull Class aClass) { List result = new SmartList(); - for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){ + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (instanceOf(aClass, child)) { result.add((T)child); } @@ -279,25 +296,28 @@ public class PsiTreeUtil { * @return the element, or null if none was found. * @since 5.1 */ - @Nullable public static T getChildOfAnyType(@NotNull PsiElement element, @NotNull Class... classes) { - for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){ - for(Class aClass : classes) { + @Nullable + public static T getChildOfAnyType(@NotNull PsiElement element, @NotNull Class... classes) { + for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) { + for (Class aClass : classes) { if (instanceOf(aClass, child)) return (T)child; } } return null; } - @Nullable public static T getNextSiblingOfType(@NotNull PsiElement sibling, @NotNull Class aClass) { - for(PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()){ + @Nullable + public static T getNextSiblingOfType(@NotNull PsiElement sibling, @NotNull Class aClass) { + for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) { if (instanceOf(aClass, child)) return (T)child; } return null; } - @Nullable public static T getPrevSiblingOfType(@NotNull PsiElement sibling, @NotNull Class aClass) { - for(PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()){ - if (instanceOf(aClass, child)) return (T)child; + @Nullable + public static T getPrevSiblingOfType(@NotNull PsiElement sibling, @NotNull Class aClass) { + for (PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()) { + if (instanceOf(aClass, child)) return (T)child; } return null; } @@ -316,14 +336,15 @@ public class PsiTreeUtil { return answer; } - @Nullable public static T getParentOfType(@Nullable PsiElement element, @NotNull Class aClass) { + @Nullable + public static T getParentOfType(@Nullable PsiElement element, @NotNull Class aClass) { return getParentOfType(element, aClass, true); } @Nullable public static E getStubOrPsiParentOfType(@Nullable PsiElement element, final Class parentClass) { if (element instanceof StubBasedPsiElement) { - StubBase stub = (StubBase)((StubBasedPsiElement) element).getStub(); + StubBase stub = (StubBase)((StubBasedPsiElement)element).getStub(); if (stub != null) { //noinspection unchecked return (E)stub.getParentStubOfType(parentClass); @@ -333,7 +354,11 @@ public class PsiTreeUtil { return getParentOfType(element, parentClass); } - @Nullable public static T getContextOfType(@Nullable PsiElement element, @NotNull Class aClass, boolean strict, Class... stopAt) { + @Nullable + public static T getContextOfType(@Nullable PsiElement element, + @NotNull Class aClass, + boolean strict, + Class... stopAt) { if (element == null) return null; if (strict) { element = element.getContext(); @@ -353,7 +378,8 @@ public class PsiTreeUtil { } - @Nullable public static T getContextOfType(@Nullable PsiElement element, @NotNull Class aClass, boolean strict) { + @Nullable + public static T getContextOfType(@Nullable PsiElement element, @NotNull Class aClass, boolean strict) { if (element == null) return null; if (strict) { element = element.getContext(); @@ -382,7 +408,10 @@ public class PsiTreeUtil { } @Nullable - public static T getParentOfType(@Nullable PsiElement element, @NotNull Class aClass, boolean strict, @NotNull Class... stopAt) { + public static T getParentOfType(@Nullable PsiElement element, + @NotNull Class aClass, + boolean strict, + @NotNull Class... stopAt) { if (element == null) return null; if (strict) { element = element.getParent(); @@ -399,7 +428,8 @@ public class PsiTreeUtil { return (T)element; } - @Nullable public static PsiElement skipSiblingsForward (@Nullable PsiElement element, @NotNull Class... elementClasses) { + @Nullable + public static PsiElement skipSiblingsForward(@Nullable PsiElement element, @NotNull Class... elementClasses) { if (element == null) return null; NextSibling: for (PsiElement e = element.getNextSibling(); e != null; e = e.getNextSibling()) { @@ -412,7 +442,7 @@ public class PsiTreeUtil { } @Nullable - public static PsiElement skipSiblingsBackward (@Nullable PsiElement element, @NotNull Class... elementClasses) { + public static PsiElement skipSiblingsBackward(@Nullable PsiElement element, @NotNull Class... elementClasses) { if (element == null) return null; NextSibling: for (PsiElement e = element.getPrevSibling(); e != null; e = e.getPrevSibling()) { @@ -467,12 +497,13 @@ public class PsiTreeUtil { } @NotNull - public static Collection collectElementsOfType(@Nullable PsiElement element, final @NotNull Class ... classes) { + public static Collection collectElementsOfType(@Nullable PsiElement element, + final @NotNull Class... classes) { PsiElementProcessor.CollectFilteredElements processor = new PsiElementProcessor.CollectFilteredElements(new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { - for (Class clazz: classes) { + for (Class clazz : classes) { if (clazz.isInstance(element)) { return true; } @@ -503,7 +534,7 @@ public class PsiTreeUtil { boolean failed = false; for (int j = 0; j < elements.length; j++) { PsiElement element = elements[j]; - if (i != j && isAncestor(element, rootCandidate, true)) { + if (i != j && isAncestor(element, rootCandidate, true)) { failed = true; break; } @@ -552,7 +583,8 @@ public class PsiTreeUtil { if (marker.equals(root.getCopyableUserData(MARKER))) { root.putCopyableUserData(MARKER, null); return root; - } else { + } + else { PsiElement child = root.getFirstChild(); while (child != null) { final PsiElement result = releaseMark(child, marker); @@ -564,7 +596,10 @@ public class PsiTreeUtil { } @Nullable - public static T findElementOfClassAtOffset (@NotNull PsiFile file, int offset, @NotNull Class clazz, boolean strictStart) { + public static T findElementOfClassAtOffset(@NotNull PsiFile file, + int offset, + @NotNull Class clazz, + boolean strictStart) { final PsiElement[] psiRoots = file.getPsiRoots(); T result = null; for (PsiElement root : psiRoots) { @@ -589,7 +624,10 @@ public class PsiTreeUtil { * @return maximal element of specified Class starting at startOffset exactly and ending not farther than endOffset */ @Nullable - public static T findElementOfClassAtRange (@NotNull PsiFile file, int startOffset, int endOffset, @NotNull Class clazz) { + public static T findElementOfClassAtRange(@NotNull PsiFile file, + int startOffset, + int endOffset, + @NotNull Class clazz) { final FileViewProvider viewProvider = file.getViewProvider(); T result = null; for (Language lang : viewProvider.getLanguages()) { @@ -638,32 +676,32 @@ public class PsiTreeUtil { } @Nullable - public static PsiElement prevLeaf(@NotNull PsiElement current){ + public static PsiElement prevLeaf(@NotNull PsiElement current) { final PsiElement prevSibling = current.getPrevSibling(); - if(prevSibling != null) return lastChild(prevSibling); + if (prevSibling != null) return lastChild(prevSibling); final PsiElement parent = current.getParent(); - if(parent == null || parent instanceof PsiFile) return null; + if (parent == null || parent instanceof PsiFile) return null; return prevLeaf(parent); } @Nullable - public static PsiElement nextLeaf(@NotNull PsiElement current){ + public static PsiElement nextLeaf(@NotNull PsiElement current) { final PsiElement nextSibling = current.getNextSibling(); - if(nextSibling != null) return firstChild(nextSibling); + if (nextSibling != null) return firstChild(nextSibling); final PsiElement parent = current.getParent(); - if(parent == null || parent instanceof PsiFile) return null; + if (parent == null || parent instanceof PsiFile) return null; return nextLeaf(parent); } public static PsiElement lastChild(@NotNull PsiElement element) { PsiElement lastChild = element.getLastChild(); - if(lastChild != null) return lastChild(lastChild); + if (lastChild != null) return lastChild(lastChild); return element; } public static PsiElement firstChild(@NotNull final PsiElement element) { PsiElement child = element.getFirstChild(); - if(child != null) return firstChild(child); + if (child != null) return firstChild(child); return element; } @@ -761,4 +799,17 @@ public class PsiTreeUtil { throw new AssertionError(descendant + " is not a descendant of " + ancestor); } + public static List getInjectedElements(OuterLanguageElement outerLanguageElement) { + PsiElement psi = outerLanguageElement.getContainingFile().getViewProvider().getPsi(outerLanguageElement.getLanguage()); + TextRange injectionRange = outerLanguageElement.getTextRange(); + List res = Lists.newArrayList(); + + for (PsiElement element = psi.findElementAt(injectionRange.getStartOffset()); + element != null && injectionRange.intersectsStrict(element.getTextRange()); + element = element.getNextSibling()) { + res.add(element); + } + return res; + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java index b9f571c79a71..6d08eeda83fe 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java @@ -38,6 +38,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.codeStyle.Indent; import com.intellij.psi.templateLanguages.MultipleLangCommentProvider; +import com.intellij.psi.templateLanguages.OuterLanguageElement; import com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtilBase; @@ -69,12 +70,12 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.comment.block"); final Commenter commenter = findCommenter(myFile, myEditor); if (commenter == null) return; - + final SelectionModel selectionModel = myEditor.getSelectionModel(); - + final String prefix; final String suffix; - + if (commenter instanceof SelfManagingCommenter) { final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter)commenter; mySelfManagedCommenterData = selfManagingCommenter.createBlockCommentingState( @@ -89,16 +90,17 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { } prefix = selfManagingCommenter.getBlockCommentPrefix( - selectionModel.getSelectionStart(), - myDocument, + selectionModel.getSelectionStart(), + myDocument, mySelfManagedCommenterData ); suffix = selfManagingCommenter.getBlockCommentSuffix( - selectionModel.getSelectionEnd(), - myDocument, + selectionModel.getSelectionEnd(), + myDocument, mySelfManagedCommenterData ); - } else { + } + else { prefix = commenter.getBlockCommentPrefix(); suffix = commenter.getBlockCommentSuffix(); } @@ -169,17 +171,44 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { if (!model.hasSelection()) { return true; } - TextRange range = new TextRange(model.getSelectionStart(), model.getSelectionEnd() - 1); - for (PsiElement element = myFile.findElementAt(range.getStartOffset()); - element != null && range.intersects(element.getTextRange()); + TextRange range + = new TextRange(model.getSelectionStart(), model.getSelectionEnd() - 1); + for (PsiElement element = myFile.findElementAt(range.getStartOffset()); element != null && range.intersects(element.getTextRange()); element = element.getNextSibling()) { - if (!(element instanceof PsiWhiteSpace || PsiTreeUtil.getParentOfType(element, PsiComment.class, false) != null)) { - return false; + if (element instanceof OuterLanguageElement) { + List injectedElements = PsiTreeUtil.getInjectedElements((OuterLanguageElement)element); + for (PsiElement el : injectedElements) { + if (!isWhiteSpaceOrComment(el, range)) { + return false; + } + } + + } + else { + if (!isWhiteSpaceOrComment(element)) { + return false; + } } } return true; } + private boolean isWhiteSpaceOrComment(@NotNull PsiElement element, @NotNull TextRange range) { + TextRange intersection = range.intersection(element.getTextRange()); + if (intersection == null) { + return false; + } + intersection = TextRange.from(Math.max(intersection.getStartOffset() - element.getTextRange().getStartOffset(), 0), + intersection.getEndOffset()-element.getTextRange().getStartOffset()); + return isWhiteSpaceOrComment(element) || + intersection.substring(element.getText()).trim().length() == 0; + } + + private boolean isWhiteSpaceOrComment(PsiElement element) { + return element instanceof PsiWhiteSpace || + PsiTreeUtil.getParentOfType(element, PsiComment.class, false) != null; + } + @Nullable private TextRange findCommentedRange(final Commenter commenter) { final CharSequence text = myDocument.getCharsSequence(); @@ -203,37 +232,39 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { final SelectionModel selectionModel = myEditor.getSelectionModel(); if (commenter instanceof SelfManagingCommenter) { SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter)commenter; - + prefix = selfManagingCommenter.getBlockCommentPrefix( - selectionModel.getSelectionStart(), - myDocument, + selectionModel.getSelectionStart(), + myDocument, mySelfManagedCommenterData ); suffix = selfManagingCommenter.getBlockCommentSuffix( - selectionModel.getSelectionEnd(), - myDocument, + selectionModel.getSelectionEnd(), + myDocument, mySelfManagedCommenterData ); - } else { + } + else { prefix = trim(commenter.getBlockCommentPrefix()); suffix = trim(commenter.getBlockCommentSuffix()); } if (prefix == null || suffix == null) return null; TextRange commentedRange; - + if (commenter instanceof SelfManagingCommenter) { commentedRange = ((SelfManagingCommenter)commenter).getBlockCommentRange( - selectionModel.getSelectionStart(), + selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), - myDocument, + myDocument, mySelfManagedCommenterData ); - } else { + } + else { if (!testSelectionForNonComments()) { return null; } - + commentedRange = getSelectedComments(text, prefix, suffix); if (commentedRange == null) { PsiElement comment = findCommentAtCaret(); @@ -315,7 +346,7 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { } PsiElement elt = myFile.getViewProvider().findElementAt(offset); if (elt == null) return null; - PsiElement comment = PsiTreeUtil.getParentOfType(elt, PsiComment.class, false); + PsiElement comment = PsiTreeUtil.getParentOfType(elt, PsiComment.class, false); if (comment == null || selectionModel.hasSelection() && !range.contains(comment.getTextRange())) { return null; } @@ -350,13 +381,14 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { space = ""; } final StringBuilder nestingPrefix = new StringBuilder(space).append(commentPrefix); - if (!commentPrefix.endsWith("\n")){ + if (!commentPrefix.endsWith("\n")) { nestingPrefix.append("\n"); } final StringBuilder nestingSuffix = new StringBuilder(space); nestingSuffix.append(commentSuffix.startsWith("\n") ? commentSuffix.substring(1) : commentSuffix); nestingSuffix.append("\n"); - TextRange range = insertNestedComments(chars, startOffset, endOffset, nestingPrefix.toString(), nestingSuffix.toString(), commenter); + TextRange range = + insertNestedComments(chars, startOffset, endOffset, nestingPrefix.toString(), nestingSuffix.toString(), commenter); myEditor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset()); //myEditor.getSelectionModel().removeSelection(); LogicalPosition pos = new LogicalPosition(caretPosition.line + 1, caretPosition.column); @@ -374,7 +406,12 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } - private int doBoundCommentingAndGetShift(int offset, String commented, int skipLength, String toInsert, boolean skipBrace, TextRange selection) { + private int doBoundCommentingAndGetShift(int offset, + String commented, + int skipLength, + String toInsert, + boolean skipBrace, + TextRange selection) { if (commented == null && (offset == selection.getStartOffset() || offset + (skipBrace ? skipLength : 0) == selection.getEndOffset())) { return 0; } @@ -388,13 +425,18 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { } } - private TextRange insertNestedComments(CharSequence chars, int startOffset, int endOffset, String commentPrefix, String commentSuffix, Commenter commenter) { + private TextRange insertNestedComments(CharSequence chars, + int startOffset, + int endOffset, + String commentPrefix, + String commentSuffix, + Commenter commenter) { if (commenter instanceof SelfManagingCommenter) { final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter)commenter; return selfManagingCommenter.insertBlockComment( - startOffset, - endOffset, - myDocument, + startOffset, + endOffset, + myDocument, mySelfManagedCommenterData ); } @@ -416,7 +458,9 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { } } int shift = 0; - if (!(commentedSuffix == null && !nestedCommentSuffixes.isEmpty() && nestedCommentSuffixes.get(nestedCommentSuffixes.size() - 1) + commentSuffix.length() == endOffset)) { + if (!(commentedSuffix == null && + !nestedCommentSuffixes.isEmpty() && + nestedCommentSuffixes.get(nestedCommentSuffixes.size() - 1) + commentSuffix.length() == endOffset)) { myDocument.insertString(endOffset, commentSuffix); shift += commentSuffix.length(); } @@ -526,7 +570,7 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { } } - private TextRange expandRange(int delOffset1, int delOffset2) { + private TextRange expandRange(int delOffset1, int delOffset2) { CharSequence chars = myDocument.getCharsSequence(); int offset1 = CharArrayUtil.shiftBackward(chars, delOffset1 - 1, " \t"); if (offset1 < 0 || chars.charAt(offset1) == '\n' || chars.charAt(offset1) == '\r') { @@ -565,14 +609,14 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { if (commenter instanceof SelfManagingCommenter) { final SelfManagingCommenter selfManagingCommenter = (SelfManagingCommenter)commenter; selfManagingCommenter.uncommentBlockComment( - range.getStartOffset(), - range.getEndOffset(), - myDocument, + range.getStartOffset(), + range.getEndOffset(), + myDocument, mySelfManagedCommenterData ); return; } - + String text = myDocument.getCharsSequence().subSequence(range.getStartOffset(), range.getEndOffset()).toString(); int startOffset = range.getStartOffset(); //boolean endsProperly = CharArrayUtil.regionMatches(chars, range.getEndOffset() - commentSuffix.length(), commentSuffix); @@ -587,7 +631,8 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { position = start; int end = getNearest(text, commentSuffix, position + commentPrefix.length()) + commentSuffix.length(); position = end; - Pair pair = findCommentBlock(new TextRange(start + startOffset, end + startOffset), commentPrefix, commentSuffix); + Pair pair = + findCommentBlock(new TextRange(start + startOffset, end + startOffset), commentPrefix, commentSuffix); ranges.add(pair); } @@ -597,7 +642,8 @@ public class CommentByBlockCommentHandler implements CodeInsightActionHandler { int shift = toDelete.first.getEndOffset() - toDelete.first.getStartOffset(); myDocument.deleteString(toDelete.second.getStartOffset() - shift, toDelete.second.getEndOffset() - shift); if (commenter.getCommentedBlockCommentPrefix() != null) { - commentNestedComments(myDocument, new TextRange(toDelete.first.getEndOffset() - shift, toDelete.second.getStartOffset() - shift), commenter); + commentNestedComments(myDocument, new TextRange(toDelete.first.getEndOffset() - shift, toDelete.second.getStartOffset() - shift), + commenter); } } }