|
17 | 17 | // DEALINGS IN THE SOFTWARE. |
18 | 18 |
|
19 | 19 | using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.Diagnostics; |
20 | 22 | using System.Drawing; |
21 | 23 | using System.IO; |
| 24 | +using System.Linq; |
22 | 25 | using System.Windows.Forms; |
23 | 26 |
|
24 | 27 | using ICSharpCode.Core; |
@@ -249,37 +252,58 @@ public void SelectFileAndExpand(string fileName) |
249 | 252 | try { |
250 | 253 | inSelectFile = true; |
251 | 254 | 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)); |
267 | 256 | } finally { |
268 | 257 | inSelectFile = false; |
269 | 258 | } |
270 | 259 | } |
271 | 260 |
|
272 | 261 | #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 | + |
283 | 307 | void SelectDeepestOpenNodeForPath(string fileName) |
284 | 308 | { |
285 | 309 | TreeNode node = FindDeepestOpenNodeForPath(fileName); |
|
0 commit comments