我使用的解决方案是获取 Windows 路径,然后将其与IVsEditorAdaptersFactoryService
and结合使用VsShellUtilities
。
var openWindowPath = Path.Combine(window.Document.Path, window.Document.Name);
var buffer = GetBufferAt(openWindowPath);
和
internal ITextBuffer GetBufferAt(string filePath)
{
var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var editorAdapterFactoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>();
var serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(MetaSharpPackage.OleServiceProvider);
IVsUIHierarchy uiHierarchy;
uint itemID;
IVsWindowFrame windowFrame;
if (VsShellUtilities.IsDocumentOpen(
serviceProvider,
filePath,
Guid.Empty,
out uiHierarchy,
out itemID,
out windowFrame))
{
IVsTextView view = VsShellUtilities.GetTextView(windowFrame);
IVsTextLines lines;
if (view.GetBuffer(out lines) == 0)
{
var buffer = lines as IVsTextBuffer;
if (buffer != null)
return editorAdapterFactoryService.GetDataBuffer(buffer);
}
}
return null;
}