Java 我需要IMarker的字符起始和字符结束
我正在创建IMarkers,我有行号,但需要char\u start和char\u end。创建标记时,标记所连接的文件可能无法在编辑器中打开。如何在不打开文件的情况下获取字符开始和字符结束偏移量?现在,标记突出显示了整行,但我希望它忽略前面的空白。我自己能够解决这个问题。我相信有一种更有效的方式来做这件事,但就我的目的而言,这将在目前起作用Java 我需要IMarker的字符起始和字符结束,java,eclipse-plugin,Java,Eclipse Plugin,我正在创建IMarkers,我有行号,但需要char\u start和char\u end。创建标记时,标记所连接的文件可能无法在编辑器中打开。如何在不打开文件的情况下获取字符开始和字符结束偏移量?现在,标记突出显示了整行,但我希望它忽略前面的空白。我自己能够解决这个问题。我相信有一种更有效的方式来做这件事,但就我的目的而言,这将在目前起作用 IPath path = Path.fromOSString(file); IFile iFile = ResourcesPlugin.getWorksp
IPath path = Path.fromOSString(file);
IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
ITextFileBufferManager iTextFileBufferManager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer iTextFileBuffer = null;
IDocument iDoc = null;
try {
iTextFileBufferManager.connect(iFile.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
iTextFileBuffer = iTextFileBufferManager.getTextFileBuffer(iFile.getFullPath(), LocationKind.IFILE);
iDoc = iTextFileBuffer.getDocument();
iTextFileBufferManager.disconnect(iFile.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
} catch (Exception e) {
e.printStackTrace();
}
int start = iDoc.getLineOffset(iline - 1);
int end = iDoc.getLineLength(iline - 1);
//this next section was done to remove the leading white spaces
while(iDoc.getChar(start) == ' ' || iDoc.getChar(start) == '\t'){
start++;
end--;
}
final int charStart = start;
final int charEnd = start + end;
请注意,您包括行分隔符。因此,如果您将光标放在该行的末尾并点击“回车”,则标记将在下一行“继续”。如果这不是您想要的,您应该将
iDoc.getLineDelimiter(iLine-1).length()
减到charEnd(使用适当的空检查)。不要“仅仅”删除1,因为行尾可以是\r\n