“无法在设置文档缓冲区之前设置数据缓冲区”用于IVsTextLines

问题描述

在我的VSIX项目中,此错误显示在以下行:

(AdapterService as IVsEditorAdaptersFactoryService).SetDataBuffer(textLines,projBuffer as IProjectionBuffer);

其中textLines是使用我的软件包的CreateInstance方法创建的

Type textLinesType = typeof(IVsTextLines);
Guid riid = textLinesType.GUID;
Guid clsid = typeof(VsTextBufferClass).GUID;
IVsTextLines textLines = (_package as Package).CreateInstance(ref clsid,ref riid,textLinesType) as IVsTextLines;

实际上什么是DocumentTextBuffer属性,如何在新实例化的IVsTextLines上设置它?
我想做的是创建一个IVsTextLines并将其作为缓冲区传递给IVsCodeWindow

解决方法

我将textLines的初始化更改为

textLines = AdapterService.CreateVsTextBufferAdapter(serviceProvider as IOleServiceProvider,(contentTypeRegistry as IContentTypeRegistryService).GetContentType("csharp")) as IVsTextLines;

,然后使用IVsTextBuffer.InitializeContent方法将“文档和数据”文本缓冲区初始化为单个空格字符。之后,我能够成功使用SetBuffer方法。

我还没有完成扩展名的确定,因此我不确定文档缓冲区的内容有多相关。