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())?

0
3 comments

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

1

> Total: $4.99

.. it used to be 2p some time ago. Still it is good value, if you ask me.

;)

0

I was hoping there already exists some more efficient util class for this, but thanks anyway =)

0

Please sign in to leave a comment.