未验证 提交 818cb28e 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #38107 from sharwell/show-code

Show code instead of designer if the designer is not installed
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System;
using System.IO;
using System.Runtime.InteropServices;
......@@ -25,10 +27,10 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation
/// <summary>
/// The base class of both the Roslyn editor factories.
/// </summary>
internal abstract partial class AbstractEditorFactory : IVsEditorFactory, IVsEditorFactoryNotify
internal abstract class AbstractEditorFactory : IVsEditorFactory, IVsEditorFactoryNotify
{
private readonly IComponentModel _componentModel;
private Microsoft.VisualStudio.OLE.Interop.IServiceProvider _oleServiceProvider;
private Microsoft.VisualStudio.OLE.Interop.IServiceProvider? _oleServiceProvider;
private bool _encoding;
protected AbstractEditorFactory(IComponentModel componentModel)
......@@ -52,7 +54,7 @@ int IVsEditorFactory.Close()
public int CreateEditorInstance(
uint grfCreateDoc,
string pszMkDocument,
string pszPhysicalView,
string? pszPhysicalView,
IVsHierarchy vsHierarchy,
uint itemid,
IntPtr punkDocDataExisting,
......@@ -72,7 +74,7 @@ int IVsEditorFactory.Close()
? "Code"
: pszPhysicalView;
IVsTextBuffer textBuffer = null;
IVsTextBuffer? textBuffer = null;
// Is this document already open? If so, let's see if it's a IVsTextBuffer we should re-use. This allows us
// to properly handle multiple windows open for the same document.
......@@ -130,6 +132,10 @@ int IVsEditorFactory.Close()
// We must create the WinForms designer here
var loaderName = GetWinFormsLoaderName(vsHierarchy);
if (loaderName is null)
{
goto case "Code";
}
var designerService = (IVSMDDesignerService)_oleServiceProvider.QueryService<SVSMDDesignerService>();
var designerLoader = (IVSMDDesignerLoader)designerService.CreateDesignerLoader(loaderName);
......@@ -173,7 +179,7 @@ int IVsEditorFactory.Close()
return VSConstants.S_OK;
}
private static string GetWinFormsLoaderName(IVsHierarchy vsHierarchy)
private string? GetWinFormsLoaderName(IVsHierarchy vsHierarchy)
{
const string LoaderName = "Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader";
const string NewLoaderName = "Microsoft.VisualStudio.Design.Core.Serialization.CodeDom.VSCodeDomDesignerLoader";
......@@ -193,6 +199,18 @@ private static string GetWinFormsLoaderName(IVsHierarchy vsHierarchy)
if (frameworkName.Identifier == ".NETCoreApp" &&
frameworkName.Version?.Major >= 3)
{
if (!(_oleServiceProvider.QueryService<SVsShell>() is IVsShell shell))
{
return null;
}
var newWinFormsDesignerPackage = new Guid("c78ca057-cc29-421f-ad6d-3b0943debdfc");
if (!ErrorHandler.Succeeded(shell.IsPackageInstalled(newWinFormsDesignerPackage, out var installed))
|| installed == 0)
{
return null;
}
return NewLoaderName;
}
}
......@@ -205,7 +223,7 @@ private static string GetWinFormsLoaderName(IVsHierarchy vsHierarchy)
return LoaderName;
}
public int MapLogicalView(ref Guid rguidLogicalView, out string pbstrPhysicalView)
public int MapLogicalView(ref Guid rguidLogicalView, out string? pbstrPhysicalView)
{
pbstrPhysicalView = null;
......@@ -267,7 +285,7 @@ private void FormatDocumentCreatedFromTemplate(IVsHierarchy hierarchy, uint item
var workspace = _componentModel.GetService<VisualStudioWorkspace>();
var solution = workspace.CurrentSolution;
ProjectId projectIdToAddTo = null;
ProjectId? projectIdToAddTo = null;
foreach (var projectId in solution.ProjectIds)
{
......@@ -292,7 +310,7 @@ private void FormatDocumentCreatedFromTemplate(IVsHierarchy hierarchy, uint item
var documentId = DocumentId.CreateNewId(projectIdToAddTo);
var forkedSolution = solution.AddDocument(DocumentInfo.Create(documentId, filePath, loader: new FileTextLoader(filePath, defaultEncoding: null), filePath: filePath));
var addedDocument = forkedSolution.GetDocument(documentId);
var addedDocument = forkedSolution.GetDocument(documentId)!;
var rootToFormat = addedDocument.GetSyntaxRootSynchronously(cancellationToken);
var documentOptions = ThreadHelper.JoinableTaskFactory.Run(() => addedDocument.GetOptionsAsync(cancellationToken));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册