The trick is: a SearchMatch
give you a SearchRange
, meaning several lines can potentially be included in that Range.
The solution is to parse the Document associated to the object returned by the SearchMatch in order to compute those line numbers.
The relevant method is getLineOfOffset(int offset)
You have here an example, in the case where the object is a IMember
ISourceRange range = member.getSourceRange();
if (range == null){
return null;
}
IBuffer buf = null;
ISourceModule compilationUnit = member.getSourceModule();
if (!compilationUnit.isConsistent()) {
return null;
}
buf = compilationUnit.getBuffer();
final int start = range.getOffset();
String contents = buf.getContents();
Document doc = new Document(contents);
try {
int line = doc.getLineOfOffset(start);
...