用 Python 打造你的 Eclipse
Eclipse Python
![](/data/attachment/album/201604/25/191915fryzxzybvavz4m0o.png)


Eclipse 高级脚本环境（[EASE](https://eclipse.org/ease/)）项目虽然还在开发中，但是必须要承认它非常强大，它让我们可以快速打造自己的Eclipse 开发环境。


依据 Eclipse 强大的框架，可以通过其内建的插件系统全方面的扩展 Eclipse。然而，编写和部署一个新的插件还是十分麻烦，即使你只是需要一个额外的小功能。不过，现在依托于 EASE，你可以不用写任何一行 Java 代码就可以方便的做到这点。EASE 是一种使用 Python 或者 Javascript 这样的脚本语言自动实现这些功能的平台。


本文中，根据我在今年北美的 EclipseCon 大会上的[演讲](https://www.eclipsecon.org/na2016/session/scripting-eclipse-python)，我将介绍如何用 Python 和 EASE 设置你的 Eclipse 环境，并告诉如何发挥 Python 的能量让你的 IDE 跑的飞起。


### 安装并运行 "Hello World"


本文中的例子使用 Python 的 Java 实现 Jython。你可以将 EASE 直接安装到你已有的 Eclipse IDE 中。本例中使用[Eclipse Mars](https://www.eclipse.org/downloads/packages/eclipse-ide-eclipse-committers-451/mars1)，并安装 EASE 环境本身以及它的模块和 Jython 引擎。


使用 Eclipse 安装对话框(`Help>Install New Software`...)，安装 EASE：<http://download.eclipse.org/ease/update/nightly>，


选择下列组件：


* EASE Core feature
* EASE core UI feature
* EASE Python Developer Resources
* EASE modules (Incubation)


这会安装 EASE 及其模块。这里我们要注意一下 Resource 模块，此模块可以访问 Eclipse 工作空间、项目和文件 API。


![](/data/attachment/album/201604/25/191916dtj4tmszt433tsmx.png)


成功安装后，接下来安装 EASE Jython 引擎 <https://dl.bintray.com/pontesegger/ease-jython/> 。完成后，测试下。新建一个项目并新建一个 hello.py 文件，输入：



```
print "hello world"

```

选中这个文件，右击并选择“Run as -> EASE script”。这样就可以在控制台看到“Hello world”的输出。


现在就可以编写 Python 脚本来访问工作空间和项目了。这种方法可以用于各种定制，下面只是一些思路。


### 提升你的代码质量


管理良好的代码质量本身是一件非常烦恼的事情，尤其是当需要处理一个大型代码库或者要许多工程师参与的时候。而这些痛苦可以通过脚本来减轻，比如批量格式化一些文件，或者[去掉文件中的 unix 式的行结束符](http://code.activestate.com/recipes/66434-change-line-endings/)来使得在 git 之类的源代码控制系统中比较差异更加容易。另外一个更好的用途是使用脚本来生成 Eclipse markers 以高亮你可以改善的代码。这里有一些示例脚本，你可以用来在 java 文件中所有找到的“printStackTrace”方法中加入task markers 。请看[源码](https://gist.github.com/tracymiranda/6556482e278c9afc421d)。


拷贝该文件到工作空间来运行，右击并选择“Run as -> EASE script”。



```
loadModule('/System/Resources')

from org.eclipse.core.resources import IMarker

for ifile in findFiles("*.java"):
    file_name = str(ifile.getLocation())
    print "Processing " + file_name
    with open(file_name) as f:
        for line_no, line in enumerate(f, start=1):
            if "printStackTrace" in line:
                marker = ifile.createMarker(IMarker.TASK)
                marker.setAttribute(IMarker.TRANSIENT, True)
                marker.setAttribute(IMarker.LINE_NUMBER, line_no)
                marker.setAttribute(IMarker.MESSAGE, "Fix in Sprint 2: " + line.strip())


```

如果你的 java 文件中包含了 printStackTraces，你就可以看见任务视图和编辑器侧边栏上自动新加的标记。


![](/data/attachment/album/201604/25/191916xrf3fa6nsaffforj.png)


### 自动构建繁琐任务


当同时工作在多个项目的时候，肯定需要完成许多繁杂、重复的任务。可能你需要在所有源文件头上加入版权信息，或者采用新框架时候自动更新文件。例如，当首次切换到 Tycho 和 Maven 的时候，我们需要 giel每个项目添加 pom.xml 文件。使用几行 Python 代码可以很轻松的完成这个任务。然后当 Tycho 支持无 pom 构建后，我们需要移除不要的 pom 文件。同样，几行代码就可以搞定这个任务，例如，这里有个脚本可以在每一个打开的工作空间项目上加入 README.md。请看源代码 [add\_readme.py](https://gist.github.com/tracymiranda/f20f233b40f1f79b1df2)。


拷贝该文件到工作空间来运行，右击并选择“Run as -> EASE script”。



```
loadModule('/System/Resources')  
  
for iproject in getWorkspace().getProjects():
    if not iproject.isOpen():
        continue

    ifile = iproject.getFile("README.md")

    if not ifile.exists():
        contents = "# " + iproject.getName() + "\n\n" 
        if iproject.hasNature("org.eclipse.jdt.core.javanature"):
            contents += "A Java Project\n"
        elif iproject.hasNature("org.python.pydev.pythonNature"):
            contents += "A Python Project\n"
        writeFile(ifile, contents)

```

脚本运行的结果会在每个打开的项目中加入 README.md，java 和 Python 的项目还会自动加上一行描述。


![](/data/attachment/album/201604/25/191917t5sikssm56m0jm85.png)


### 构建新功能


你可以使用 Python 脚本来快速构建一些急需的功能，或者做个原型给团队和用户演示你想要的功能。例如，一个 Eclipse 目前不支持的功能是自动保存你正在工作的文件。即使这个功能将会很快提供，但是你现在就可以马上拥有一个能每隔 30 秒或处于后台时自动保存的编辑器。以下是主要方法的片段。请看下列代码：[autosave.py](https://gist.github.com/tracymiranda/e9588d0976c46a987463)。



```
def save_dirty_editors():
    workbench = getService(org.eclipse.ui.IWorkbench)
    for window in workbench.getWorkbenchWindows():
        for page in window.getPages():
            for editor_ref in page.getEditorReferences():
                part = editor_ref.getPart(False)
                if part and part.isDirty():
                    print "Auto-Saving", part.getTitle()
                    part.doSave(None)

```

在运行脚本之前，你需要勾选 'Allow Scripts to run code in UI thread' 设定，这个设定在 Window > Preferences > Scripting 中。然后添加该脚本到工作空间，右击并选择“Run as > EASE Script”。每次编辑器自动保存时，控制台就会输出一个保存的信息。要关掉自动保存脚本，只需要点击控制台的红色方块的停止按钮即可。


![](/data/attachment/album/201604/25/191917swd14d7ycg8la18m.png)


### 快速扩展用户界面


EASE 最棒的事情是可以将你的脚本与 IDE 界面上元素（比如一个新的按钮或菜单）结合起来。不需要编写 java 代码或者安装新的插件，只需要在你的脚本前面增加几行代码。


下面是一个简单的脚本示例，用来创建三个新项目。



```
# name      : Create fruit projects
# toolbar   : Project Explorer
# description   : Create fruit projects

loadModule("/System/Resources")

for name in ["banana", "pineapple", "mango"]:
    createProject(name)

```

上述注释会专门告诉 EASE 增加了一个按钮到 Project Explorer 工具条。下面这个脚本是用来增加一个删除这三个项目的按钮的。请看源码 [createProjects.py](https://gist.github.com/tracymiranda/55995daaea9a4db584dc) 和 [deleteProjects.py](https://gist.github.com/tracymiranda/baa218fc2c1a8e898194)。



```
# name            :Delete fruit projects
# toolbar        : Project Explorer
# description    : Get rid of the fruit projects

loadModule("/System/Resources")

for name in ["banana", "pineapple", "mango"]:
    project = getProject(name)
    project.delete(0, None)

```

为了使按钮显示出来，增加这两个脚本到一个新的项目，假如叫做 'ScriptsProject'。然后到 Windows > Preference > Scripting > Script Location，点击 'Add Workspace' 按钮并选择 ScriptProject 项目。这个项目现在会成为放置脚本的默认位置。你可以发现 Project Explorer 上出现了这两个按钮，这样你就可以通过这两个新加的按钮快速增加和删除项目。


![](/data/attachment/album/201604/25/191917oycpekkflccppc55.png)


### 整合第三方工具


不管怎么说，你可能需要除了 Eclipse 生态系统以外的工具（这是真的，虽然 Eclipse 已经很丰富了，但是不是什么都有）。这些时候你会发现将他们包装在一个脚本来调用会非常方便。这里有一个简单的例子让你整合资源管理器，并将它加入到右键菜单栏，这样点击图标就可以打开资源管理器浏览当前文件。请看源码 [explorer.py](https://gist.github.com/tracymiranda/8aa3f0fc4bf44f4a5cd3)。



```
# name      : Explore from here
# popup     : enableFor(org.eclipse.core.resources.IResource)
# description   : Start a file browser using current selection
loadModule("/System/Platform")
loadModule('/System/UI')

selection = getSelection()
if isinstance(selection, org.eclipse.jface.viewers.IStructuredSelection):
    selection = selection.getFirstElement()

if not isinstance(selection, org.eclipse.core.resources.IResource):
    selection = adapt(selection, org.eclipse.core.resources.IResource)

if isinstance(selection, org.eclipse.core.resources.IFile):
    selection = selection.getParent()

if isinstance(selection, org.eclipse.core.resources.IContainer):
    runProcess("explorer.exe", [selection.getLocation().toFile().toString()])

```

为了让这个菜单显示出来，像之前一样将该文件加入一个新项目，比如说 'ScriptProject'。然后到 Windows > Preference > Scripting > Script Locations，点击“Add Workspace”并选择 'ScriptProject' 项目。当你在文件上右击鼠标键，你会看到弹出菜单出现了新的菜单项。点击它就会出现资源管理器。（注意，这个功能已经出现在 Eclipse 中了，但是你可以在这个例子中换成其它第三方工具。）


![](/data/attachment/album/201604/25/191918r7cyc7cl7zxlrtt4.png)


Eclipse 高级基本环境 （EASE）提供一套很棒的扩展功能，使得 Eclipse IDE 能使用 Python 来轻松扩展。虽然这个项目还在早期，但是[关于这个项目](https://eclipse.org/ease/)更多更棒的功能也正在加紧开发中，如果你想为它做出贡献，请到[论坛](https://dev.eclipse.org/mailman/listinfo/ease-dev)讨论。


我会在 2016 年的 [Eclipsecon North America](https://www.eclipsecon.org/na2016) 会议上发布更多 EASE 细节。我的演讲 [Scripting Eclipse with Python](https://www.eclipsecon.org/na2016/session/scripting-eclipse-python) 也会不单会介绍 Jython，也包括 C-Python 和这个功能在科学领域是如何扩展的。




---


via: <https://opensource.com/life/16/2/how-use-python-hack-your-ide>


作者：[Tracy Miranda](https://opensource.com/users/tracymiranda) 译者：[VicYu/Vic020](http://vicyu.net) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The Eclipse Advanced Scripting Environment ([EASE](https://eclipse.org/ease/)) project is a new but powerful set of plugins that enables you to quickly hack your Eclipse IDE.

Eclipse is a powerful framework that can be extended in many different ways by using its built-in plugin mechanism. However, writing and deploying a new plugin can be cumbersome if all you want is a bit of additional functionality. Now, using EASE, there's a better way to do that, without having to write a single line of Java code. EASE provides a way to easily automate workbench functionality using scripting languages such as Python or Javascript.

In this article, based on my [talk](https://www.eclipsecon.org/na2016/session/scripting-eclipse-python) at EclipseCon North America this year, I'll cover the basics of how to set up your Eclipse environment with Python and EASE and look at a few ideas to supercharge your IDE with the power of Python.

## Setup and run "Hello World"

The examples in this article are based on the Java-implementation of Python, Jython. You can install EASE directly into your existing Eclipse IDE. In this example we use [Eclipse Mars](https://www.eclipse.org/downloads/packages/eclipse-ide-eclipse-committers-451/mars1) and install EASE itself, its modules and the Jython engine.

From within the Eclipse Install Dialog (Help>Install New Software...), install EASE: [http://download.eclipse.org/ ease/update/nightly](http://download.eclipse.org/ease/update/nightly)


And, select the following components:

- EASE Core feature
- EASE core UI feature
- EASE Python Developer Resources
- EASE modules (Incubation)

This will give you EASE and its modules. The main one we are interested in is the Resource module that gives you access to the Eclipse workspace, projects, and files API.

After those have been successfully installed, next install the EASE Jython engine: [https://dl.bintray.com/ pontesegger/ease-jython/](https://dl.bintray.com/pontesegger/ease-jython/). Once the plugins are installed, test EASE out. Create a new project and add in a new file called hello.py with this content:

```
````print "hello world"`


Select the file, right click, and select 'Run as -> EASE script'. You should see "Hello World" appear in the console.

Now you can start writing Python scripts that can access the workspace and projects. This power can be used for all sorts of hacks, below are just a few ideas.

## Improve your code quality

Maintaining good code quality can be a tiresome job especially when dealing with a large codebase or when lots of developers are involved. Some of this pain can be made easier with a script, such as for batch formatting for a set of files, or even fixing certain files to [remove unix line endings](http://code.activestate.com/recipes/66434-change-line-endings/) for easy comparison in source control like git. Another nice thing to do is use a script to generate Eclipse markers to highlight code that could do with improving. Here's an example script that you could use to add task markers for all "printStackTrace" methods it detects in Java files. See the source code: [markers.py](https://gist.github.com/tracymiranda/6556482e278c9afc421d)

To run, copy the file to your workspace, then right click and select 'Run as -> EASE script'.

```
``````
loadModule('/System/Resources')
from org.eclipse.core.resources import IMarker
for ifile in findFiles("*.java"):
file_name = str(ifile.getLocation())
print "Processing " + file_name
with open(file_name) as f:
for line_no, line in enumerate(f, start=1):
if "printStackTrace" in line:
marker = ifile.createMarker(IMarker.TASK)
marker.setAttribute(IMarker.TRANSIENT, True)
marker.setAttribute(IMarker.LINE_NUMBER, line_no)
marker.setAttribute(IMarker.MESSAGE, "Fix in Sprint 2: " + line.strip())
```


If you have any java files with printStackTraces you will be able to see the newly created markers in the Tasks view and in the editor margin.

## Automate tedious tasks

When you are working with several projects you may want to automate some tedious, repetitive tasks. Perhaps you need to add in a copyright header to the beginning of each source file, or update source files when adopting a new framework. For instance, when we first switched to using Tycho and Maven, we had to add a pom.xml to each project. This is easily done using a few lines of Python. Then when Tycho provided support for pom-less builds, we wanted to remove unnecessary pom files. Again, a few lines of Python script enabled this. As an example, here is a script which adds a README.md file to every open project in your workspace, noting if they are Java or Python projects. See the source code: [add_readme.py](https://gist.github.com/tracymiranda/f20f233b40f1f79b1df2).

To run, copy the file to your workspace, then right click and select 'Run as -> EASE script'.

```
``````
loadModule('/System/Resources')
for iproject in getWorkspace().getProjects():
if not iproject.isOpen():
continue
ifile = iproject.getFile("README.md")
if not ifile.exists():
contents = "# " + iproject.getName() + "\n\n"
if iproject.hasNature("org.eclipse.jdt.core.javanature"):
contents += "A Java Project\n"
elif iproject.hasNature("org.python.pydev.pythonNature"):
contents += "A Python Project\n"
writeFile(ifile, contents)
```


The result should be that every open project will have a README.md file, with Java and Python projects having an additional descriptive line.

## Prototype new features

You can also use a Python script to hack a quick-fix for some much wanted functionality, or as a prototype to help demonstrate to your team or users how you envision a feature. For instance, one feature Eclipse IDE doesn't currently support is auto-save on the current file you are working on. Although this feature is in the works for future releases, you can have a quick and dirty version that autosaves every 30 seconds or when the editor is deactivated. Below is a snippet of the main method. See the full source: [autosave.py](https://gist.github.com/tracymiranda/e9588d0976c46a987463)

```
``````
def save_dirty_editors():
workbench = getService(org.eclipse.ui.IWorkbench)
for window in workbench.getWorkbenchWindows():
for page in window.getPages():
for editor_ref in page.getEditorReferences():
part = editor_ref.getPart(False)
if part and part.isDirty():
print "Auto-Saving", part.getTitle()
part.doSave(None)
```


Before running this script you will need to turn on the 'Allow Scripts to run code in UI thread' setting by checking the box under Window > Preferences > Scripting. Then you can add the file to your workspace, right click on it and select 'Run As>EASE Script'. A save message is printed out in the Console view every time an editor is saved. To turn off the autosave just stop the script by pressing the 'Terminate' red square button in the Console view.

## Quickly extend the user interface with custom buttons, menus, etc

One of the best things about EASE is that it allows you to take your scripts and quickly hook them into UI elements of the IDE, for example, as a new button or new menu item. No need to write Java or have a new plugin, just add a couple of lines to your script header—it's that simple.

Here's an example for a simplistic script that creates us three new projects.

```
``````
# name : Create fruit projects
# toolbar : Project Explorer
# description : Create fruit projects
loadModule("/System/Resources")
for name in ["banana", "pineapple", "mango"]:
createProject(name)
```


The comment lines specify to EASE to add a button to the Project Explorer toolbar. Here's another script that adds a button to the same toolbar to delete those three projects. See the source files: [createProjects.py](https://gist.github.com/tracymiranda/55995daaea9a4db584dc) and [deleteProjects.py](https://gist.github.com/tracymiranda/baa218fc2c1a8e898194)

```
``````
# name :Delete fruit projects
# toolbar : Project Explorer
# description : Get rid of the fruit projects
loadModule("/System/Resources")
for name in ["banana", "pineapple", "mango"]:
project = getProject(name)
project.delete(0, None)
```


To get the buttons to appear, add the two script files to a new project—let's call it 'ScriptsProject'. Then go to Windows > Preference > Scripting > Script Locations. Click on the 'Add Workspace' button and select the ScriptsProject. This project now becomes a default location for locating script files. You should see the buttons show up in the Project Explorer without needing to restart your IDE. You should be able to quickly create and delete the projects using your newly added buttons.

## Integrate with third-party tools

Every now and then you may need to use a tool outside the Eclipse ecosystem (sad but true, it has a lot but it does not do everything). For those occasions it might be quite handy to wrap calling that call to the tool in a script. Here's an example that allows you to integrate with explorer.exe, and add it to the content menu so you could instantly open a file browser using the current selection. See the source code: [explorer.py](https://gist.github.com/tracymiranda/8aa3f0fc4bf44f4a5cd3)

```
``````
# name : Explore from here
# popup : enableFor(org.eclipse.core.resources.IResource)
# description : Start a file browser using current selection
loadModule("/System/Platform")
loadModule('/System/UI')
selection = getSelection()
if isinstance(selection, org.eclipse.jface.viewers.IStructuredSelection):
selection = selection.getFirstElement()
if not isinstance(selection, org.eclipse.core.resources.IResource):
selection = adapt(selection, org.eclipse.core.resources.IResource)
if isinstance(selection, org.eclipse.core.resources.IFile):
selection = selection.getParent()
if isinstance(selection, org.eclipse.core.resources.IContainer):
runProcess("explorer.exe", [selection.getLocation().toFile().toString()])
```


To get the menu to appear, add the script to a new project—let's call it 'ScriptsProject'. Then go to Windows > Preference > Scripting > Script Locations. Click on the 'Add Workspace' button and select the ScriptsProject. You should see the new menu item show up in the context menu when you right-click on a file. Select this action to bring up a file browser. (Note this functionality already exists in Eclipse but this example is one you could adapt to other third-party tools).

The Eclipse Advanced Scripting Environment provides a great way to get more out of your Eclipse IDE by leveraging the power of Python. It is a project in its infancy so there is so much more to come. Learn more [about the project](https://eclipse.org/ease/) and get involved by signing up for the [forum](https://dev.eclipse.org/mailman/listinfo/ease-dev).

I'll be talking more about EASE at [Eclipsecon North America](https://www.eclipsecon.org/na2016) 2016. My talk * Scripting Eclipse with Python* will go into how you can use not just Jython, but C-Python and how this functionality can be extended specifically for scientific use-cases.

## 2 Comments