From f1894f80f519f9788b77a9e0bcebff4d1f1662d1 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 13 Jul 2014 10:58:47 +0200 Subject: [PATCH] fix #179: "navigate to file in project browser" does not work all the times --- .../ProjectBrowser/ProjectBrowserControl.cs | 74 ++++++++++++------- .../TreeNodes/SolutionItemNode.cs | 1 + 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/ProjectBrowserControl.cs b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/ProjectBrowserControl.cs index 012b101e55..4cd0081d21 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/ProjectBrowserControl.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/ProjectBrowserControl.cs @@ -17,8 +17,11 @@ // DEALINGS IN THE SOFTWARE. using System; +using System.Collections.Generic; +using System.Diagnostics; using System.Drawing; using System.IO; +using System.Linq; using System.Windows.Forms; using ICSharpCode.Core; @@ -249,37 +252,58 @@ namespace ICSharpCode.SharpDevelop.Project try { inSelectFile = true; lastSelectionTarget = fileName; - TreeNode node = FindFileNode(fileName); - - if (node != null) { - // Expand to node - TreeNode parent = node.Parent; - while (parent != null) { - parent.Expand(); - parent = parent.Parent; - } - //node = FindFileNode(fileName); - treeView.SelectedNode = node; - } else { - // Node for this file does not exist yet (the tree view is lazy loaded) - SelectDeepestOpenNodeForPath(fileName); - } + LoadAndExpandToNode(new FileName(fileName)); } finally { inSelectFile = false; } } #region SelectDeepestOpenNode internals -// -// SolutionNode RootSolutionNode { -// get { -// if (treeView.Nodes != null && treeView.Nodes.Count>0) { -// return treeView.Nodes[0] as SolutionNode; -// } -// return null; -// } -// } -// + + void LoadAndExpandToNode(FileName fileName) + { + IProject project = null; + if (!SD.ProjectService.IsSolutionOrProjectFile(fileName)) { + project = SD.ProjectService.FindProjectContainingFile(fileName); + } + Stack itemsToExpand = new Stack(); + ISolutionItem item = project; + if (project == null) { + item = SD.ProjectService.CurrentSolution.AllItems + .OfType().FirstOrDefault(i => i.FileName.Equals(fileName)); + } + while (item != null) { + itemsToExpand.Push(item); + item = item.ParentFolder; + } + AbstractProjectBrowserTreeNode current = null; + var currentChildren = treeView.Nodes; + while (itemsToExpand.Any()) { + var currentItem = itemsToExpand.Pop(); + current = currentChildren.OfType().FirstOrDefault(n => n.Tag == currentItem); + if (current == null) break; + current.Expand(); + currentChildren = current.Nodes; + } + if (project != null) { + var fileItem = project.FindFile(fileName); + var virtualPath = fileItem.VirtualName; + if (!string.IsNullOrWhiteSpace(fileItem.DependentUpon)) { + int index = virtualPath.LastIndexOf('\\') + 1; + virtualPath = virtualPath.Insert(index, fileItem.DependentUpon + "\\"); + } + string[] relativePath = virtualPath.Split('\\'); + for (int i = 0; i < relativePath.Length; i++) { + current = currentChildren.OfType() + .FirstOrDefault(n => n.Text.Equals(relativePath[i], StringComparison.OrdinalIgnoreCase)); + if (current == null) break; + current.Expand(); + currentChildren = current.Nodes; + } + } + treeView.SelectedNode = current; + } + void SelectDeepestOpenNodeForPath(string fileName) { TreeNode node = FindDeepestOpenNodeForPath(fileName); diff --git a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/TreeNodes/SolutionItemNode.cs b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/TreeNodes/SolutionItemNode.cs index 56e8359c6a..c4ec929e73 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/TreeNodes/SolutionItemNode.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/TreeNodes/SolutionItemNode.cs @@ -51,6 +51,7 @@ namespace ICSharpCode.SharpDevelop.Project this.solution = item.ParentSolution; this.item = item; this.Text = Path.GetFileName(FileName); + this.Tag = item; SetIcon(IconService.GetImageForFile(FileName)); }