kill nextLeaf/prevLeaf duplicates

This commit is contained in:
peter
2011-06-06 17:27:41 +02:00
parent ea622c99f3
commit 5f3617f1bc
7 changed files with 20 additions and 171 deletions
@@ -23,6 +23,7 @@ import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.meta.PsiMetaOwner;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.PairProcessor;
import com.intellij.util.ProcessingContext;
import com.intellij.util.ReflectionCache;
@@ -159,18 +160,10 @@ public abstract class PsiElementPattern<T extends PsiElement,Self extends PsiEle
public boolean accepts(@NotNull T t, final ProcessingContext context) {
PsiElement element = t;
while (true) {
do {
while (element != null && element.getPrevSibling() == null) {
element = element.getParent();
}
if (element == null) break;
element = element.getPrevSibling();
while (element != null && element.getLastChild() != null) {
element = element.getLastChild();
}
element = PsiTreeUtil.prevLeaf(element);
if (element != null && element.getTextLength() == 0) {
continue;
}
while (element != null && element.getTextLength() == 0);
if (!skip.getCondition().accepts(element, context)) {
return pattern.getCondition().accepts(element, context);
@@ -186,18 +179,11 @@ public abstract class PsiElementPattern<T extends PsiElement,Self extends PsiEle
public boolean accepts(@NotNull T t, final ProcessingContext context) {
PsiElement element = t;
while (true) {
do {
while (element != null && element.getNextSibling() == null) {
element = element.getParent();
}
if (element == null) break;
element = element.getNextSibling();
while (element != null && element.getFirstChild() != null) {
element = element.getFirstChild();
}
element = PsiTreeUtil.nextLeaf(element);
if (element != null && element.getTextLength() == 0) {
continue;
}
while (element != null && element.getTextLength() == 0);
if (!skip.getCondition().accepts(element, context)) {
return pattern.getCondition().accepts(element, context);
}
@@ -21,8 +21,8 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.TreeUtil;
class CStyleCommentPredicate implements PsiElementPredicate{
@@ -38,7 +38,7 @@ class CStyleCommentPredicate implements PsiElementPredicate{
if(!JavaTokenType.C_STYLE_COMMENT.equals(type)){
return false;
}
final PsiElement sibling = TreeUtil.getNextLeaf(comment);
final PsiElement sibling = PsiTreeUtil.nextLeaf(comment);
if(!(sibling instanceof PsiWhiteSpace))
{
return false;
@@ -47,4 +47,4 @@ class CStyleCommentPredicate implements PsiElementPredicate{
return whitespaceText.indexOf((int) '\n') >=0 ||
whitespaceText.indexOf((int) '\r') >= 0;
}
}
}
@@ -19,8 +19,8 @@ import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.TreeUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -48,7 +48,7 @@ class CommentOnLineWithSourcePredicate implements PsiElementPredicate{
!JavaTokenType.END_OF_LINE_COMMENT.equals(type)){
return false; // can't move JSP comments
}
final PsiElement prevSibling = TreeUtil.getPrevLeaf(element);
final PsiElement prevSibling = PsiTreeUtil.prevLeaf(element);
if(!(prevSibling instanceof PsiWhiteSpace)){
return true;
}
@@ -57,7 +57,7 @@ class CommentOnLineWithSourcePredicate implements PsiElementPredicate{
prevSiblingText.indexOf((int) '\r') < 0){
return true;
}
final PsiElement nextSibling = TreeUtil.getNextLeaf(element);
final PsiElement nextSibling = PsiTreeUtil.nextLeaf(element);
if(!(nextSibling instanceof PsiWhiteSpace)){
return true;
}
@@ -75,4 +75,4 @@ class CommentOnLineWithSourcePredicate implements PsiElementPredicate{
});
return containsInjected.get();
}
}
}
@@ -17,10 +17,10 @@ package com.siyeh.ipp.comment;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import com.siyeh.ipp.psiutils.TreeUtil;
import org.jetbrains.annotations.NotNull;
public class MoveCommentToSeparateLineIntention extends Intention {
@@ -36,7 +36,7 @@ public class MoveCommentToSeparateLineIntention extends Intention {
PsiElement elementToCheck = selectedComment;
final PsiWhiteSpace whiteSpace;
while (true) {
elementToCheck = TreeUtil.getPrevLeaf(elementToCheck);
elementToCheck = PsiTreeUtil.prevLeaf(elementToCheck);
if (elementToCheck == null) {
return;
}
@@ -73,4 +73,4 @@ public class MoveCommentToSeparateLineIntention extends Intention {
private static boolean containsLineBreak(String text) {
return text.indexOf((int)'\n') >= 0 || text.indexOf((int)'\r') >= 0;
}
}
}
@@ -1,69 +0,0 @@
/*
* Copyright 2003-2005 Dave Griffith
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.psiutils;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
public class TreeUtil{
private TreeUtil(){
super();
}
@Nullable
public static PsiElement getNextLeaf(PsiElement element){
if(element == null){
return null;
}
final PsiElement sibling = element.getNextSibling();
if(sibling == null){
final PsiElement parent = element.getParent();
return getNextLeaf(parent);
}
return getFirstLeaf(sibling);
}
private static PsiElement getFirstLeaf(PsiElement element){
final PsiElement[] children = element.getChildren();
if(children.length == 0){
return element;
}
return getFirstLeaf(children[0]);
}
@Nullable
public static PsiElement getPrevLeaf(PsiElement element){
if(element == null || element instanceof PsiFile){
return null;
}
final PsiElement sibling = element.getPrevSibling();
if(sibling == null){
final PsiElement parent = element.getParent();
return getPrevLeaf(parent);
}
return getLastLeaf(sibling);
}
private static PsiElement getLastLeaf(PsiElement element){
final PsiElement[] children = element.getChildren();
if(children.length == 0){
return element;
}
return getLastLeaf(children[children.length-1]);
}
}
@@ -17,11 +17,10 @@ package org.jetbrains.plugins.groovy.intentions.comments;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
import org.jetbrains.plugins.groovy.intentions.utils.TreeUtil;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
class CStyleCommentPredicate implements PsiElementPredicate {
@@ -38,7 +37,7 @@ class CStyleCommentPredicate implements PsiElementPredicate {
if (!GroovyTokenTypes.mML_COMMENT.equals(type)) {
return false;
}
final PsiElement sibling = TreeUtil.getNextLeaf(comment);
final PsiElement sibling = PsiTreeUtil.nextLeaf(comment);
if(sibling == null)
{
return true;
@@ -1,67 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.intentions.utils;
import org.jetbrains.annotations.Nullable;
import com.intellij.psi.PsiElement;
public class TreeUtil {
private TreeUtil() {
super();
}
@Nullable
public static PsiElement getNextLeaf(PsiElement element) {
if (element == null) {
return null;
}
final PsiElement sibling = element.getNextSibling();
if (sibling == null) {
final PsiElement parent = element.getParent();
return getNextLeaf(parent);
}
return getFirstLeaf(sibling);
}
private static PsiElement getFirstLeaf(PsiElement element) {
final PsiElement[] children = element.getChildren();
if (children.length == 0) {
return element;
}
return getFirstLeaf(children[0]);
}
@Nullable
public static PsiElement getPrevLeaf(PsiElement element) {
if (element == null) {
return null;
}
final PsiElement sibling = element.getPrevSibling();
if (sibling == null) {
final PsiElement parent = element.getParent();
return getPrevLeaf(parent);
}
return getLastLeaf(sibling);
}
private static PsiElement getLastLeaf(PsiElement element) {
final PsiElement[] children = element.getChildren();
if (children.length == 0) {
return element;
}
return getLastLeaf(children[children.length - 1]);
}
}