提交 23a5343e 编写于 作者: S Sam Harwell

Use IVsSolution to get the correct IVsProject for an IVsHierarchy

Fixes crashes associated with InvisibleEditor
上级 6f5b2d47
......@@ -42,7 +42,7 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVisua
_needsSave = needsSave;
var invisibleEditorManager = (IIntPtrReturningVsInvisibleEditorManager)serviceProvider.GetService(typeof(SVsInvisibleEditorManager));
var vsProject = projectOpt?.Hierarchy as IVsProject;
var vsProject = TryGetProjectOfHierarchy(projectOpt?.Hierarchy);
var invisibleEditorPtr = IntPtr.Zero;
Marshal.ThrowExceptionForHR(invisibleEditorManager.RegisterInvisibleEditor(filePath, vsProject, 0, null, out invisibleEditorPtr));
......@@ -84,6 +84,34 @@ public InvisibleEditor(IServiceProvider serviceProvider, string filePath, IVisua
}
}
private IVsProject TryGetProjectOfHierarchy(IVsHierarchy hierarchyOpt)
{
// The invisible editor manager will fail in cases where the IVsProject passed to it is not consistent with
// the IVsProject known to IVsSolution (e.g. if the object is a wrapper like AbstractHostObject created by
// the CPS-based project system). This method returns an IVsProject instance known to the solution, or null
// if the project could not be determined.
if (hierarchyOpt == null)
{
return null;
}
if (!ErrorHandler.Succeeded(hierarchyOpt.GetGuidProperty(
(uint)VSConstants.VSITEMID.Root,
(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
out var projectId)))
{
return null;
}
var solution = (IVsSolution)_serviceProvider.GetService(typeof(SVsSolution));
if (!ErrorHandler.Succeeded(solution.GetProjectOfGuid(projectId, out var projectHierarchy)))
{
return null;
}
return projectHierarchy as IVsProject;
}
public IVsTextLines VsTextLines
{
get
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册