工作区生成
Open Folder 方案中的生成支持需要扩展程序为工作区提供索引和文件上下文数据,以及要运行的生成操作。
下面是扩展需要的内容的概述。
生成文件上下文
- 提供程序工厂
ExportFileContextProviderAttribute
属性,其中包含supportedContextTypeGuids
所有适用的string
常量BuildContextTypes
- 实现
IWorkspaceProviderFactory<IFileContextProvider>
- 文件上下文提供程序
- 为每个支持生成操作和配置返回一个
FileContext
contextType
来自 BuildContextTypescontext
将IBuildConfigurationContextConfiguration
属性实现为生成配置(例如"Debug|x86"
,"ret"
或null
不适用)。 或者,使用 . 的 BuildConfigurationContext实例。 配置值 必须与 索引文件数据值中的配置匹配。
- 为每个支持生成操作和配置返回一个
索引生成文件数据值
- 提供程序工厂
ExportFileScannerAttribute
属性,用作IReadOnlyCollection<FileDataValue>
支持的类型- 实现
IWorkspaceProviderFactory<IFileScanner>
- 文件扫描程序打开
ScanContentAsync<T>
- 当类型参数为时
FileScannerTypeConstants.FileDataValuesType
返回数据 - 返回使用以下各项构造的每个配置的文件数据值:
type
为BuildConfigurationContext.ContextTypeGuid
context
作为生成配置(例如"Debug|x86"
,"ret"
或null
不适用)。 此值 必须与 文件上下文中的配置匹配。
- 当类型参数为时
生成文件上下文操作
- 提供程序工厂
ExportFileContextActionProvider
属性,其中包含supportedContextTypeGuids
所有适用的string
常量BuildContextTypes
- 实现
IWorkspaceProviderFactory<IFileContextActionProvider>
- 操作提供程序打开
IFileContextActionProvider.GetActionsAsync
- 返回与给定
FileContext.ContextType
值匹配的一个IFileContextAction
- 返回与给定
- 文件上下文操作
IFileContextAction
实现和IVsCommandItemCommandGroup
属性返回16537f6e-cb14-44da-b087-d1387ce3bf57
CommandId
0x1000
用于生成、0x1010
重新生成或0x1020
清理
注意
FileDataValue
由于需要编制索引,因此打开工作区和扫描文件以获取完整生成功能的时间点之间会有一些时间。 由于以前没有缓存的索引,因此首次打开文件夹时会出现延迟。
从生成报告消息
该生成可以向用户显示信息、警告和错误消息,这两种方法之一。 最简单的方法是使用 IBuildMessageService 并提供一个 BuildMessage,如下所示:
using Microsoft.VisualStudio.Workspace;
using Microsoft.VisualStudio.Workspace.Build;
private static void OutputBuildMessage(IWorkspace workspace)
{
IBuildMessageService buildMessageService = workspace.GetBuildMessageService();
if (buildMessageService != null)
{
// Example error build message. See the documentation for BuildMessage for more information.
var message = new BuildMessage()
{
Type = BuildMessage.TaskType.Error,
Code = "MY1001",
TaskText = "This is a sample error",
ProjectFile = "buildfile.bld",
File = "sourcefile.src"
LogMessage = $"This is sample text that will only go to the Build output window pane.\n"
// And any other properties to set
};
buildMessageService.ReportBuildMessages(new BuildMessage[] { message });
}
}
BuildMessage.Type
并 BuildMessage.LogMessage
控制向用户显示信息的位置行为。 除生成具有给定详细信息的错误列表条目以外的None
任何BuildMessage.TaskType
值。 LogMessage
将始终在“输出工具”窗口的“生成”窗格中输出。
或者,扩展可以直接与 “错误列表 ”或 “生成 ”窗格进行交互。 Visual Studio 2017 版本 15.7 之前的版本中存在 bug,其中 pszProjectUniqueName
忽略了参数 OutputTaskItemStringEx2 。
警告
可为参数提供任意基础实现的IProgress<IFileContextActionProgressUpdate>
调用方IFileContextAction.ExecuteAsync
。 从不直接调用 IProgress<IFileContextActionProgressUpdate>.Report(IFileContextActionProgressUpdate)
。 目前没有使用此参数的一般准则,但这些准则可能会更改。
生成相关的 API
- IBuildConfigurationContext 提供生成配置详细信息。
- IBuildMessageService 向用户显示 BuildMessages。
tasks.vs.json 和 launch.vs.json
有关创作 tasks.vs.json 或 launch.vs.json 文件的信息,请参阅 自定义生成和调试任务。
相关内容
- 语言服务器协议 - 了解如何将语言服务器集成到 Visual Studio 中。