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

Commit a651dca

Browse files
committed
Fix 1023500: When saving new document, tab title doesn't update
Main problem was that `IsNewDocument = false;` is called after `FilePath` is updated(which calls SetTitleEvent) which means logic for assigning untitled name was executed instead setting to `controller.DocumentTitle`, by moving code to `DocumentManager` we achieve following things: 1) Move logic specific to FileDocumentController out of SdiWorkspaceWindow.cs 2) Simplify logic in SdiWorkspaceWindow.cs(removal of `myUntitledTitle`) 3) And by moving logic of creating unique file name closer to start of document creation reduce change of bugs and code complexity that has to deal with file name change in middle of creation Also added unit test for new code in DocumentManager
1 parent f541dc7 commit a651dca

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Documents/DocumentManager.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,31 @@ public Task<Document> NewDocument (string defaultName, string mimeType, string c
178178
return NewDocument (defaultName, mimeType, ms);
179179
}
180180

181+
internal string GetUniqueFileName (FilePath fileName)
182+
{
183+
string baseName = fileName.FileNameWithoutExtension;
184+
int number = 1;
185+
bool found = true;
186+
var uniqueFileName = baseName + fileName.Extension;
187+
while (found) {
188+
found = false;
189+
foreach (var document in Documents) {
190+
string existingFileName = document.Name;
191+
if (existingFileName == uniqueFileName) {
192+
uniqueFileName = baseName + number + fileName.Extension;
193+
found = true;
194+
++number;
195+
break;
196+
}
197+
}
198+
}
199+
return uniqueFileName;
200+
}
201+
181202
public async Task<Document> NewDocument (string defaultName, string mimeType, Stream content)
182203
{
204+
defaultName = GetUniqueFileName (defaultName);
205+
183206
var fileDescriptor = new FileDescriptor (defaultName, mimeType, content, null);
184207

185208
var documentControllerService = await ServiceProvider.GetService<DocumentControllerService> ();

main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Shell/DefaultWorkbench.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,27 +1050,6 @@ internal void OnTabsReordered (DockNotebookTab widget, int oldPlacement, int new
10501050
WindowReordered?.Invoke (this, new WindowReorderedEventArgs { OldPosition = oldPlacement, NewPosition = newPlacement });
10511051
}
10521052

1053-
internal string GetUniqueTabName (FilePath fileName)
1054-
{
1055-
string baseName = fileName.FileNameWithoutExtension;
1056-
int number = 1;
1057-
bool found = true;
1058-
var myUntitledTitle = baseName + fileName.Extension;
1059-
while (found) {
1060-
found = false;
1061-
foreach (var window in viewContentCollection) {
1062-
string title = window.Title;
1063-
if (title == myUntitledTitle) {
1064-
myUntitledTitle = baseName + number + fileName.Extension;
1065-
found = true;
1066-
++number;
1067-
break;
1068-
}
1069-
}
1070-
}
1071-
return myUntitledTitle;
1072-
}
1073-
10741053
public event EventHandler<WindowReorderedEventArgs> WindowReordered;
10751054

10761055

main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Shell/SdiWorkspaceWindow.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ internal class SdiWorkspaceWindow : EventBox, IWorkbenchWindow, ICommandDelegato
4343
DockNotebookTab tab;
4444
DockNotebook tabControl;
4545

46-
string myUntitledTitle;
4746
string titleHolder = "";
4847

4948
bool showNotification;
@@ -270,13 +269,7 @@ void SetTitleEvent ()
270269
if (controller == null)
271270
return;
272271

273-
string newTitle;
274-
if (controller.IsNewDocument && controller is FileDocumentController fileController) {
275-
if (myUntitledTitle == null)
276-
myUntitledTitle = workbench.GetUniqueTabName (fileController.FilePath);
277-
newTitle = myUntitledTitle;
278-
} else
279-
newTitle = controller.DocumentTitle;
272+
var newTitle = controller.DocumentTitle;
280273

281274
if (newTitle != Title)
282275
Title = newTitle;

main/tests/Ide.Tests/MonoDevelop.Ide.Gui.Documents/DocumentManagerTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using IdeUnitTests;
3232
using MonoDevelop.Components;
3333
using MonoDevelop.Core;
34+
using MonoDevelop.Ide.Fonts;
3435
using MonoDevelop.Ide.Gui.Shell;
3536
using MonoDevelop.Ide.TypeSystem;
3637
using MonoDevelop.Projects;
@@ -39,7 +40,8 @@
3940

4041
namespace MonoDevelop.Ide.Gui.Documents
4142
{
42-
[RequireService(typeof(TypeSystemService))]
43+
[RequireService (typeof (TypeSystemService))]
44+
[RequireService (typeof (FontService))]
4345
public class DocumentManagerTests : TestBase
4446
{
4547
// BasicServiceProvider serviceProvider;
@@ -828,6 +830,15 @@ public async Task RunWhenContentAddedForSlowView()
828830
Assert.AreEqual (1, contentAddedEvents);
829831
Assert.IsNotNull (doc.GetContent<SomeContent> ());
830832
}
833+
834+
[Test]
835+
public async Task NewDocumentUniqueFileName()
836+
{
837+
const string newName = "Untitled";
838+
var doc = await documentManager.NewDocument (newName, "text/plain", "");
839+
var doc2 = await documentManager.NewDocument (newName, "text/plain", "");
840+
Assert.AreNotEqual (doc.FileName, doc2.FileName);
841+
}
831842
}
832843

833844
class TestController: DocumentController

0 commit comments

Comments
 (0)