mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed byBlock commenting whitespace skipping in case of OuterLanguageElement (PY-1558)
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
<orderEntry type="module" module-name="platform-api" exported="" />
|
||||
<orderEntry type="module" module-name="lvcs-api" exported="" />
|
||||
<orderEntry type="library" exported="" name="NanoXML" level="project" />
|
||||
<orderEntry type="library" name="Guava" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
@@ -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. <code>false</code> 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. <code>false</code> 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<? extends PsiElement> 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 extends PsiElement> T findChildOfType(@NotNull final PsiElement element, @NotNull final Class<T> aClass) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfType(@NotNull final PsiElement element, @NotNull final Class<T> aClass) {
|
||||
return findChildOfType(element, aClass, true);
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T findChildOfType(@NotNull final PsiElement element, @NotNull final Class<T> aClass, final boolean strict) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfType(@NotNull final PsiElement element,
|
||||
@NotNull final Class<T> aClass,
|
||||
final boolean strict) {
|
||||
return findChildOfAnyType(element, strict, aClass);
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T findChildOfAnyType(@NotNull final PsiElement element, @NotNull final Class<T>... classes) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfAnyType(@NotNull final PsiElement element, @NotNull final Class<T>... classes) {
|
||||
return findChildOfAnyType(element, true, classes);
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T findChildOfAnyType(@NotNull final PsiElement element, final boolean strict, @NotNull final Class<T>... classes) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T findChildOfAnyType(@NotNull final PsiElement element,
|
||||
final boolean strict,
|
||||
@NotNull final Class<T>... classes) {
|
||||
PsiElementProcessor.FindElement<PsiElement> processor = new PsiElementProcessor.FindElement<PsiElement>() {
|
||||
@Override
|
||||
public boolean execute(PsiElement each) {
|
||||
@@ -219,22 +232,25 @@ public class PsiTreeUtil {
|
||||
return (T)processor.getFoundElement();
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T getChildOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getChildOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull public static <T extends PsiElement> T getRequiredChildOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
@NotNull
|
||||
public static <T extends PsiElement> T getRequiredChildOfType(@NotNull PsiElement element, @NotNull Class<T> 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 extends PsiElement> T[] getChildrenOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T[] getChildrenOfType(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
List<T> 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<T>();
|
||||
result.add((T)child);
|
||||
@@ -243,9 +259,10 @@ public class PsiTreeUtil {
|
||||
return result == null ? null : ArrayUtil.toObjectArray(result, aClass);
|
||||
}
|
||||
|
||||
@NotNull public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
@NotNull
|
||||
public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@NotNull PsiElement element, @NotNull Class<T> aClass) {
|
||||
List<T> result = new SmartList<T>();
|
||||
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 extends PsiElement> T getChildOfAnyType(@NotNull PsiElement element, @NotNull Class<? extends T>... classes) {
|
||||
for(PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()){
|
||||
for(Class<? extends T> aClass : classes) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getChildOfAnyType(@NotNull PsiElement element, @NotNull Class<? extends T>... classes) {
|
||||
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
for (Class<? extends T> aClass : classes) {
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T getNextSiblingOfType(@NotNull PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
for(PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()){
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getNextSiblingOfType(@NotNull PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
for (PsiElement child = sibling.getNextSibling(); child != null; child = child.getNextSibling()) {
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T getPrevSiblingOfType(@NotNull PsiElement sibling, @NotNull Class<T> aClass) {
|
||||
for(PsiElement child = sibling.getPrevSibling(); child != null; child = child.getPrevSibling()){
|
||||
if (instanceOf(aClass, child)) return (T)child;
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getPrevSiblingOfType(@NotNull PsiElement sibling, @NotNull Class<T> 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 extends PsiElement> T getParentOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getParentOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
|
||||
return getParentOfType(element, aClass, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <E extends PsiElement> E getStubOrPsiParentOfType(@Nullable PsiElement element, final Class<E> 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 extends PsiElement> T getContextOfType(@Nullable PsiElement element, @NotNull Class<T> aClass, boolean strict, Class<? extends PsiElement>... stopAt) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getContextOfType(@Nullable PsiElement element,
|
||||
@NotNull Class<T> aClass,
|
||||
boolean strict,
|
||||
Class<? extends PsiElement>... stopAt) {
|
||||
if (element == null) return null;
|
||||
if (strict) {
|
||||
element = element.getContext();
|
||||
@@ -353,7 +378,8 @@ public class PsiTreeUtil {
|
||||
|
||||
}
|
||||
|
||||
@Nullable public static <T extends PsiElement> T getContextOfType(@Nullable PsiElement element, @NotNull Class<T> aClass, boolean strict) {
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getContextOfType(@Nullable PsiElement element, @NotNull Class<T> aClass, boolean strict) {
|
||||
if (element == null) return null;
|
||||
if (strict) {
|
||||
element = element.getContext();
|
||||
@@ -382,7 +408,10 @@ public class PsiTreeUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T extends PsiElement> T getParentOfType(@Nullable PsiElement element, @NotNull Class<T> aClass, boolean strict, @NotNull Class<? extends PsiElement>... stopAt) {
|
||||
public static <T extends PsiElement> T getParentOfType(@Nullable PsiElement element,
|
||||
@NotNull Class<T> aClass,
|
||||
boolean strict,
|
||||
@NotNull Class<? extends PsiElement>... 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 <T extends PsiElement> Collection<T> collectElementsOfType(@Nullable PsiElement element, final @NotNull Class<T> ... classes) {
|
||||
public static <T extends PsiElement> Collection<T> collectElementsOfType(@Nullable PsiElement element,
|
||||
final @NotNull Class<T>... classes) {
|
||||
PsiElementProcessor.CollectFilteredElements<T> processor = new PsiElementProcessor.CollectFilteredElements<T>(new PsiElementFilter() {
|
||||
|
||||
@Override
|
||||
public boolean isAccepted(PsiElement element) {
|
||||
for (Class<T> clazz: classes) {
|
||||
for (Class<T> 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 extends PsiElement> T findElementOfClassAtOffset (@NotNull PsiFile file, int offset, @NotNull Class<T> clazz, boolean strictStart) {
|
||||
public static <T extends PsiElement> T findElementOfClassAtOffset(@NotNull PsiFile file,
|
||||
int offset,
|
||||
@NotNull Class<T> 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 extends PsiElement> T findElementOfClassAtRange (@NotNull PsiFile file, int startOffset, int endOffset, @NotNull Class<T> clazz) {
|
||||
public static <T extends PsiElement> T findElementOfClassAtRange(@NotNull PsiFile file,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
@NotNull Class<T> 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<PsiElement> getInjectedElements(OuterLanguageElement outerLanguageElement) {
|
||||
PsiElement psi = outerLanguageElement.getContainingFile().getViewProvider().getPsi(outerLanguageElement.getLanguage());
|
||||
TextRange injectionRange = outerLanguageElement.getTextRange();
|
||||
List<PsiElement> res = Lists.newArrayList();
|
||||
|
||||
for (PsiElement element = psi.findElementAt(injectionRange.getStartOffset());
|
||||
element != null && injectionRange.intersectsStrict(element.getTextRange());
|
||||
element = element.getNextSibling()) {
|
||||
res.add(element);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+86
-40
@@ -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<PsiElement> 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<TextRange, TextRange> pair = findCommentBlock(new TextRange(start + startOffset, end + startOffset), commentPrefix, commentSuffix);
|
||||
Pair<TextRange, TextRange> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user