Skip to content

Instantly share code, notes, and snippets.

@tracymiranda
Last active July 19, 2020 19:11
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tracymiranda/e9588d0976c46a987463 to your computer and use it in GitHub Desktop.
This script runs with Eclipse EASE Jython engine.It autosaves the current editor every 30 seconds or when the editor is deactivated.
# 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.
# To run you can right click on the file 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 for example, by pressing the
# 'Terminate' red square button in the Eclipse Console view.
import time
loadModule("/System/Platform")
loadModule("/System/UI")
AUTOSAVE_INTERVAL_S = 30
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)
class AutoSavePartListener(org.eclipse.ui.IPartListener):
def partDeactivated(self, part):
if isinstance(part, org.eclipse.ui.IEditorPart):
if part.isDirty():
print "Auto-Saving", part.getTitle()
part.doSave(None)
def partActivated(self, part):
pass
def partBroughtToTop(self, part):
pass
def partClosed(self, part):
pass
def partOpened(self, part):
pass
class AutoSaveWindowListener(org.eclipse.ui.IWindowListener):
def windowDeactivated(self, window):
save_dirty_editors()
def windowActivated(self, part):
pass
def windowClosed(self, part):
pass
def windowOpened(self, part):
pass
window_listener = AutoSaveWindowListener()
part_listener = AutoSavePartListener()
def install_listener():
workbench = getService(org.eclipse.ui.IWorkbench)
workbench.addWindowListener(window_listener)
for window in workbench.getWorkbenchWindows():
part_service = window.getPartService()
part_service.addPartListener(part_listener)
print "Installed Window and Part Listener for Auto-Save"
def remove_listener():
workbench = getService(org.eclipse.ui.IWorkbench)
workbench.removeWindowListener(window_listener)
for window in workbench.getWorkbenchWindows():
part_service = window.getPartService()
part_service.removePartListener(part_listener)
print "Removed Window and Part Listener for Auto-Save"
executeUI("install_listener()")
try:
while True:
executeUI("save_dirty_editors()")
time.sleep(AUTOSAVE_INTERVAL_S)
except:
executeUI("remove_listener()")
@jbuchberger
Copy link

Nice demo!
Did you know, that Eclipse has a Workspace save interval in minutes for ages in the Preferencs/General/Workspace?
Cheers

@jonahgraham
Copy link

Workspace save is for saving workspace metadata, it doesn't cover saving editors. This script can save editors. The auto save editor feature is being funded by Friends of Eclipse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment