[cleanup] Delete the elementsToDocuments map in the
IntentDocumentProvider
diff --git a/plugins/org.eclipse.mylyn.docs.intent.client.ui/src/org/eclipse/mylyn/docs/intent/client/ui/editor/IntentDocumentProvider.java b/plugins/org.eclipse.mylyn.docs.intent.client.ui/src/org/eclipse/mylyn/docs/intent/client/ui/editor/IntentDocumentProvider.java
index af196c3..0e82ab1 100644
--- a/plugins/org.eclipse.mylyn.docs.intent.client.ui/src/org/eclipse/mylyn/docs/intent/client/ui/editor/IntentDocumentProvider.java
+++ b/plugins/org.eclipse.mylyn.docs.intent.client.ui/src/org/eclipse/mylyn/docs/intent/client/ui/editor/IntentDocumentProvider.java
@@ -12,12 +12,8 @@
 
 import com.google.common.collect.Sets;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 import org.eclipse.core.runtime.Assert;
@@ -27,7 +23,9 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.emf.common.util.TreeIterator;
+import org.eclipse.emf.common.util.URI;
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.eclipse.jface.operation.IRunnableContext;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IDocumentPartitioner;
@@ -65,6 +63,7 @@
 import org.eclipse.mylyn.docs.intent.core.compiler.CompilationStatusManager;
 import org.eclipse.mylyn.docs.intent.core.compiler.CompilationStatusSeverity;
 import org.eclipse.mylyn.docs.intent.core.compiler.CompilerPackage;
+import org.eclipse.mylyn.docs.intent.core.document.IntentDocument;
 import org.eclipse.mylyn.docs.intent.core.document.IntentGenericElement;
 import org.eclipse.mylyn.docs.intent.core.document.IntentStructuredElement;
 import org.eclipse.mylyn.docs.intent.core.document.UnitInstruction;
@@ -99,11 +98,6 @@
 	private RepositoryObjectHandler listenedElementsHandler;
 
 	/**
-	 * Keep the associations between an element and all the documents that are opened on it.
-	 */
-	private Map<Object, List<IntentEditorDocument>> elementsToDocuments;
-
-	/**
 	 * Root for the handled document.
 	 */
 	private EObject documentRoot;
@@ -148,7 +142,6 @@
 	 *            the editor associated to this document Provider
 	 */
 	public IntentDocumentProvider(IntentEditor editor) {
-		this.elementsToDocuments = new HashMap<Object, List<IntentEditorDocument>>();
 		this.associatedEditor = editor;
 		this.annotationModelManager = new IntentAnnotationModelManager();
 	}
@@ -176,21 +169,16 @@
 		// Step 1: create annotations for all compilation statuses
 		for (CompilationStatus status : IntentHelper.getAllStatus((IntentGenericElement)documentRoot)) {
 
-			List<IntentEditorDocument> list = elementsToDocuments.get(listenedElementsHandler
-					.getRepositoryAdapter().getIDFromElement(status.getTarget()));
-			if (list != null) {
-				IntentEditorDocument doc = list.get(0);
-				// We use the annotationModelManager to create annotations
-				ParsedElementPosition posit = doc.getIntentPosition(status.getTarget());
-				if (posit == null) {
-					posit = new ParsedElementPosition(0, 0);
-				}
+			// We use the annotationModelManager to create annotations
+			ParsedElementPosition posit = createdDocument.getIntentPosition(status.getTarget());
+			if (posit == null) {
+				posit = new ParsedElementPosition(0, 0);
+			}
 
-				if (!status.getSeverity().equals(CompilationStatusSeverity.INFO)) {
-					annotationModelManager.addAnnotationFromStatus(
-							this.listenedElementsHandler.getRepositoryAdapter(), status,
-							new Position(posit.getOffset(), posit.getDeclarationLength()));
-				}
+			if (!status.getSeverity().equals(CompilationStatusSeverity.INFO)) {
+				annotationModelManager.addAnnotationFromStatus(
+						this.listenedElementsHandler.getRepositoryAdapter(), status,
+						new Position(posit.getOffset(), posit.getDeclarationLength()));
 			}
 		}
 
@@ -262,7 +250,6 @@
 			partitioner.connect(createdDocument);
 			createdDocument.setDocumentPartitioner(partitioner);
 			subscribeRepository(((IntentEditorInput)element).getRepositoryAdapter());
-			addAllContentAsIntentElement(documentRoot, createdDocument);
 		}
 		return createdDocument;
 	}
