/// <summary> /// Loads the data for this index item from a snippet file /// </summary> /// <param name="filePath">the path of the file</param> private bool AddOrUpdateSnippetsToIndexFromSnippetFile(string filePath) { try { SnippetFile snippetFile = new SnippetFile(filePath); foreach (Snippet currentSnippet in snippetFile.Snippets) { SnippetIndexItem existingItem = null; indexedSnippets.TryGetValue(GetDictionaryKey(filePath, currentSnippet.Title), out existingItem); if (existingItem == null) { //add the item to the collection CreateIndexItemDataFromSnippet(currentSnippet, filePath); } else { UpdateIndexItemData(existingItem, currentSnippet); } } } catch (IOException e) { logger.Log("Unable to open snippet file at path: " + filePath, "SnippetIndex", e); return false; } return true; }
/// <summary> /// Update a snippet item in the collection based upon the current filepath /// then swap the item with the new one /// </summary> /// <param name="updatedSnippet">The updated snippet.</param> /// <returns></returns> public bool UpdateSnippetFile(SnippetFile updatedSnippetFile) { // Find keys to remove List<string> keysToRemove = new List<string>(); // Keys we found and updated List<string> foundKeys = new List<string>(); // These have title changes to we need to create a new key for them List<Snippet> snippetsToAdd = new List<Snippet>(); // Update snippets that have not changed titles foreach (Snippet snippet in updatedSnippetFile.Snippets) { SnippetIndexItem item = null; string key = GetDictionaryKey(updatedSnippetFile.FileName, snippet.Title); indexedSnippets.TryGetValue(key, out item); if (item != null) { UpdateIndexItemData(item, snippet); foundKeys.Add(key); } else { snippetsToAdd.Add(snippet); } } if (snippetsToAdd.Count > 0) { // Figure out which keys are no longer valid foreach (string key in indexedSnippets.Keys) { if (key.Contains(updatedSnippetFile.FileName.ToUpperInvariant()) && !foundKeys.Contains(key)) { keysToRemove.Add(key); } } // Since this file only has one snippet we know the one to update // so we don't need to re-add it if (updatedSnippetFile.Snippets.Count == 1 && keysToRemove.Count == 1) { SnippetIndexItem item = null; indexedSnippets.TryGetValue(keysToRemove[0], out item); if (item != null) { UpdateIndexItemData(item, updatedSnippetFile.Snippets[0]); } } else { // Remove those keys foreach (string key in keysToRemove) { lock (indexedSnippets) { indexedSnippets.Remove(key); } } // Add update snippet items foreach (Snippet snippet in snippetsToAdd) { CreateIndexItemDataFromSnippet(snippet, updatedSnippetFile.FileName); } } } return SaveIndexFile(); }