PsiReference problem

Hi,
Using IDEA platform, I'm creating a plugin to edit and compile my custom SQL-like language. I wrote already a lexer and a parser, which create a PsiElement tree. Until there, all work well. Now, in order to support code completion later, I have to provide PsiReference for some of my PsiElement (right?). Thus, I started by writing LabeledExpression (see below). To check if it work, I run my plugin and I move the mouse over the text corresponding to LabeledExpression while pressing Ctrl. I'm expecting to see the text underlined in blue but instead nothing hapend. Also, it seems that only the getRangeInElement() is called. Is-it normal? Does the parent of LabeledExpression must also provide a PsiReference in order to work? Any help would be greatly appreciated.



<code>
public interface LabeledExpression extends Expression {
     String getLabel();
     PsiElement getExpression();
}

public class LabeledExpressionImpl extends ExpressionImpl implements LabeledExpression, PsiReference {

    public LabeledExpressionImpl(@NotNull ASTNode node) {
        super(node);
    }

    public String getLabel() {
        return getNode().findChildByType(TokenTypes.LABEL).getText();
    }

    public PsiElement getExpression() {
        return findChildByClass(PsiElement.class);
    }

  
    @Override public PsiReference getReference() {
        return this;
    }

     public PsiElement getElement() {
        return this;
    }

     public TextRange getRangeInElement() {
        TextRange range = getNode().getTextRange();
        return range;
    }


     public PsiElement resolve() {
        PsiElement target = findChildByType(ElementTypes.FIELD_ACCESS_EXPRESSION);
        return target;
    }

     public String getCanonicalText() {
        return null;
    }

     public PsiElement handleElementRename(java.lang.String s) throws com.intellij.util.IncorrectOperationException {
        return this;
    }

     public PsiElement bindToElement(@org.jetbrains.annotations.NotNull com.intellij.psi.PsiElement psiElement) throws com.intellij.util.IncorrectOperationException {
        return this;
    }

     public boolean isReferenceTo(PsiElement element) {
        return resolve() == element;
    }

     public Object[] getVariants() {
        return new Object[0];
    }

     public boolean isSoft() {
        return false;
    }

}
</code>
3 comments
Comment actions Permalink

HI,
Not sure if I can help.
I think getRangeInElement has to return the range inside of the element, starting with 0 if it's at the beginning.

For example:

<code>
    public TextRange getRangeInElement() {
        return TextRange.from(1, getText().length());
    }
</code>

0
Comment actions Permalink

Thanks for your help. You was right, apparently the TextRange returned by getRangeInElement() must be relative to its enclosing PsiElement.

0
Comment actions Permalink

Thank you. May be it will be more correctly to calc textRange from "0", I mean:      return TextRange.from(0, getText().length()-1);

0

Please sign in to leave a comment.