Skip to content

Commit

Permalink
Merge pull request #33871 from sharwell/fix-close-file
Browse files Browse the repository at this point in the history
Split CloseFile into CloseCodeFile and CloseDesignerFile
  • Loading branch information
sharwell committed Mar 5, 2019
2 parents 4de954e + 2aae65a commit 52e26cd
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
Expand Up @@ -55,7 +55,7 @@ public void GoToDefinitionOpensProvisionalTabIfDocumentNotAlreadyOpen()
{
}
");
VisualStudio.SolutionExplorer.CloseFile(project, "FileDef.cs", saveFile: true);
VisualStudio.SolutionExplorer.CloseCodeFile(project, "FileDef.cs", saveFile: true);
VisualStudio.SolutionExplorer.AddFile(project, "FileConsumer.cs");
VisualStudio.SolutionExplorer.OpenFile(project, "FileConsumer.cs");
VisualStudio.Editor.SetText(
Expand Down
Expand Up @@ -52,7 +52,7 @@ public void GoToImplementationOpensProvisionalTabIfDocumentNotOpen()
{
}
");
VisualStudio.SolutionExplorer.CloseFile(project, "FileImplementation.cs", saveFile: true);
VisualStudio.SolutionExplorer.CloseCodeFile(project, "FileImplementation.cs", saveFile: true);
VisualStudio.SolutionExplorer.AddFile(project, "FileInterface.cs");
VisualStudio.SolutionExplorer.OpenFile(project, "FileInterface.cs");
VisualStudio.Editor.SetText(
Expand Down
Expand Up @@ -39,7 +39,7 @@ public void ChangeControlProperty()
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Text", propertyValue: "NewButtonText");
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.cs", saveFile: true);
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.cs", saveFile: true);
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"this.SomeButton.Text = ""NewButtonText""", actualText);
Expand All @@ -55,7 +55,7 @@ public void ChangeControlPropertyInCode()
var expectedPropertyValue = "ButtonTextGoesHere";
var actualPropertyValue = VisualStudio.Editor.GetWinFormButtonPropertyValue(buttonName: "SomeButton", propertyName: "Text");
Assert.Equal(expectedPropertyValue, actualPropertyValue);
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.cs", saveFile: true);
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.cs", saveFile: true);
// Change the control's text in designer.cs code
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
// Verify that the control's property was set correctly. The following text should appear in InitializeComponent().
Expand All @@ -64,7 +64,7 @@ public void ChangeControlPropertyInCode()
// Replace text property with something else
VisualStudio.Editor.SelectTextInCurrentDocument(@"this.SomeButton.Text = ""ButtonTextGoesHere"";");
VisualStudio.Editor.SendKeys(@"this.SomeButton.Text = ""GibberishText"";");
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.Designer.cs", saveFile: true);
VisualStudio.SolutionExplorer.CloseCodeFile(project, "Form1.Designer.cs", saveFile: true);
// Verify that the control text has changed in the designer
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
expectedPropertyValue = "GibberishText";
Expand Down
Expand Up @@ -107,7 +107,7 @@ End Sub
VisualStudio.SolutionExplorer.AddFile(new ProjName(ProjectName), "TestZ.vb", open: true); // Cause focus lost
VisualStudio.SolutionExplorer.OpenFile(new ProjName(ProjectName), "TestZ.vb"); // Work around https://github.com/dotnet/roslyn/issues/18488
VisualStudio.Editor.SendKeys(" ");
VisualStudio.SolutionExplorer.CloseFile(new ProjName(ProjectName), "TestZ.vb", saveFile: false);
VisualStudio.SolutionExplorer.CloseCodeFile(new ProjName(ProjectName), "TestZ.vb", saveFile: false);
VisualStudio.Editor.Verify.TextContains(@"
Sub M()
End Sub
Expand All @@ -130,7 +130,7 @@ End Sub
VisualStudio.SolutionExplorer.AddFile(new ProjName(ProjectName), "TestZ.vb", open: true); // Cause focus lost
VisualStudio.SolutionExplorer.OpenFile(new ProjName(ProjectName), "TestZ.vb"); // Work around https://github.com/dotnet/roslyn/issues/18488
VisualStudio.Editor.SendKeys(" ");
VisualStudio.SolutionExplorer.CloseFile(new ProjName(ProjectName), "TestZ.vb", saveFile: false);
VisualStudio.SolutionExplorer.CloseCodeFile(new ProjName(ProjectName), "TestZ.vb", saveFile: false);
VisualStudio.Editor.Verify.TextContains(@"
Sub M()
End Sub
Expand Down
Expand Up @@ -55,7 +55,7 @@ public void AddControl()
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.vb");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.vb", saveFile: true);
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.vb", saveFile: true);
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.vb");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"Me.SomeButton.Name = ""SomeButton""", actualText);
Expand All @@ -69,7 +69,7 @@ public void ChangeControlProperty()
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.vb");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Text", propertyValue: "NewButtonText");
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.vb", saveFile: true);
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.vb", saveFile: true);
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.vb");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"Me.SomeButton.Text = ""NewButtonText""", actualText);
Expand All @@ -85,7 +85,7 @@ public void ChangeControlPropertyInCode()
var expectedPropertyValue = "ButtonTextGoesHere";
var actualPropertyValue = VisualStudio.Editor.GetWinFormButtonPropertyValue(buttonName: "SomeButton", propertyName: "Text");
Assert.Equal(expectedPropertyValue, actualPropertyValue);
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.vb", saveFile: true);
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.vb", saveFile: true);
// Change the control's text in designer.vb code
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.vb");
// Verify that the control's property was set correctly. The following text should appear in InitializeComponent().
Expand All @@ -94,7 +94,7 @@ public void ChangeControlPropertyInCode()
// Replace text property with something else
VisualStudio.Editor.SelectTextInCurrentDocument(@"Me.SomeButton.Text = ""ButtonTextGoesHere""");
VisualStudio.Editor.SendKeys(@"Me.SomeButton.Text = ""GibberishText""");
VisualStudio.SolutionExplorer.CloseFile(project, "Form1.Designer.vb", saveFile: true);
VisualStudio.SolutionExplorer.CloseCodeFile(project, "Form1.Designer.vb", saveFile: true);
// Verify that the control text has changed in the designer
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.vb");
expectedPropertyValue = "GibberishText";
Expand Down
Expand Up @@ -584,7 +584,7 @@ void SetText(string text)

OpenFile(projectName, fileName);
SetText(contents ?? string.Empty);
CloseFile(projectName, fileName, saveFile: true);
CloseCodeFile(projectName, fileName, saveFile: true);
if (open)
{
OpenFile(projectName, fileName);
Expand Down Expand Up @@ -888,25 +888,36 @@ public void OpenFile(string projectName, string relativeFilePath)
ErrorHandler.ThrowOnFailure(textManager.NavigateToLineAndColumn(textLines, VSConstants.LOGVIEWID.Code_guid, line, column, line, column));
}

public void CloseFile(string projectName, string relativeFilePath, bool saveFile)
public void CloseDesignerFile(string projectName, string relativeFilePath, bool saveFile)
{
var document = GetOpenDocument(projectName, relativeFilePath);
if (saveFile)
{
SaveFileWithExtraValidation(document);
document.Close(EnvDTE.vsSaveChanges.vsSaveChangesYes);
}
else
CloseFile(projectName, relativeFilePath, VSConstants.LOGVIEWID.Designer_guid, saveFile);
}

public void CloseCodeFile(string projectName, string relativeFilePath, bool saveFile)
{
CloseFile(projectName, relativeFilePath, VSConstants.LOGVIEWID.Code_guid, saveFile);
}

private void CloseFile(string projectName, string relativeFilePath, Guid logicalView, bool saveFile)
{
InvokeOnUIThread(() =>
{
document.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
}
var filePath = GetAbsolutePathForProjectRelativeFilePath(projectName, relativeFilePath);
if (!VsShellUtilities.IsDocumentOpen(ServiceProvider.GlobalProvider, filePath, logicalView, out _, out _, out var windowFrame))
{
throw new InvalidOperationException($"File '{filePath}' is not open in logical view '{logicalView}'");
}
var frameClose = saveFile ? __FRAMECLOSE.FRAMECLOSE_SaveIfDirty : __FRAMECLOSE.FRAMECLOSE_NoSave;
ErrorHandler.ThrowOnFailure(windowFrame.CloseFrame((uint)frameClose));
});
}

