You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LSP规范中WorkspaceEdit的changes字段语法解析请求

Understanding the changes field syntax in LSP's WorkspaceEdit TypeScript interface

Great question! This isn't a spelling mistake at all—it's a core TypeScript syntax feature called an index signature, designed to define objects with dynamic string keys. Let me break this down clearly for developers coming from other languages, and tie it to the LSP context.

First, let's start with a properly formatted version of the WorkspaceEdit interface to make the syntax easier to read:

export interface WorkspaceEdit {
  /**
   * 存储对现有资源的变更。
   */
  changes?: { 
    [uri: string]: TextEdit[]; 
  };

  /**
   * 一个`TextDocumentEdit`数组,用于表示对n个不同文本文档的变更,其中每个文本文档变更对应文本文档的特定版本。客户端是否支持版本化文档变更通过`WorkspaceClientCapabilities.workspaceEdit.documentChanges`表示。
   */
  documentChanges?: TextDocumentEdit[];
}

What does the changes syntax mean?

The { [uri: string]: TextEdit[]; } part is TypeScript's way of defining an object where:

  • Keys: Every property name (key) in this object must be a string. The uri here is just a descriptive placeholder name—it tells developers that these keys are expected to be document URIs (like file:///path/to/file.txt) that identify which file the edits apply to.
  • Values: Each key maps to an array of TextEdit objects. These are the specific text modifications (like insertions, deletions, or replacements) to apply to the document at the corresponding URI.

Analogies to other programming languages

If you're implementing LSP in a language other than TypeScript, this structure translates directly to a dictionary/map type where keys are strings and values are lists of edit objects:

  • Python: dict[str, list[TextEdit]] (Python 3.9+) or Dict[str, List[TextEdit]] (using typing module)
  • Java: Map<String, List<TextEdit>>
  • C#: Dictionary<string, List<TextEdit>>
  • Go: map[string][]TextEdit

Context in LSP

In the Language Server Protocol, the changes field is a simple way to batch edits across multiple documents without tracking document versions. It's the older, more widely supported alternative to documentChanges (which adds versioning for more precise edit handling).

内容的提问来源于stack exchange,提问作者fferri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:42:32