Get line/column by offset
Answered
I'm using a standalone parser generated by Grammar-Kit, so there's no Editor/Document.
Is there still any easy way to get line/column by offset (as reported by PsiElement.getTextRange())?
Please sign in to leave a comment.
import com.intellij.openapi.util.text.StringUtil;
int line = StringUtil.countNewLines(text.subSequence()) + 1; // bad: copies text
int column = offset - StringUtil.lastIndexOf(text, '\n', 0, offset); // note: substract 1 if there's '\r'
------------
Total: $4.99
> Total: $4.99
.. it used to be 2p some time ago. Still it is good value, if you ask me.
;)
I was hoping there already exists some more efficient util class for this, but thanks anyway =)