简体   繁体   English

Eclipse文本编辑器获取光标位置

[英]Eclipse Text editor get cursor location

I am developing a plugin that recommends code using eclipse plugin development environment (PDE). 我正在开发一个使用Eclipse插件开发环境(PDE)推荐代码的插件。 For now I'm working on designing the interface. 现在,我正在设计界面。 The thing is I want to get the cursor location in the eclipse editor and open a JFrame at that position. 我要在Eclipse编辑器中获取光标位置,然后在该位置打开一个JFrame。 I've tried to get the location with the help of documentation and forums and only able to get the offset till now or you can say line and column offset. 我试图在文档和论坛的帮助下获取位置,但到目前为止只能获取偏移量,或者您可以说行和列偏移量。 I want to get it in a point(x,y) that represents a location. 我想在代表位置的点(x,y)中获取它。 So any ideas how to get the cursor position? 那么有什么想法如何获得光标位置?

Assuming you have the StyledText control for the editor use getCaretOffset to get the caret offset: 假设您拥有用于编辑器的StyledText控件,请使用getCaretOffset获取插入符号偏移量:

StyledText text = ... get editor styled text

int caret = text.getCaretOffset();

Then call getLocationAtOffset to get the x, y coordinates of the offset relative to the control: 然后调用getLocationAtOffset以获取相对于控件的偏移量的x,y坐标:

Point point = text.getLocationAtOffset(caret);

If necessary you can convert this to be relative to the display: 如有必要,可以将其转换为相对于显示的相对值:

point = text.toDisplay(point);

Note that Eclipse plugins normally use SWT, not Swing. 请注意,Eclipse插件通常使用SWT,而不是Swing。 It will be a lot more difficult to open a JFrame than a SWT control. 打开JFrame比打开SWT控件要困难得多。

You can get the StyledText for an ITextEditor using: 您可以使用以下方法获取ITextEditorStyledText

StyledText text = (StyledText)editor.getAdapter(Control.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM