以前用Eclipse的时候,有一个“Link with Editor”,即下面红色框住的图标。作用是展开当前打开的文件所在的目录树
后来转到idea以后,也有这么一个类似的图标可以点击,效果一模一样,功能名改为了“Scroll From Source”,即下面红色框住的图标。
不过自从换了Mac以后,鼠标已经被我丢弃了,所以快捷键对我来说是越多越好。但是不幸的是,这个功能居然不能添加快捷键,这搞得我有时候得用触摸板点击,甚为不爽。而且也有几个用idea的朋友对这点吐槽多次了,所以我用了点零碎的时间看了下开发插件的过程,然后花了半个下午写了一个可以为”Scroll From Source“这个功能添加快捷键的插件。
插件已经提交到jetbrains的官方插件库中,地址是:https://plugins.jetbrains.com/plugin/7606?pr=idea 同时我把插件的源代码也公开到了github,地址是:https://github.com/luonanqin/intellij-idea-plugins/tree/master/ScrollFromSource 因为代码很简单,所以我就顺便贴出来简单说明下
package luonq; import com.intellij.ide.projectView.ProjectView; import com.intellij.ide.projectView.impl.ProjectViewImpl; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * Created by Luonanqin on 11/13/14. */ public class ScrollFromSource extends AnAction { private static final Logger LOG = Logger.getInstance(ScrollFromSource.class); public void actionPerformed(AnActionEvent e) { try { Project project = e.getProject(); ProjectViewImpl projectView = (ProjectViewImpl) ProjectView.getInstance(project); Class<ProjectViewImpl> clazz = ProjectViewImpl.class; Field[] fields = clazz.getDeclaredFields(); Field myAutoScrollFromSourceHandlerField = null; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; // can't use field's name to find the variable of MyAutoScrollFromSourceHandler if ("MyAutoScrollFromSourceHandler".equals(field.getType().getSimpleName())) { myAutoScrollFromSourceHandlerField = field; } } myAutoScrollFromSourceHandlerField.setAccessible(true); Object handler = myAutoScrollFromSourceHandlerField.get(projectView); Class<?>[] clazzes = clazz.getDeclaredClasses(); for (int i = 0; i < clazzes.length; i++) { Class<?> clazze = clazzes[i]; String simpleName = clazze.getSimpleName(); if ("MyAutoScrollFromSourceHandler".equals(simpleName)) { Method fromSource = clazze.getMethod("scrollFromSource"); fromSource.setAccessible(true); fromSource.invoke(handler); return; } } } catch (Exception ex) { LOG.error("ScrollFromSource execute ERROR!", ex); } } }
效果图如下:
PS:目前只测试了idea的community和ultimate版本,其他平台没测试过。如果使用中有出现问题可以随时回复我。
Intellij IDEA插件 - Scroll From Source
原文地址:http://blog.csdn.net/luonanqin/article/details/41088171