Use Python to create your Eclipse

Source: Internet
Author: User

Use Python to create your Eclipse

Although the Eclipse advanced script environment (IDEA) project is still under development, you must acknowledge that it is very powerful, which allows us to quickly build our own Eclipse development environment.

Based on the powerful Eclipse framework, you can use its built-in plug-in system to fully expand Eclipse. However, writing and deploying a new plug-in is still very troublesome, even if you only need an additional small feature. However, depending on the pipeline, you can easily achieve this without writing any line of Java code. Ghost is a platform that automatically implements these functions using scripting languages such as Python or Javascript.

In this article, based on my speech at the EclipseCon conference in North America this year, I will introduce how to use Python and idea to set up your Eclipse environment, it also shows you how to use the Python energy to make your IDE fly.

 

Install and run "Hello World"

The example in this article uses Python Java to implement Jython. You can install idea directly to your existing Eclipse IDE. In this example, Eclipse Mars is used, and the runtime environment itself and its modules and Jython engine are installed.

Use the Eclipse installation dialog box (Help>Install New Software...), Install idea: http://download.eclipse.org/ease/update/nightly,

Select the following components:

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

This will install the supervisor and Its modules. Pay attention to the Resource module. This module can access the Eclipse workspace, project, and file APIs.

After successful installation, install the mongojython engine https://dl.bintray.com/pontesegger/ease-jython. Then, test. Create a project and create a hello. py file. Enter:

  1. print"hello world"

Right-click the file and select "Run as-> scripts script ". In this way, the output of "Hello world" is displayed on the console.

Now you can write Python scripts to access your workspace and projects. This method can be used for various customization. The following are some ideas.

 

Improve your code Quality

Managing good code quality is a very troublesome thing, especially when you need to deal with a large code library or involve many engineers. These pains can be mitigated through scripts, such as batch formatting some files, or removing unix-style line Terminators in the files, which makes it easier to differentiate between source code control systems such as git. Another better purpose is to use scripts to generate Eclipse markers to highlight the code you can improve. Here are some sample scripts that you can use to add task markers to all the "printStackTrace" methods found in the java file. See the source code.

Copy the file to the workspace and Run it. Right-click the file and select "Run as-> prepare script ".

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

If your java file contains printStackTraces, you can see the tag automatically added in the task view and editor sidebar.

 

Automated construction of tedious tasks

When you work in multiple projects at the same time, you must complete many complicated and repetitive tasks. You may need to add copyright information to all source file headers or update files automatically when a new framework is used. For example, when switching to Tycho and Maven for the first time, we need to add the pom. xml file for each giel project. You can easily complete this task using several lines of Python code. After Tycho supports building without pom, we need to remove the non-pom file. Similarly, a few lines of code can handle this task. For example, a script can be added to README. md for each open workspace project. See the source code add_readme.py.

Copy the file to the workspace and Run it. Right-click the file and select "Run as-> prepare 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 execution result of the script is added to README. md in each open project. A line of description is automatically added to the java and Python projects.

 

Build New Functions

You can use the Python script to quickly build some urgently needed functions, or create a prototype to demonstrate the desired functions to the team and users. For example, an Eclipse does not currently support automatic saving of files you are working on. Even if this feature will be available soon, you can now have an editor that can be automatically saved every 30 seconds or at the background. The following is a snippet of the main method. See the following code: autosave. py.

  1. def save_dirty_editors():
  2. workbench = getService(org.eclipse.ui.IWorkbench)
  3. for window in workbench.getWorkbenchWindows():
  4. for page in window.getPages():
  5. for editor_ref in page.getEditorReferences():
  6. part = editor_ref.getPart(False)
  7. if part and part.isDirty():
  8. print"Auto-Saving", part.getTitle()
  9. part.doSave(None)

Before running the script, you must check 'Allow Scripts to run code in UI thread'. This setting is in Window> Preferences> Scripting. Add the Script to the workspace, right-click it, and select "Run as> scripts Script ". Each time the editor is automatically saved, the console outputs a saved message. To disable the automatic saving script, you only need to click the stop button in the Red Square on the console.

 

