Eclipse java 文件编辑器 根据字符串定位光标位置

来源:互联网 发布:小区车辆出入数据 编辑:程序博客网 时间:2024/06/08 06:46
//获得当前java编辑器的文本编辑对象

CompilationUnitEditor currenteditor = (CompilationUnitEditor) editorPart.getPages().get(1);

//获得文本编辑对象的document

IDocument document = currenteditor.getViewer().getDocument();

//根据字符串获取到字符的位置

position = document.get().indexOf("{",position)+1;

//将当前编辑器设为活动编辑器

editorPart.setActiveEditor(currenteditor);
//设置光标的位置

currenteditor.selectAndReveal(position, 0);//这个0  就是选中光标位置后面的文本长度

//编辑器获得焦点

currenteditor.setFocus();

到此,就可以定位到java 文件中某个字符串中的位置

0 0
原创粉丝点击