PsiElement element = ...; // get a PSI element int offset = element.getTextOffset(); // get the offset of the first character String text = element.getText(); // get the text of the element System.out.println("Element text: " + text + ", offset: " + offset);
PsiFile file = ...; // get a PSI file for (PsiElement element : file.getChildren()) { int offset = element.getTextOffset(); // get the offset of the first character String text = element.getText(); // get the text of the element System.out.println("Element text: " + text + ", offset: " + offset); }This example demonstrates how to iterate over all the children of a PSI file and get their text and offset. The code first obtains a PSI file, then iterates over all its children using the getChildren() method, and for each child element, it gets the text and offset using the getTextOffset() and getText() methods. The package library for com.intellij.psi is part of IntelliJ IDEA's proprietary API, and can only be used within IntelliJ IDEA plugins.