Language Server Protocol 文件重命名通知(workspace/didRenameFiles)完全指南

发布时间:2026/10/7 16:17:42
Language Server Protocol 文件重命名通知(workspace/didRenameFiles)完全指南 开发工具【免费下载链接】language-server-protocolDefines a common protocol for language servers.项目地址https://gitcode.com/gh_mirrors/la/language-server-protocol点击查看免费下载导读workspace/didRenameFiles是 LSPLanguage Server Protocol工作区文件操作系列消息中的事后通知notification当用户在客户端如编辑器内部重命名文件或文件夹后客户端立即将该事件通知给语言服务器用于触发索引更新、符号追踪、引用刷新等后续处理。本文以仓库中 3.18 版规范文档 _specifications/lsp/3.18/workspace/didRenameFiles.md 为主体结合同版本初始化协商与相关类型定义完整讲解该通知的语义、能力协商、参数结构、注册过滤器以及它与workspace/willRenameFiles请求的前后配合关系帮助语言服务器开发者正确实现文件重命名感知。通知语义文件被重命名之后才发出workspace/didRenameFiles是一条通知notification客户端 → 服务器箭头标记其触发条件是文件从客户端内部被重命名files were renamed from within the client。与文件系统层面的变化不同这类事件来自用户在编辑器 UI 中的操作例如在资源管理器里直接 F2 重命名文件或文件夹或通过某个命令触发的重命名。需要特别注意的是from within the client这一限定只有在客户端内部发起的重命名才会触发该通知用户在终端里用mv、或外部程序修改文件系统并不在此通知的覆盖范围内重命名既可以是用户直接操作也可以是客户端应用某个 workspace edit 的副作用参见同目录下 _specifications/lsp/3.18/workspace/willRenameFiles.md 中对触发来源的描述。该通知的核心用途是让服务器在重命名完成后同步内部状态例如更新符号索引中文件 URI 的映射、刷新跨文件引用、使已打开的文档与新的 URI 保持一致等。能力协商客户端与服务器各自声明与 LSP 中所有功能一样workspace/didRenameFiles需要客户端与服务器在初始化阶段通过能力capability字段双向协商双方能力都已声明后才会实际收发消息。规范原文在 _specifications/lsp/3.18/general/initialize.md 中给出了完整的 TypeScript 定义。客户端能力Client Capability项目内容属性名可选workspace.fileOperations.didRename属性类型boolean客户端能力表示客户端支持发送workspace/didRenameFiles通知。在 3.18 版初始化定义中文件操作相关客户端能力被统一收敛在FileOperationClientCapabilities接口下since 3.16.0其中与重命名相关的字段为export interface FileOperationClientCapabilities { /** * Whether the client supports dynamic registration for * file requests/notifications. */ dynamicRegistration?: boolean; /** * The client has support for sending didCreateFiles notifications. */ didCreate?: boolean; /** * The client has support for sending willCreateFiles requests. */ willCreate?: boolean; /** * The client has support for sending didRenameFiles notifications. */ didRename?: boolean; /** * The client has support for sending willRenameFiles requests. */ willRename?: boolean; /** * The client has support for sending didDeleteFiles notifications. */ didDelete?: boolean; /** * The client has support for sending willDeleteFiles requests. */ willDelete?: boolean; }即客户端在initialize请求中发送形如下面的能力片段{ capabilities: { workspace: { fileOperations: { didRename: true } } } }服务器能力Server Capability项目内容属性名可选workspace.fileOperations.didRename属性类型FileOperationRegistrationOptions服务器能力表示服务器有兴趣接收workspace/didRenameFiles通知。与客户端的boolean不同服务器侧的值是FileOperationRegistrationOptions结构用于描述服务器希望过滤哪些文件/文件夹的重命名事件详见下文过滤器一节。服务器在initialize响应中声明{ capabilities: { workspace: { fileOperations: { didRename: { filters: [ { pattern: { glob: **/*.{ts,tsx}, matches: file } } ] } } } } }通知协议定义规范原文对消息本身的定义非常精确项目内容方向客户端 → 服务器notification方法名workspace/didRenameFiles参数paramsRenameFilesParams响应无通知不产生响应这是一条纯通知客户端发出后不需要也不等待任何响应服务器应将其视为状态已改变的信号在后台完成索引等增量更新即可。参数结构RenameFilesParams 与 FileRename通知的 params 类型为RenameFilesParams。该类型在 _specifications/lsp/3.18/workspace/willRenameFiles.md 中与workspace/willRenameFiles请求共享定义since 3.16.0完整 TypeScript 定义如下/** * The parameters sent in notifications/requests for user-initiated renames * of files. * * since 3.16.0 */ export interface RenameFilesParams { /** * An array of all files/folders renamed in this operation. When a folder * is renamed, only the folder will be included, and not its children. */ files: FileRename[]; }其中每个重命名项由FileRename描述/** * Represents information on a file/folder rename. * * since 3.16.0 */ export interface FileRename { /** * A file:// URI for the original location of the file/folder being renamed. */ oldUri: string; /** * A file:// URI for the new location of the file/folder being renamed. */ newUri: string; }要点解读files是一个数组一次可以携带批量重命名的结果例如一次涉及多个文件的重构操作每个元素通过oldUri与newUri一对 URI 表达从哪到哪URI 使用file://scheme文件夹重命名时只上报文件夹本身不展开其子项only the folder will be included, and not its children。服务器如果维护按目录组织的信息应据此推断整个子树的迁移而不是等待逐个子文件的条目。一个典型的通知负载示例如下{ jsonrpc: 2.0, method: workspace/didRenameFiles, params: { files: [ { oldUri: file:///workspace/src/foo.ts, newUri: file:///workspace/src/bar.ts }, { oldUri: file:///workspace/src/lib, newUri: file:///workspace/src/utils } ] } }该结构同时被workspace/willRenameFiles请求复用见 _specifications/lsp/3.18/workspace/willRenameFiles.md 中的_Request_段因此在实现时可以共享同一套参数解析与验证逻辑。注册过滤器FileOperationRegistrationOptions 家族服务器声明workspace.fileOperations.didRename时使用FileOperationRegistrationOptions该类型以及它的过滤器家族完整定义在 _specifications/lsp/3.18/workspace/willCreateFiles.md 中since 3.16.0并被 create / rename / delete 三组文件操作共用。结构如下/** * The options to register for file operations. * * since 3.16.0 */ interface FileOperationRegistrationOptions { /** * The actual filters. */ filters: FileOperationFilter[]; }每个过滤器由 URI scheme 与 glob 模式组成export interface FileOperationFilter { /** * A URI scheme, like file or untitled. */ scheme?: string; /** * The actual file operation pattern. */ pattern: FileOperationPattern; }模式本身支持指定匹配对象文件 / 文件夹 / 两者与大小写选项export interface FileOperationPattern { glob: string; /** * Whether to match files or folders with this pattern. * * Matches both if undefined. */ matches?: FileOperationPatternKind; /** * Additional options used during matching. */ options?: FileOperationPatternOptions; } export namespace FileOperationPatternKind { export const file: file file; export const folder: folder folder; } export type FileOperationPatternKind file | folder; export interface FileOperationPatternOptions { ignoreCase?: boolean; }规范原文对 glob 语法给出了明确的说明*匹配一个路径段内的零个或多个字符?匹配一个路径段内的单个字符**匹配任意多个路径段含零个{}将子模式组合为 OR 表达式例如**/*.{ts,js}匹配所有 TypeScript 与 JavaScript 文件[]声明路径段内的字符范围例如example.[0-9]可匹配example.0、example.1等[!...]对字符范围取反例如example.[!0-9]匹配example.a、example.b但不匹配example.0。实战中服务器通常用过滤器把通知收敛到关心的文件类型。例如只关心 TypeScript 源文件的重命名{ filters: [ { scheme: file, pattern: { glob: **/*.{ts,tsx}, matches: file, options: { ignoreCase: false } } } ] }若matches缺省则文件和文件夹都匹配scheme缺省时则对任意 scheme 生效。这些选项同样适用于didCreate/willCreate/didDelete/willDelete等相邻能力参见 _specifications/lsp/3.18/workspace/didCreateFiles.md、_specifications/lsp/3.18/workspace/didDeleteFiles.md。动态注册dynamicRegistrationFileOperationClientCapabilities中的dynamicRegistration?: boolean字段表明如果客户端支持文件操作的动态注册服务器可以在会话运行期间通过client/registerCapability动态注册workspace/didRenameFiles的兴趣而不必在初始化阶段静态声明。这也解释了为何服务器能力中的fileOperations.didRename是一个携带过滤器的选项对象——它既可出现在initialize响应中也可出现在动态注册的注册选项registration options中。与 willRenameFiles 的时序配合先请求、后通知workspace/didRenameFiles通常与workspace/willRenameFiles请求成对出现二者构成完整的重命名前协商 重命名后通知流程_specifications/lsp/3.18/workspace/willRenameFiles.md重命名前客户端发送workspace/willRenameFiles请求服务器可返回一个WorkspaceEdit供客户端在真正重命名前应用例如同步修改被引用路径的导入语句。请求参数同样为RenameFilesParams因此服务器收到的oldUri/newUri与后续通知中的完全一致重命名中客户端应用服务器返回的 edit然后执行实际的文件重命名重命名后客户端发送workspace/didRenameFiles通知携带同一组oldUri/newUri服务器据此做善后处理更新索引、缓存、诊断关联等。该通知不需要也不允许返回结果服务器应把重心放在幂等、高效的状态同步上。规范还提示客户端可能因为计算耗时过长或服务器频繁失败而丢弃willRenameFiles请求的结果以保证重命名操作快速可靠服务器在收到didRenameFiles时应能容忍前置请求可能未被执行的情况。在 3.16/3.17/3.18 版本间的一致性workspace/didRenameFiles自 3.16.0 引入其消息定义、能力属性名与参数类型在 3.16、3.17、3.18 三个版本中保持一致。仓库中 3.17 版文档 _specifications/lsp/3.17/workspace/didRenameFiles.md 与 3.18 版内容完全相同3.18 版在FileOperationClientCapabilities中额外扩展了inlineValue、diagnostics、textDocumentContent等新能力见 _specifications/lsp/3.18/general/initialize.md但文件操作相关字段并未变更。因此实现方可以放心复用 3.16 时代以来的参数结构与过滤器逻辑无需为 3.18 做额外兼容分支。服务端实现建议综合规范原文与类型定义语言服务器实现workspace/didRenameFiles时的最小工作清单如下能力声明在initialize响应或通过动态注册中给出workspace.fileOperations.didRename并配置filters限定关心的 glob 与 scheme参数解析按RenameFilesParams.files[].oldUri/newUri解析 URI注意文件夹条目不展开子项的语义自行推导子树影响范围状态同步更新内部索引中 URI → 符号、文档、诊断的映射对oldUri对应的文档执行已迁移处理并在newUri下重建条目幂等处理不假设willRenameFiles一定被调用过也不对通知触发顺序做强依赖批量支持正确处理files数组中的多条记录重命名操作往往是批量事务的一部分。以上类型与接口的权威定义可在仓库 _specifications/lsp/3.18/metaModel/metaModel.json 中检索到对应的结构描述如RenameFilesParams、FileRename、FileOperationRegistrationOptions等可作为实现校验与生成客户端/服务端类型绑定的依据。结语workspace/didRenameFiles是 LSP 文件操作能力闭环中的收尾通知它把客户端内部的用户重命名行为转化为服务器可消费的结构化事件oldUri→newUri。通过与workspace/willRenameFiles请求、FileOperationRegistrationOptions过滤器机制以及初始化能力协商的组合语言服务器得以在重命名发生后精确、及时地维护自身的索引与状态从而保证跨文件引用、符号导航等功能在文件迁移后依然准确可用。赞分享开发工具【免费下载链接】language-server-protocolDefines a common protocol for language servers.项目地址https://gitcode.com/gh_mirrors/la/language-server-protocol点击查看免费下载相关推荐Language Server Protocol 文件删除事件workspace/didDeleteFiles 通知详解Language Server Protocol 文件删除事件workspace/didDeleteFiles 通知详解 导读 workspace/didDe开发工具Language Server Protocol 文件重命名前钩子深入解析 workspace/willRenameFiles 请求Language Server Protocol 文件重命名前钩子深入解析 workspace/willRenameFiles 请求 导读 workspace开发工具深入解析 Biome 的 Markdown 格式化setext 标题与跨行 HTML 的边界处理example-59 用例剖析深入解析 Biome 的 Markdown 格式化setext 标题与跨行 HTML 的边界处理example 59 用例剖析 导读 本文以 Biome开发工具上一篇Plain Craft Launcher 2重新定义Minecraft模组管理的智能启动器解决方案下一篇Python 核心语法速查基于 awesome-cheatsheets 的完整语言手册创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考