views:

34

answers:

1

hi,

in my plug in after performing search, each matches are sent to acceptsearchmatch(searchmatch) function as objects of searchmatch.I want to get the line numbers where the match happened.cant use getoffset because it gives relative to source buffer.how can i get the line number?help

thanks

+1  A: 

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);
  ...
VonC
hi, there is no class as ISourceModule......not able to find in docs
Steven
@Nishan: `ISourceModule` comes with the Eclipse Dynamic Languages Toolkit (DLTK). You need to get the document associated with the element found by the `SearchMatch` another way.
VonC
@Nishan : See slide 16 of http://www.eclipsecon.org/2005/presentations/EclipseCON2005_Tutorial29.pdf for a possible way.
VonC
And potentially http://www.devdaily.com/java/jwarehouse/eclipse/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/propertiesfileeditor/PropertyKeyHyperlink.java.shtml
VonC