@@ -326,31 +313,6 @@
 	}
 
 	/**
-	 * Add all the given object and all its contained elements in the elementsToDocuments mapping.
-	 * 
-	 * @param root
-	 *            the element to inspect
-	 * @param document
-	 *            the document to consider for the mapping.
-	 */
-	public void addAllContentAsIntentElement(EObject root, IntentEditorDocument document) {
-		Object identifier = listenedElementsHandler.getRepositoryAdapter().getIDFromElement(root);
-
-		// We first associate this root in to the given document
-		if (elementsToDocuments.get(identifier) == null) {
-			elementsToDocuments.put(identifier, new ArrayList<IntentEditorDocument>());
-		}
-		if (!elementsToDocuments.get(identifier).contains(document)) {
-			elementsToDocuments.get(identifier).add(document);
-		}
-
-		// Then we do the same for all its contained element
-		for (EObject content : root.eContents()) {
-			addAllContentAsIntentElement(content, document);
-		}
-	}
-
-	/**
 	 * Refreshes the outline View.
 	 * 
 	 * @param newAST
@@ -410,9 +372,6 @@
 				IntentUiLogger.logError(e);
 			}
 
-			// We update the mapping between elements and documents
-			addAllContentAsIntentElement(documentRoot, document);
-
 		} catch (NullPointerException npe) { // FIXME catch NPE ??
 			mustUndo = true;
 			IntentUiLogger.logError(npe);
@@ -486,15 +445,10 @@
 
 		// For each object modified indicated by this notification
 		for (EObject modifiedObject : notification.getImpactedElements()) {
-			Object modifiedObjectIdentifier = listenedElementsHandler.getRepositoryAdapter()
-					.getIDFromElement(modifiedObject);
 			// For all documents that have been opened on this object
-			if (elementsToDocuments.get(modifiedObjectIdentifier) != null) {
-				handleContentHasChanged(modifiedObject, modifiedObjectIdentifier);
-			} else if (modifiedObject.equals(new IntentDocumentQuery(listenedElementsHandler
-					.getRepositoryAdapter()).getOrCreateIntentDocument())) {
-				handleContentHasChanged(this.documentRoot, listenedElementsHandler.getRepositoryAdapter()
-						.getIDFromElement(this.documentRoot));
+			if (modifiedObject.equals(new IntentDocumentQuery(listenedElementsHandler.getRepositoryAdapter())
+					.getOrCreateIntentDocument())) {
+				handleContentHasChanged(modifiedObject);
 			} else {
 				// update annotations (if the compilation status manager has changed)
 				handleCompilationStatusHasChanged(modifiedObject);
@@ -701,13 +655,7 @@
 	 */
 	private boolean handleRootHasBeenDeleted(RepositoryChangeNotification notification) {
 		if (notification.getImpactedElements().size() < 1) {
-			Object modifiedObjectIdentifier = listenedElementsHandler.getRepositoryAdapter()
-					.getIDFromElement(documentRoot);
-			if (elementsToDocuments.get(modifiedObjectIdentifier) != null) {
-				for (IntentEditorDocument relatedDocument : elementsToDocuments.get(modifiedObjectIdentifier)) {
-					relatedDocument.unsynchronize();
-				}
-			}
+			createdDocument.unsynchronize();
 			return true;
 		}
 		return false;
@@ -721,11 +669,8 @@
 	 */
 	private void handleCompilationStatusHasChanged(EObject modifiedObject) {
 		if (modifiedObject instanceof CompilationStatusManager) {
-			if (elementsToDocuments.values().iterator().hasNext()
-					&& elementsToDocuments.values().iterator().next().iterator().hasNext()) {
-				updateAnnotationModelFromCompilationStatusAndChildren((IntentGenericElement)documentRoot,
-						elementsToDocuments.values().iterator().next().iterator().next());
-			}
+			updateAnnotationModelFromCompilationStatusAndChildren((IntentGenericElement)documentRoot,
+					createdDocument);
 		}
 	}
 
@@ -734,24 +679,32 @@
 	 * 
 	 * @param modifiedObject
 	 *            the modified object
-	 * @param modifiedObjectIdentifier
-	 *            the modified object identifier inside of the repository
 	 */
-	private void handleContentHasChanged(EObject modifiedObject, Object modifiedObjectIdentifier) {
+	private void handleContentHasChanged(EObject modifiedObject) {
 		if (modifiedObject instanceof IntentStructuredElement || modifiedObject instanceof UnitInstruction) {
-			if (listenedElementsHandler.getRepositoryAdapter().getIDFromElement(documentRoot)
-					.equals(modifiedObjectIdentifier)) {
-				documentRoot = modifiedObject;
-			}
-			for (final IntentEditorDocument relatedDocument : elementsToDocuments
-					.get(modifiedObjectIdentifier)) {
+			EObject newDocumentRoot = modifiedObject;
+			if (!(documentRoot instanceof IntentDocument)) {
 
-				relatedDocument.setAST(documentRoot);
-				relatedDocument.reloadFromAST();
-
-				// We update the mapping between elements and documents
-				addAllContentAsIntentElement(documentRoot, relatedDocument);
+				// Get the new version of the documentRoot
+				URI oldDocumentRootURI = EcoreUtil.getURI(documentRoot);
+				String oldDocumentRootFragment = null;
+				if (oldDocumentRootURI != null) {
+					oldDocumentRootFragment = oldDocumentRootURI.fragment();
+				}
+				if (oldDocumentRootFragment != null) {
+					try {
+						newDocumentRoot = modifiedObject.eResource().getEObject(oldDocumentRootFragment);
+						// CHECKSTYLE:OFF
+					} catch (Exception e) {
+						// CHECKSTYLE:ON
+						// Silent catch : the modifiedObject will be used as new root
+					}
+				}
 			}
+			documentRoot = newDocumentRoot;
+			createdDocument.setAST(newDocumentRoot);
+			createdDocument.reloadFromAST();
+
 			// In any case, we launch the syntax coloring
 			partitioner.computePartitioning(0, 1);
 
diff --git a/plugins/org.eclipse.mylyn.docs.intent.collab.ide/src/org/eclipse/mylyn/docs/intent/collab/ide/resource/RepoModelResource.java b/plugins/org.eclipse.mylyn.docs.intent.collab.ide/src/org/eclipse/mylyn/docs/intent/collab/ide/resource/RepoModelResource.java
index 7817ea1..447f9b7 100644
--- a/plugins/org.eclipse.mylyn.docs.intent.collab.ide/src/org/eclipse/mylyn/docs/intent/collab/ide/resource/RepoModelResource.java
+++ b/plugins/org.eclipse.mylyn.docs.intent.collab.ide/src/org/eclipse/mylyn/docs/intent/collab/ide/resource/RepoModelResource.java
@@ -45,4 +45,13 @@
 	protected boolean useUUIDs() {
 		return true;
 	}
+
+	/**
+	 * {@inheritDoc}
+	 * 
+	 * @see org.eclipse.emf.ecore.resource.impl.ResourceImpl#setModified(boolean)
+	 */
+	public void setModified(boolean isModified) {
+		// Do nothing: never used in case of a repository resource
+	}
 }
diff --git a/tests/org.eclipse.mylyn.docs.intent.client.ui.test/src/org/eclipse/mylyn/docs/intent/client/ui/test/unit/scenario/ExternalChangesTest.java b/tests/org.eclipse.mylyn.docs.intent.client.ui.test/src/org/eclipse/mylyn/docs/intent/client/ui/test/unit/scenario/ExternalChangesTest.java
index d5e1771..a8ebf93 100644
--- a/tests/org.eclipse.mylyn.docs.intent.client.ui.test/src/org/eclipse/mylyn/docs/intent/client/ui/test/unit/scenario/ExternalChangesTest.java
+++ b/tests/org.eclipse.mylyn.docs.intent.client.ui.test/src/org/eclipse/mylyn/docs/intent/client/ui/test/unit/scenario/ExternalChangesTest.java
@@ -32,6 +32,7 @@
 import org.eclipse.mylyn.docs.intent.core.modelingunit.ModelingUnitInstructionReference;
 import org.eclipse.mylyn.docs.intent.core.modelingunit.NativeValue;
 import org.eclipse.mylyn.docs.intent.core.modelingunit.StructuralFeatureAffectation;
+import org.eclipse.mylyn.docs.intent.parser.IntentKeyWords;
 
 /**
  * An test ensuring that external changes (i.e. changes on the document made without using the
@@ -149,7 +150,7 @@
 		assertEquals("Wrong initial state: Intent document should have only one chapter", 1, intentDocument
 				.eContents().size());
 		assertEquals("Wrong initial state: Intent document should have only one chapter", 2, document.get()
-				.split("Chapter").length);
+				.split(IntentKeyWords.INTENT_KEYWORD_CHAPTER).length);
 
 		intentDocument.getIntentContent().add(IntentDocumentFactory.eINSTANCE.createIntentSection());
 		try {
@@ -171,7 +172,8 @@
 
 		// Step 4: check that editor was updated
 		waitForAllOperationsInUIThread();
-		assertEquals("Editor should have been reloaded", 3, document.get().split("Chapter").length);
+		assertEquals("Editor should have been reloaded", 3,
+				document.get().split(IntentKeyWords.INTENT_KEYWORD_CHAPTER).length);
 	}
 
 	/**
@@ -179,18 +181,38 @@
 	 * for example git updates) are correctly handled.
 	 */
 	public void testExternalChangesOnChapterWithDocumentOpened() {
-		// Step 1 : open an editor on the root document
+		// Open an editor on the root document
 		editor = openIntentEditor();
+		doTestExternalChangesOnChapter();
+		assertTrue(document.get().startsWith("Document"));
+	}
+
+	/**
+	 * Ensures that external changes (i.e. changes on the document made without using the RepositoryAdapter,
+	 * for example git updates) are correctly handled.
+	 */
+	public void testExternalChangesOnChapterWithChapterOpened() {
+		// Open an editor on the first chapter
+		editor = openIntentEditor(getIntentSection(1));
+		doTestExternalChangesOnChapter();
+		assertTrue(document.get().startsWith(IntentKeyWords.INTENT_KEYWORD_CHAPTER));
+	}
+
+	/**
+	 * Ensures that external changes (i.e. changes on the document made without using the RepositoryAdapter,
+	 * for example git updates) are correctly handled.
+	 */
+	private void doTestExternalChangesOnChapter() {
 		document = (IntentEditorDocument)editor.getDocumentProvider().getDocument(editor.getEditorInput());
 
-		// Step 2: modify the IntentChapter without using the repository adapter
+		// Step 1: modify the IntentChapter without using the repository adapter
 		IntentDocument intentDocument = (IntentDocument)new ResourceSetImpl().getResource(documentURI, true)
 				.getContents().iterator().next();
 		IntentSection intentSection = intentDocument.getSubSections().get(0).getSubSections().get(0);
 		assertEquals("Wrong initial state: Intent section should have only 2 modeling units", 2,
 				intentSection.getModelingUnits().size());
 		assertEquals("Wrong initial state: Intent section should have only 2 modeling units", 3, document
-				.get().split("@M").length);
+				.get().split(IntentKeyWords.MODELING_UNIT_BEGIN).length);
 
 		intentSection.getIntentContent().add(ModelingUnitFactory.eINSTANCE.createModelingUnit());
 		try {
@@ -204,7 +226,7 @@
 			fail(e.getMessage());
 		}
 
-		// Step 3: check that intent model was updated
+		// Step 2: check that intent model was updated
 		EObject intentDocumentAsLoadedByRepositoryAdapter = repositoryAdapter
 				.getResource(IntentLocations.INTENT_INDEX).getContents().iterator().next();
 		IntentSection intentSectionAsLoadedByRepositoryAdapter = ((IntentDocument)intentDocumentAsLoadedByRepositoryAdapter)
@@ -212,9 +234,10 @@
 		assertEquals("Intent document should have been reloaded due to external changes", 3,
 				intentSectionAsLoadedByRepositoryAdapter.getModelingUnits().size());
 
-		// Step 4: check that editor was updated
+		// Step 3: check that editor was updated
 		waitForAllOperationsInUIThread();
-		assertEquals("Editor should have been reloaded", 4, document.get().split("@M").length);
+		assertEquals("Editor should have been reloaded", 4,
+				document.get().split(IntentKeyWords.MODELING_UNIT_BEGIN).length);
 	}
 
 	/**