Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit f1894f8

Browse files
fix #179: "navigate to file in project browser" does not work all the times
1 parent ddd2ff1 commit f1894f8

2 files changed

Lines changed: 50 additions & 25 deletions

File tree

src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/ProjectBrowserControl.cs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
// DEALINGS IN THE SOFTWARE.
1818

1919
using System;
20+
using System.Collections.Generic;
21+
using System.Diagnostics;
2022
using System.Drawing;
2123
using System.IO;
24+
using System.Linq;
2225
using System.Windows.Forms;
2326

2427
using ICSharpCode.Core;
@@ -249,37 +252,58 @@ public void SelectFileAndExpand(string fileName)
249252
try {
250253
inSelectFile = true;
251254
lastSelectionTarget = fileName;
252-
TreeNode node = FindFileNode(fileName);
253-
254-
if (node != null) {
255-
// Expand to node
256-
TreeNode parent = node.Parent;
257-
while (parent != null) {
258-
parent.Expand();
259-
parent = parent.Parent;
260-
}
261-
//node = FindFileNode(fileName);
262-
treeView.SelectedNode = node;
263-
} else {
264-
// Node for this file does not exist yet (the tree view is lazy loaded)
265-
SelectDeepestOpenNodeForPath(fileName);
266-
}
255+
LoadAndExpandToNode(new FileName(fileName));
267256
} finally {
268257
inSelectFile = false;
269258
}
270259
}
271260

272261
#region SelectDeepestOpenNode internals
273-
//
274-
// SolutionNode RootSolutionNode {
275-
// get {
276-
// if (treeView.Nodes != null && treeView.Nodes.Count>0) {
277-
// return treeView.Nodes[0] as SolutionNode;
278-
// }
279-
// return null;
280-
// }
281-
// }
282-
//
262+
263+
void LoadAndExpandToNode(FileName fileName)
264+
{
265+
IProject project = null;
266+
if (!SD.ProjectService.IsSolutionOrProjectFile(fileName)) {
267+
project = SD.ProjectService.FindProjectContainingFile(fileName);
268+
}
269+
Stack<ISolutionItem> itemsToExpand = new Stack<ISolutionItem>();
270+
ISolutionItem item = project;
271+
if (project == null) {
272+
item = SD.ProjectService.CurrentSolution.AllItems
273+
.OfType<ISolutionFileItem>().FirstOrDefault(i => i.FileName.Equals(fileName));
274+
}
275+
while (item != null) {
276+
itemsToExpand.Push(item);
277+
item = item.ParentFolder;
278+
}
279+
AbstractProjectBrowserTreeNode current = null;
280+
var currentChildren = treeView.Nodes;
281+
while (itemsToExpand.Any()) {
282+
var currentItem = itemsToExpand.Pop();
283+
current = currentChildren.OfType<AbstractProjectBrowserTreeNode>().FirstOrDefault(n => n.Tag == currentItem);
284+
if (current == null) break;
285+
current.Expand();
286+
currentChildren = current.Nodes;
287+
}
288+
if (project != null) {
289+
var fileItem = project.FindFile(fileName);
290+
var virtualPath = fileItem.VirtualName;
291+
if (!string.IsNullOrWhiteSpace(fileItem.DependentUpon)) {
292+
int index = virtualPath.LastIndexOf('\\') + 1;
293+
virtualPath = virtualPath.Insert(index, fileItem.DependentUpon + "\\");
294+
}
295+
string[] relativePath = virtualPath.Split('\\');
296+
for (int i = 0; i < relativePath.Length; i++) {
297+
current = currentChildren.OfType<AbstractProjectBrowserTreeNode>()
298+
.FirstOrDefault(n => n.Text.Equals(relativePath[i], StringComparison.OrdinalIgnoreCase));
299+
if (current == null) break;
300+
current.Expand();
301+
currentChildren = current.Nodes;
302+
}
303+
}
304+
treeView.SelectedNode = current;
305+
}
306+
283307
void SelectDeepestOpenNodeForPath(string fileName)
284308
{
285309
TreeNode node = FindDeepestOpenNodeForPath(fileName);

src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/TreeNodes/SolutionItemNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public SolutionItemNode(ISolutionFileItem item)
5151
this.solution = item.ParentSolution;
5252
this.item = item;
5353
this.Text = Path.GetFileName(FileName);
54+
this.Tag = item;
5455
SetIcon(IconService.GetImageForFile(FileName));
5556
}
5657

0 commit comments

Comments
 (0)