diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockWorkbench.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockWorkbench.cs index 24ddf57dbf..558d3a4b7a 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockWorkbench.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/MockWorkbench.cs @@ -94,6 +94,11 @@ namespace PythonBinding.Tests.Utils throw new NotImplementedException(); } + public void ShowView(IViewContent content, bool switchToOpenedView) + { + throw new NotImplementedException(); + } + public void ShowPad(PadDescriptor content) { throw new NotImplementedException(); diff --git a/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs b/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs index 3a81e38e88..9912eb44c1 100644 --- a/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs +++ b/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs @@ -192,7 +192,15 @@ namespace ICSharpCode.FormsDesigner if (formClass.Name != this.formClass.Name) { LoggingService.Info("Renaming form to " + formClass.Name); - ICSharpCode.SharpDevelop.Refactoring.FindReferencesAndRenameHelper.RenameClass(this.formClass, formClass.Name); + Dictionary providedFileDocuments = new Dictionary(); + providedFileDocuments.Add(this.ViewContent.DesignerCodeFile.FileName, this.ViewContent.DesignerCodeFileDocument); + if (!this.ViewContent.PrimaryFile.Equals(this.ViewContent.DesignerCodeFile)) { + System.Diagnostics.Debug.Assert(!this.ViewContent.DesignerCodeFileDocument.Equals(this.ViewContent.PrimaryFileDocument)); + providedFileDocuments.Add(this.ViewContent.PrimaryFileName, this.ViewContent.PrimaryFileDocument); + } + ICSharpCode.SharpDevelop.Refactoring.FindReferencesAndRenameHelper.RenameClass(this.formClass, formClass.Name, providedFileDocuments); + this.ViewContent.DesignerCodeFile.MakeDirty(); + this.ViewContent.PrimaryFile.MakeDirty(); Reparse(); } diff --git a/src/Main/Base/Project/Src/Gui/IWorkbench.cs b/src/Main/Base/Project/Src/Gui/IWorkbench.cs index 00e5ac8a61..e69ff9b5de 100644 --- a/src/Main/Base/Project/Src/Gui/IWorkbench.cs +++ b/src/Main/Base/Project/Src/Gui/IWorkbench.cs @@ -117,10 +117,15 @@ namespace ICSharpCode.SharpDevelop.Gui void Initialize(); /// - /// Inserts a new object in the workspace. + /// Inserts a new object in the workspace and switches to the new view. /// void ShowView(IViewContent content); + /// + /// Inserts a new object in the workspace. + /// + void ShowView(IViewContent content, bool switchToOpenedView); + /// /// Inserts a new object in the workspace. /// diff --git a/src/Main/Base/Project/Src/Gui/IWorkbenchLayout.cs b/src/Main/Base/Project/Src/Gui/IWorkbenchLayout.cs index 40e3b8334a..f44f28a502 100644 --- a/src/Main/Base/Project/Src/Gui/IWorkbenchLayout.cs +++ b/src/Main/Base/Project/Src/Gui/IWorkbenchLayout.cs @@ -75,9 +75,9 @@ namespace ICSharpCode.SharpDevelop.Gui void RedrawAllComponents(); /// - /// Shows a new . + /// Shows a new and optionally switches to it. /// - IWorkbenchWindow ShowView(IViewContent content); + IWorkbenchWindow ShowView(IViewContent content, bool switchToOpenedView); void LoadConfiguration(); diff --git a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/DefaultFileNodeCommands.cs b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/DefaultFileNodeCommands.cs index 2fd629fc78..5b49b59a91 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/DefaultFileNodeCommands.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/DefaultFileNodeCommands.cs @@ -77,7 +77,7 @@ namespace ICSharpCode.SharpDevelop.Project.Commands int defaultCodonIndex = codons.IndexOf(DisplayBindingService.GetDefaultCodonPerFileName(fileName)); using (OpenWithDialog dlg = new OpenWithDialog(codons, defaultCodonIndex, Path.GetExtension(fileName))) { if (dlg.ShowDialog(WorkbenchSingleton.MainForm) == DialogResult.OK) { - FileUtility.ObservedLoad(new FileService.LoadFileWrapper(dlg.SelectedBinding.Binding).Invoke, fileName); + FileUtility.ObservedLoad(new FileService.LoadFileWrapper(dlg.SelectedBinding.Binding, true).Invoke, fileName); } } } diff --git a/src/Main/Base/Project/Src/Gui/Workbench/DefaultWorkbench.cs b/src/Main/Base/Project/Src/Gui/Workbench/DefaultWorkbench.cs index 9ef5c6164a..87df8534d7 100644 --- a/src/Main/Base/Project/Src/Gui/Workbench/DefaultWorkbench.cs +++ b/src/Main/Base/Project/Src/Gui/Workbench/DefaultWorkbench.cs @@ -323,6 +323,11 @@ namespace ICSharpCode.SharpDevelop.Gui } public void ShowView(IViewContent content) + { + this.ShowView(content, true); + } + + public void ShowView(IViewContent content, bool switchToOpenedView) { System.Diagnostics.Debug.Assert(layout != null); primaryViewContentCollection.Add(content); @@ -337,8 +342,10 @@ namespace ICSharpCode.SharpDevelop.Gui } } - layout.ShowView(content); - content.WorkbenchWindow.SelectWindow(); + layout.ShowView(content, switchToOpenedView); + if (switchToOpenedView) { + content.WorkbenchWindow.SelectWindow(); + } OnViewOpened(new ViewContentEventArgs(content)); } diff --git a/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceLayout.cs b/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceLayout.cs index 221ff6e9ff..d29ba5c30b 100644 --- a/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceLayout.cs +++ b/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceLayout.cs @@ -9,6 +9,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Windows.Forms; using ICSharpCode.Core; @@ -181,7 +182,7 @@ namespace ICSharpCode.SharpDevelop.Gui void ShowViewContents() { foreach (IViewContent content in WorkbenchSingleton.Workbench.PrimaryViewContents) { - ShowView(content); + ShowView(content, true); } } @@ -548,12 +549,16 @@ namespace ICSharpCode.SharpDevelop.Gui ActiveMdiChanged(this, null); } - public IWorkbenchWindow ShowView(IViewContent content) + public IWorkbenchWindow ShowView(IViewContent content, bool switchToOpenedView) { if (content.WorkbenchWindow is SdiWorkspaceWindow) { SdiWorkspaceWindow oldSdiWindow = (SdiWorkspaceWindow)content.WorkbenchWindow; if (!oldSdiWindow.IsDisposed) { - oldSdiWindow.Show(dockPanel); + if (switchToOpenedView) { + oldSdiWindow.Show(dockPanel); + } else { + this.AddWindowToDockPanelWithoutSwitching(oldSdiWindow); + } return oldSdiWindow; } } @@ -563,12 +568,29 @@ namespace ICSharpCode.SharpDevelop.Gui sdiWorkspaceWindow.ViewContents.AddRange(content.SecondaryViewContents); sdiWorkspaceWindow.CloseEvent += new EventHandler(CloseWindowEvent); if (dockPanel != null) { - sdiWorkspaceWindow.Show(dockPanel); + if (switchToOpenedView) { + sdiWorkspaceWindow.Show(dockPanel); + } else { + this.AddWindowToDockPanelWithoutSwitching(sdiWorkspaceWindow); + } } return sdiWorkspaceWindow; } + void AddWindowToDockPanelWithoutSwitching(SdiWorkspaceWindow sdiWorkspaceWindow) + { + sdiWorkspaceWindow.DockPanel = dockPanel; + SdiWorkspaceWindow otherWindow = dockPanel.ActiveContent as SdiWorkspaceWindow; + if (otherWindow == null) { + otherWindow = dockPanel.Contents.OfType().FirstOrDefault(c => c.Pane != null); + } + if (otherWindow != null) { + sdiWorkspaceWindow.Pane = otherWindow.Pane; + } + sdiWorkspaceWindow.DockState = DockState.Document; + } + void ActiveMdiChanged(object sender, EventArgs e) { OnActiveWorkbenchWindowChanged(e); diff --git a/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceWindow.cs b/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceWindow.cs index e5bf5204ab..aa9ed6b2f7 100644 --- a/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceWindow.cs +++ b/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SdiWorkspaceWindow.cs @@ -285,6 +285,13 @@ namespace ICSharpCode.SharpDevelop.Gui void UpdateTitle() { IViewContent content = ActiveViewContent; + if (content == null && this.ViewContents.Count > 0) { + // This can happen when the window is inactive and + // no tab page of the viewTabControl is selected + // (viewTabControl.SelectedIndex == -1) + // but we have multiple ViewContents. + content = this.ViewContents[0]; + } if (content != null) { base.ToolTipText = content.PrimaryFileName; diff --git a/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SimpleWorkbenchLayout.cs b/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SimpleWorkbenchLayout.cs index ceecac992b..fbbc3674c8 100644 --- a/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SimpleWorkbenchLayout.cs +++ b/src/Main/Base/Project/Src/Gui/Workbench/Layouts/SimpleWorkbenchLayout.cs @@ -140,11 +140,14 @@ namespace ICSharpCode.SharpDevelop.Gui { } - public IWorkbenchWindow ShowView(IViewContent content) + public IWorkbenchWindow ShowView(IViewContent content, bool switchToOpenedView) { SimpleDocumentTab sdt = new SimpleDocumentTab(); sdt.window.ViewContents.Add(content); documentTabs.TabPages.Add(sdt); + if (switchToOpenedView) { + documentTabs.SelectedTab = sdt; + } return sdt.window; } diff --git a/src/Main/Base/Project/Src/Services/File/FileService.cs b/src/Main/Base/Project/Src/Services/File/FileService.cs index 70e01783b8..fee01f3df7 100644 --- a/src/Main/Base/Project/Src/Services/File/FileService.cs +++ b/src/Main/Base/Project/Src/Services/File/FileService.cs @@ -173,11 +173,13 @@ namespace ICSharpCode.SharpDevelop internal sealed class LoadFileWrapper { - IDisplayBinding binding; + readonly IDisplayBinding binding; + readonly bool switchToOpenedView; - public LoadFileWrapper(IDisplayBinding binding) + public LoadFileWrapper(IDisplayBinding binding, bool switchToOpenedView) { this.binding = binding; + this.switchToOpenedView = switchToOpenedView; } public void Invoke(string fileName) @@ -186,7 +188,7 @@ namespace ICSharpCode.SharpDevelop IViewContent newContent = binding.CreateContentForFile(file); if (newContent != null) { DisplayBindingService.AttachSubWindows(newContent, false); - WorkbenchSingleton.Workbench.ShowView(newContent); + WorkbenchSingleton.Workbench.ShowView(newContent, switchToOpenedView); } file.CloseIfAllViewsClosed(); } @@ -207,21 +209,41 @@ namespace ICSharpCode.SharpDevelop return GetOpenFile(fileName) != null; } + /// + /// Opens a view content for the specified file and switches to the opened view + /// or switches to and returns the existing view content for the file if it is already open. + /// + /// The name of the file to open. + /// The existing or opened for the specified file. public static IViewContent OpenFile(string fileName) + { + return OpenFile(fileName, true); + } + + /// + /// Opens a view content for the specified file + /// or returns the existing view content for the file if it is already open. + /// + /// The name of the file to open. + /// Specifies whether to switch to the view for the specified file. + /// The existing or opened for the specified file. + public static IViewContent OpenFile(string fileName, bool switchToOpenedView) { fileName = FileUtility.NormalizePath(fileName); LoggingService.Info("Open file " + fileName); IViewContent viewContent = GetOpenFile(fileName); if (viewContent != null) { - viewContent.WorkbenchWindow.SelectWindow(); + if (switchToOpenedView) { + viewContent.WorkbenchWindow.SelectWindow(); + } return viewContent; } IDisplayBinding binding = DisplayBindingService.GetBindingPerFileName(fileName); if (binding != null) { - if (FileUtility.ObservedLoad(new NamedFileOperationDelegate(new LoadFileWrapper(binding).Invoke), fileName) == FileOperationResult.OK) { + if (FileUtility.ObservedLoad(new NamedFileOperationDelegate(new LoadFileWrapper(binding, switchToOpenedView).Invoke), fileName) == FileOperationResult.OK) { FileService.RecentOpen.AddLastFile(fileName); } } else { diff --git a/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs b/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs index 14fdd9d93e..4df66c0bae 100644 --- a/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs +++ b/src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs @@ -13,7 +13,6 @@ using System.Text; using ICSharpCode.Core; using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; using ICSharpCode.SharpDevelop.Dom; -using ICSharpCode.SharpDevelop.Dom.Refactoring; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Project; using ICSharpCode.TextEditor; @@ -132,6 +131,11 @@ namespace ICSharpCode.SharpDevelop.Refactoring } public static void RenameClass(IClass c, string newName) + { + RenameClass(c, newName, null); + } + + public static void RenameClass(IClass c, string newName, IDictionary providedFileDocuments) { c = c.GetCompoundClass(); // get compound class if class is partial @@ -150,7 +154,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring } } - FindReferencesAndRenameHelper.RenameReferences(list, newName); + FindReferencesAndRenameHelper.RenameReferences(list, newName, providedFileDocuments); } static IList GetClassParts(IClass c) @@ -367,28 +371,42 @@ namespace ICSharpCode.SharpDevelop.Refactoring public static void RenameReferences(List list, string newName) { - List modifiedContents = new List(); + RenameReferences(list, newName, null); + } + + public static void RenameReferences(List list, string newName, IDictionary providedFileDocuments) + { + Dictionary modifiedDocuments = new Dictionary(); List modifications = new List(); foreach (Reference r in list) { - IViewContent viewContent = FileService.OpenFile(r.FileName); - ITextEditorControlProvider p = viewContent as ITextEditorControlProvider; + ICSharpCode.TextEditor.Document.IDocument document; + IViewContent viewContent; - if (!modifiedContents.Contains(viewContent)) { - modifiedContents.Add(viewContent); - if (p != null) - p.TextEditorControl.Document.UndoStack.StartUndoGroup(); + if (providedFileDocuments == null || !providedFileDocuments.TryGetValue(FileUtility.NormalizePath(r.FileName), out document)) { + viewContent = FileService.OpenFile(r.FileName, false); + ITextEditorControlProvider p = viewContent as ITextEditorControlProvider; + document = (p == null) ? null : p.TextEditorControl.Document; + } else { + viewContent = null; } - if (p != null) { - ModifyDocument(modifications, p.TextEditorControl.Document, r.Offset, r.Length, newName); + if (document == null) { + LoggingService.Warn("RenameReferences: Could not get document for file '" + r.FileName + "'"); + continue; + } + + if (!modifiedDocuments.ContainsKey(document)) { + modifiedDocuments.Add(document, viewContent); + document.UndoStack.StartUndoGroup(); } - } - foreach (IViewContent viewContent in modifiedContents) { - ITextEditorControlProvider p = viewContent as ITextEditorControlProvider; - if (p != null) - p.TextEditorControl.Document.UndoStack.EndUndoGroup(); - ParserService.ParseViewContent(viewContent); + ModifyDocument(modifications, document, r.Offset, r.Length, newName); + } + foreach (KeyValuePair entry in modifiedDocuments) { + entry.Key.UndoStack.EndUndoGroup(); + if (entry.Value != null) { + ParserService.ParseViewContent(entry.Value); + } } }