问题解答:Eclipse中怎样判断Editor中光标的位置,并找到光标周围代码,这样的事件在哪里写?
来源:互联网 发布:黄维德的周瑜 知乎 编辑:程序博客网 时间:2024/05/01 16:20
一、先说触发方式:
有样学样法则。
首先,查找调用org.eclipse.ui.texteditor.AbstractTextEditor.setAction(String, IAction)的地方:
搜索结果:
org.eclipse.ui.texteditor - src - org.eclipse.ui.workbench.texteditor
AbstractTextEditor
createAccessibilityActions()
createActions() (44 matches)重点看这里。见下文代码一
createNavigationActions() (8 matches)
createUndoRedoActions() (2 matches)
getAction(String)
registerUndoRedoAction(String, OperationHistoryActionHandler)
代码一(这个action,最基础的接口是IAction,也可以从TextEditorAction派生)
protected void createActions() {
ResourceAction action;
action= new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Cut.", this, ITextOperationTarget.CUT); //$NON-NLS-1$
action.setHelpContextId(IAbstractTextEditorHelpContextIds.CUT_ACTION);
action.setActionDefinitionId(IWorkbenchActionDefinitionIds.CUT);
setAction(ITextEditorActionConstants.CUT, action);
注释:
1、那么action如何触发呢?与setActionDefinitionId有关,这个ID需要通过org.eclipse.ui.commands扩展点扩展(这个扩展点中指定的id就是触发这个动作的command id)。
2、command id有了,command如何触发呢?需要菜单、快捷键、上下文菜单、按钮、等等。那么把你想要做的事情关联到这个id就 ok了。
-------------------------------------------------
二、现在说光标,要在自己的action run()的时候:
public void run()
{
ITextEditor editor= getTextEditor();
ISelection section = editor.getSelectionProvider().getSelection();
int Selectedoffset=0;
if(section instanceof ITextSelection)
Selectedoffset = ((ITextSelection)section).getOffset();
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
try {
IRegion selectdLineRegion = document.getLineInformationOfOffset(Selectedoffset);
String lineString = document.get(selectdLineRegion.getOffset(), selectdLineRegion.getLength());
重点在ITextSelection,和 getDocument
public interface ITextSelection extends ISelection {
/**
* Returns the offset of the selected text.
*
* @return the offset of the selected text
*/
int getOffset();
/**
* Returns the length of the selected text.
*
* @return the length of the selected text
*/
int getLength();
/**
* Returns number of the line containing the offset of the selected text.
* If the underlying text has been changed between the creation of this
* selection object and the call of this method, the value returned might
* differ from what it would have been at the point of creation.
*
* @return the start line of this selection or <code>-1</code> if there is no valid line information
*/
int getStartLine();//哈哈。O(∩_∩)O哈哈~
/**
* Returns the number of the line containing the last character of the selected text.
* If the underlying text has been changed between the creation of this
* selection object and the call of this method, the value returned might
* differ from what it would have been at the point of creation.
*
* @return the end line of this selection or <code>-1</code> if there is no valid line information
*/
int getEndLine();
/**
* Returns the selected text.
* If the underlying text has been changed between the creation of this
* selection object and the call of this method, the value returned might
* differ from what it would have been at the point of creation.
*
* @return the selected text or <code>null</code> if there is no valid text information
*/
String getText();
}
- 问题解答:Eclipse中怎样判断Editor中光标的位置,并找到光标周围代码,这样的事件在哪里写?
- EditText--在EditText中显示图片,控制光标的位置,以及操作光标位置的元素
- 在javascript中如何设置textbox的光标的位置
- 根据光标位置在字符中插入新的字符
- 在javascript中如何获得textbox输入光标的位置
- Delphi 设置文本框中光标的位置在最后
- 在javascript中如何获得textbox输入光标的位置
- c++ 在console中控制光标的位置
- 控制TextBox中光标的位置
- C#中控制TextBox的光标位置
- WTL设置edit中光标的位置
- 设置 EditText 中,光标出现的位置
- 确定UITextView中起始光标的位置
- JBuilder Editor中光标不能正确定位问题的解决
- JBuilder Editor中光标不能正确定位问题的解决
- JBuilder Editor中光标不能正确定位问题的解决
- JBuilder Editor中光标不能正确定位问题的解决
- 代码中设置EditView的光标
- 2009下半年软件设计师考试大纲
- JFreeChart教程
- SAP系统配置参数详解
- IT碰碰车第四期:中国式创业因李开复而不同?
- 2009.9.17-名字空间
- 问题解答:Eclipse中怎样判断Editor中光标的位置,并找到光标周围代码,这样的事件在哪里写?
- SharePoint工作流解决方案QuickFlow系列(6)—事件总线(EventBus)
- 影响网站排名的因素
- 关于云计算的一点浅见
- BlackBreey程序的调试技巧
- 三本书-关于现实事件抽象为模型
- Linux 中的计时——gettimeofday函数
- java析构函数替代者finalize()解说
- 第一篇博客