Quick User Interface Scaling

The best thing about idea is that you can combine your script with elements on the IDE interface (such as a new button or menu. You do not need to write java code or install a new plug-in. You only need to add several lines of code before your script.

The following is a simple script example to create three new projects.

  1. # name :Create fruit projects
  2. # toolbar :ProjectExplorer
  3. # description :Create fruit projects
  4. loadModule("/System/Resources")
  5. for name in["banana","pineapple","mango"]:
  6. createProject(name)

The above annotation will specifically tell idea to add a button to the Project Explorer toolbar. The following script adds a button to delete the three items. See source code createProjects. py and deleteProjects. py.

  1. # name :Delete fruit projects
  2. # toolbar :ProjectExplorer
  3. # description :Get rid of the fruit projects
  4. loadModule("/System/Resources")
  5. for name in["banana","pineapple","mango"]:
  6. project = getProject(name)
  7. project.delete(0,None)

To display the buttons, add these two scripts to a new project, for example, 'scriptsproobject '. Go to Windows> Preference> Scripting> Script Location, click 'add workspace', and select the ScriptProject project. This project is now the default location for storing scripts. You can find the two buttons appear on the Project Explorer, so that you can quickly add and delete projects through the two newly added buttons.

 

Integrate third-party tools

In any case, you may need tools other than the Eclipse ecosystem (this is true, although Eclipse is already rich, it does not have everything ). In these cases, you will find it very convenient to wrap them in a script for calling. Here is a simple example for you to integrate the resource manager and add it to the right-click menu bar, so that you can click the icon to open the resource manager to browse the current file. See source code explorer. py.

  1. # name :Explorefrom here
  2. # popup : enableFor(org.eclipse.core.resources.IResource)
  3. # description :Start a file browser using current selection
  4. loadModule("/System/Platform")
  5. loadModule('/System/UI')
  6. selection = getSelection()
  7. if isinstance(selection, org.eclipse.jface.viewers.IStructuredSelection):
  8. selection = selection.getFirstElement()
  9. ifnot isinstance(selection, org.eclipse.core.resources.IResource):
  10. selection = adapt(selection, org.eclipse.core.resources.IResource)
  11. if isinstance(selection, org.eclipse.core.resources.IFile):
  12. selection = selection.getParent()
  13. if isinstance(selection, org.eclipse.core.resources.IContainer):
  14. runProcess("explorer.exe",[selection.getLocation().toFile().toString()])

To display the menu, add the file to a new project, for example, 'scriptproject '. Go to Windows> Preference> Scripting> Script Locations, click "Add Workspace", and select the 'scriptproject' project. When you right-click the mouse button on the file, a new menu item appears in the pop-up menu. Click it to display the resource manager. (Note that this function has already appeared in Eclipse, but you can replace it with other third-party tools in this example .)

Eclipse's advanced basic environment (IDEA) provides a great set of extension functions, allowing Eclipse IDE to use Python for easy extension. Although this project is still in its early stages, more and more great features of this project are being developed. If you want to contribute to it, please go to the forum to discuss it.

I will release more details about the release at the Eclipsecon North America Conference on April 9. In my speech, Scripting Eclipse with Python will not only introduce Jython, but also how C-Python and how this function is extended in the scientific field.

Use Eclipse 15.04, Java 8, and WTP in Ubuntu 4.4

Install Java 8 and Eclipse 14.04 on Ubuntu 4.4 LTS

Hadoop Eclipse plug-in compilation and installation 1.2.0

Compile Hadoop plug-in Eclipse

Hadoop 1.2.1 compile Eclipse plug-in

Install JDK and Eclipse for C/C ++ in Ubuntu 13.10 (solving global menu problems)

How to install the latest Eclipse version in Ubuntu 14.04

Eclipse details: click here
Eclipse: click here

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

Author: Tracy Miranda Translator: VicYu/Vic020 proofreaders: wxy

This article was originally compiled by LCTT and launched with the honor of Linux in China

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.