private EnvDTE.Document GetOpenDocument(string projectName, string relativeFilePath)
{
var filePath = GetAbsolutePathForProjectRelativeFilePath(projectName, relativeFilePath);
var documents = GetDTE().Documents.Cast<EnvDTE.Document>();
var document = documents.FirstOrDefault(d => d.FullName == filePath);
var document = documents.SingleOrDefault(d => d.FullName == filePath);

if (document == null)
{
Expand Down
Expand Up @@ -128,8 +128,11 @@ public void RenameFileViaDTE(ProjectUtils.Project project, string oldFileName, s
_instance.Workspace.WaitForAsyncOperations(FeatureAttribute.Workspace);
}

public void CloseFile(ProjectUtils.Project project, string fileName, bool saveFile)
=> _inProc.CloseFile(project.Name, fileName, saveFile);
public void CloseDesignerFile(ProjectUtils.Project project, string fileName, bool saveFile)
=> _inProc.CloseDesignerFile(project.Name, fileName, saveFile);

public void CloseCodeFile(ProjectUtils.Project project, string fileName, bool saveFile)
=> _inProc.CloseCodeFile(project.Name, fileName, saveFile);

public void SaveFile(ProjectUtils.Project project, string fileName)
=> _inProc.SaveFile(project.Name, fileName);
Expand Down

0 comments on commit 52e26cd

Please sign in to comment.