I have code that correctly finds the line number of an IMethod in Eclipse under Windows:
IMethod method= ...;
String source= type.getCompilationUnit().getSource();
int lineNumber= 1;
for (int i= 0; i < method.getSourceRange().getOffset(); i++)
if (source.charAt(i) == Character.LINE_SEPARATOR)
lineNumber++;
However, this doesn't work on the Mac, presume because the line separator character is different even though the source code it is operating on is the same.
- is there a built-in way to get the line number without having to traverse every character of the source? (seems like there should be but I couldn't find it)
- if not, is there a platform independent way to count line breaks in a string?
Thanks,
Kent Beck