private void AddToItems(string fileNameSource, ProjectItemElement element, bool isUnsaved = true) { var relativePath = fileNameSource.RemoveStart(Path.GetDirectoryName(this.FileName) + @"\"); var item = new VSProjectItem(this, element); var existingItem = items.SingleOrDefault(i => i.FilePath == item.FilePath); if (existingItem != null) { if (existingItem.IsHidden) { items.Remove(existingItem); } else { DebugUtils.Break(); } } items.Add(item); if (item.ProjectPath == null) { DebugUtils.Break(); } else { if (item.ProjectPath != null && !item.HasDependentUpon) { VSProjectItem parentItem = null; var parts = item.ProjectPath.Split('\\', StringSplitOptions.RemoveEmptyEntries); string path = null; foreach (var part in parts) { if (path == null) { path = part; } else { path += @"\" + part; } if (part == item.Name) { if (parentItem != null) { if (item.ParentItem != (IVSProjectItem)parentItem) { if (parentItem.ChildItems.Any(i => i.FilePath == item.FilePath)) { existingItem = (VSProjectItem)parentItem.ChildItems.Single(i => i.FilePath == item.FilePath); if (existingItem.IsHidden) { parentItem.Remove(existingItem); } } item.ParentItem = parentItem; parentItem.AddChild(item); } } } else { if (items.Any(i => i.RelativePath == path)) { parentItem = items.Single(i => i.RelativePath == path); if (parentItem.IsHidden) { parentItem.IsHidden = false; } } else { var newItem = new VSProjectItem(this, path, parentItem); items.Add(newItem); newItem.IsUnsaved = isUnsaved; parentItem = newItem; } } } } else { var dependencyItem = item; var dependentUponItem = items.Single(i => i.RelativePath == dependencyItem.RelativePath.RemoveEnd(dependencyItem.Name) + dependencyItem.DependentUponMetadata.Value); dependencyItem.IsUnsaved = isUnsaved; dependencyItem.DependentUpon = dependentUponItem; dependentUponItem.AddChild(dependencyItem); if (dependencyItem.ParentItem != null && dependencyItem.ParentItem.ChildItems.Any(i => i.FilePath == dependencyItem.FilePath)) { ((VSProjectItem)dependencyItem.ParentItem).Remove(dependencyItem); } dependencyItem.ParentItem = dependentUponItem; } } }