Fixed NPE's in handling of FileStoreEditorInput

Signed-off-by: Vasili Gulevich <vasili.gulevich@xored.com>
Change-Id: Ifb07e902e025d7bcb1a7c29b012768eb7fb70faf
diff --git a/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/internal/core/CommitWorkingCopyOperation.java b/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/internal/core/CommitWorkingCopyOperation.java
index 3521de5..2ac196d 100644
--- a/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/internal/core/CommitWorkingCopyOperation.java
+++ b/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/internal/core/CommitWorkingCopyOperation.java
@@ -20,6 +20,7 @@
 import org.eclipse.dltk.core.IModelElement;
 import org.eclipse.dltk.core.IModelStatus;
 import org.eclipse.dltk.core.IModelStatusConstants;
+import org.eclipse.dltk.core.IOpenable;
 import org.eclipse.dltk.core.IProjectFragment;
 import org.eclipse.dltk.core.ISourceModule;
 import org.eclipse.dltk.core.ModelException;
@@ -71,15 +72,19 @@
 	protected void executeOperation() throws ModelException {
 		try {
 			beginTask(Messages.workingCopy_commit, 2);
-			SourceModule workingCopy = getSourceModule();
 
-			if (ExternalScriptProject.EXTERNAL_PROJECT_NAME.equals(workingCopy
-					.getScriptProject().getElementName())) {
+			IOpenable openable = (IOpenable) getElementToProcess();
+			if (ExternalScriptProject.EXTERNAL_PROJECT_NAME
+					.equals(openable
+							.getScriptProject().getElementName())) {
 				// case of a working copy without a resource
-				workingCopy.getBuffer().save(this.progressMonitor, this.force);
+				openable.getBuffer().save(this.progressMonitor,
+						this.force);
 				return;
 			}
 
+			SourceModule workingCopy = getSourceModule();
+
 			ISourceModule primary = workingCopy.getPrimary();
 			boolean isPrimary = workingCopy.isPrimary();
 
@@ -91,10 +96,12 @@
 			if (isPrimary
 					|| (root instanceof ProjectFragment
 							&& ((ProjectFragment) root).validateOnBuildpath()
-									.isOK() && isIncluded
+									.isOK()
+							&& isIncluded
 							&& resource.isAccessible() && Util
-							.isValidSourceModule(workingCopy, workingCopy
-									.getResource()))) {
+									.isValidSourceModule(workingCopy,
+											workingCopy
+													.getResource()))) {
 
 				// force opening so that the delta builder can get the old info
 				if (!isPrimary && !primary.isOpen()) {
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/EditorUtility.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/EditorUtility.java
index 09e10ea..a7fa12d 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/EditorUtility.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/EditorUtility.java
@@ -228,6 +228,10 @@
 
 	private static IEditorInput getEditorInput(IModelElement element) {
 		while (element != null) {
+			IEditorInput adapter = element.getAdapter(IEditorInput.class);
+			if (adapter != null) {
+				return adapter;
+			}
 			if (element instanceof IExternalSourceModule) {
 				ISourceModule unit = ((ISourceModule) element).getPrimary();
 				if (unit instanceof IStorage) {
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/SourceModuleDocumentProvider.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/SourceModuleDocumentProvider.java
index 2999380..bf3537c 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/SourceModuleDocumentProvider.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/internal/ui/editor/SourceModuleDocumentProvider.java
@@ -1389,25 +1389,32 @@
 		try {
 
 			IDocument document = info.fTextFileBuffer.getDocument();
+			boolean isSynchronized = true;
+
 			IResource resource = info.fCopy.getResource();
+			if (resource != null) {
 
-			Assert.isTrue(resource instanceof IFile);
+				Assert.isTrue(resource instanceof IFile);
 
-			boolean isSynchronized = resource
-					.isSynchronized(IResource.DEPTH_ZERO);
-			/*
-			 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=98327 Make sure
-			 * file gets save in commit() if the underlying file has been
-			 * deleted
-			 */
-			if (!isSynchronized && isDeleted(element))
-				info.fTextFileBuffer.setDirty(true);
+				isSynchronized = resource
+						.isSynchronized(IResource.DEPTH_ZERO);
+				/*
+				 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=98327 Make sure
+				 * file gets save in commit() if the underlying file has been
+				 * deleted
+				 */
+				if (!isSynchronized && isDeleted(element))
+					info.fTextFileBuffer.setDirty(true);
 
-			if (!resource.exists()) {
-				// underlying resource has been deleted, just recreate file,
-				// ignore the rest
-				createFileFromDocument(monitor, (IFile) resource, document);
-				return;
+				if (!resource.exists()) {
+					// underlying resource has been deleted, just recreate
+					// file,
+					// ignore the rest
+					createFileFromDocument(monitor, (IFile) resource,
+							document);
+					return;
+				}
+
 			}
 
 			if (fSavePolicy != null)
@@ -1487,12 +1494,14 @@
 				if (unit != null
 						&& info.fModel instanceof AbstractMarkerAnnotationModel) {
 					IResource r = unit.getResource();
-					IMarker[] markers = r.findMarkers(IMarker.MARKER, true,
-							IResource.DEPTH_ZERO);
-					if (markers != null && markers.length > 0) {
-						AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel) info.fModel;
-						for (int i = 0; i < markers.length; i++)
-							model.updateMarker(document, markers[i], null);
+					if (r != null) {
+						IMarker[] markers = r.findMarkers(IMarker.MARKER, true,
+								IResource.DEPTH_ZERO);
+						if (markers != null && markers.length > 0) {
+							AbstractMarkerAnnotationModel model = (AbstractMarkerAnnotationModel) info.fModel;
+							for (int i = 0; i < markers.length; i++)
+								model.updateMarker(document, markers[i], null);
+						}
 					}
 				}
 			}
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/DLTKUIPlugin.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/DLTKUIPlugin.java
index 2038abf..d9c236d 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/DLTKUIPlugin.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/DLTKUIPlugin.java
@@ -746,6 +746,8 @@
 			FileStoreEditorInput input) {
 		final ISourceModule[] modules = new ISourceModule[1];
 		final IPath filePath = URIUtil.toPath(input.getURI());
+		if (filePath == null)
+			return null;
 		IScriptModel scriptModel = DLTKCore
 				.create(ResourcesPlugin.getWorkspace().getRoot());
 		try {