问题解答: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();
}

原创粉丝点击