Index: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/text/editor/AutoSaveDartEditorManager.java |
diff --git a/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/text/editor/AutoSaveDartEditorManager.java b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/text/editor/AutoSaveDartEditorManager.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..027e4ea0fbedf5fc92343d21b7da50a51b4df3bd |
--- /dev/null |
+++ b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/text/editor/AutoSaveDartEditorManager.java |
@@ -0,0 +1,72 @@ |
+/* |
+ * Copyright (c) 2012, the Dart project authors. |
+ * |
+ * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except |
+ * in compliance with the License. You may obtain a copy of the License at |
+ * |
+ * http://www.eclipse.org/legal/epl-v10.html |
+ * |
+ * Unless required by applicable law or agreed to in writing, software distributed under the License |
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
+ * or implied. See the License for the specific language governing permissions and limitations under |
+ * the License. |
+ */ |
+package com.google.dart.tools.ui.internal.text.editor; |
+ |
+import com.google.dart.compiler.ast.DartUnit; |
+import com.google.dart.tools.core.buffer.Buffer; |
+import com.google.dart.tools.core.model.CompilationUnit; |
+import com.google.dart.tools.core.model.DartModelException; |
+import com.google.dart.tools.ui.DartToolsPlugin; |
+import com.google.dart.tools.ui.IWorkingCopyManager; |
+import com.google.dart.tools.ui.internal.text.dart.IDartReconcilingListener; |
+ |
+import org.eclipse.core.runtime.Assert; |
+import org.eclipse.core.runtime.IProgressMonitor; |
+import org.eclipse.core.runtime.NullProgressMonitor; |
+ |
+/** |
+ * Automatically saves the Dart Editor while typing. |
+ */ |
+class AutoSaveDartEditorManager implements IDartReconcilingListener { |
+ |
+ private static long AUTO_SAVE_MS = 10000; // 10 seconds |
+ |
+ private final DartEditor editor; |
+ |
+ private long lastSavedTime; |
+ |
+ public AutoSaveDartEditorManager(DartEditor editor) { |
+ Assert.isNotNull(editor); |
+ this.editor = editor; |
+ lastSavedTime = System.currentTimeMillis(); |
+ } |
+ |
+ @Override |
+ public void aboutToBeReconciled() { |
+ } |
+ |
+ @Override |
+ public void reconciled(DartUnit ast, boolean forced, IProgressMonitor progressMonitor) { |
+ try { |
+ autosave(); |
+ } catch (DartModelException e) { |
+ e.printStackTrace(); |
Brian Wilkerson
2012/10/22 13:57:17
This should either be replaced by a log message or
danrubel
2012/10/22 15:03:15
+1 for logging
|
+ } |
+ } |
+ |
+ private void autosave() throws DartModelException { |
+ IWorkingCopyManager manager = DartToolsPlugin.getDefault().getWorkingCopyManager(); |
+ CompilationUnit workingCopy = manager.getWorkingCopy(editor.getEditorInput()); |
+ long currentTime = System.currentTimeMillis(); |
+ long timeSinceLastSave = currentTime - lastSavedTime; |
+ if (timeSinceLastSave > AUTO_SAVE_MS) { |
+ lastSavedTime = currentTime; |
+ // Save |
+ Buffer buf = workingCopy.getBuffer(); |
+ if (buf != null) { |
+ buf.save(new NullProgressMonitor(), false); |
+ } |
+ } |
+ } |
+} |