PsiTreeUtil.getParentOfType () bug, or ?


I thought that

final Class[] targetClasses = {PsiStatement.class,
PsiComment.class};
return PsiTreeUtil.getParentOfType(i_startingElement,
targetClasses, true);

was equivalent to

for (PsiElement element = i_startingElement;;)
{
if ( null == element ) return null ;
if ( element instanceof PsiStatement ) return element ;
if ( element instanceof PsiComment ) return element ;

element = element.getParent();
}

, but they are not.



They behave differently on this piece of code (the caret is in a
comment line) :


"public class Test_RegexpForLoopDetection{" + CR +
" public void qq()" + CR +
" {" + CR +
"String v1 = \"un\"," + CR +
" v2 = \"deux\";" + CR +
"v1 = null ;" + CR +
"// ]]>;" +
" }" + CR +
"}"


with
element = i_file.findElementAt(
i_editor.getCaretModel().getOffset() );

1 comment
Comment actions Permalink

Here is the source code. You can compare it with your version.

public static PsiElement getParentOfType(PsiElement element, Class[]
classes, boolean strict) {
if (strict) {
element = element.getParent();
}

while (element != null) {
for (int i = 0; i < classes.length; i++) {
Class aClass = classes+;
if (aClass.isInstance(element)) return element;
}
element = element.getParent();
}

return element;
}

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"

"Alain Ravet" <alain.ravet.list@wanadoo.be> wrote in message
news:bm4fcb$gev$1@is.intellij.net...
>
>

I thought that

>

final Class[] targetClasses = {PsiStatement.class,
PsiComment.class};
return PsiTreeUtil.getParentOfType(i_startingElement,
targetClasses, true);

>

was equivalent to

>

for (PsiElement element = i_startingElement;;)
{
if ( null == element ) return null ;
if ( element instanceof PsiStatement ) return element ;
if ( element instanceof PsiComment ) return element ;

>

element = element.getParent();
}

>

, but they are not.

>
>
>

They behave differently on this piece of code (the caret is in a
comment line) :

>
>

"public class Test_RegexpForLoopDetection{" + CR +
" public void qq()" + CR +
" {" + CR +
"String v1 = \"un\"," + CR +
" v2 = \"deux\";" + CR +
"v1 = null ;" + CR +
"// <caret>;" +
" }" + CR +
"}"

>
>

with
element = i_file.findElementAt(
i_editor.getCaretModel().getOffset() );

>


0

Please sign in to leave a comment.