在执行搜索后在我的插件中,每个匹配项都作为searchmatch的对象发送到acceptsearchmatch(searchmatch)函数。我想获取匹配发生的行号。不能使用getoffset因为它提供了相对于源缓冲区的信息。行号?帮助
谢谢
最佳答案
诀窍是:SearchMatch
为您提供SearchRange
,这意味着该范围中可能包含几行。
解决方案是解析与SearchMatch返回的对象关联的Document,以便计算这些行号。
相关方法为getLineOfOffset(int offset)
如果对象是IMember
,则具有here an example
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);
...