调试适配器协议是一个伪装的REPL协议。
The Debug Adapter Protocol is a REPL protocol in disguise

原始链接: https://zignar.net/2025/06/23/debug-adapter-protocol-is-a-repl-protocol/

## REPL 的调试适配器协议:nluarepl & hprofdap 调试适配器协议 (DAP) 传统上用于调试,但令人惊讶的是,它非常适合构建 REPL(读取-求值-打印循环)。这种方法被用于创建诸如 **nluarepl**(Neovim 的 Lua REPL)和 **hprofdap**(用于检查 Java 堆转储)之类的工具。 DAP 的 `evaluate` 命令允许客户端发送表达式进行求值,并返回 `result` 以及可选的 `type`。重要的是,它包含一个 `variablesReference`,可以以 UI 中可导航的树的形式表示结构化数据。可以使用 `variables` 命令检索更多详细信息,从而可以深入检查数据结构,即使是嵌套或循环引用。DAP 还支持代码补全等功能。 虽然 DAP 包含断点处理等调试特定功能,但这些功能可以被绕过或为 REPL 使用而最小化实现——例如,nluarepl 使用日志点代替断点。使用 DAP 的主要动机是 **重用现有的调试 UI 元素**(例如 `nvim-dap` 中的元素),并避免重新发明 REPL 界面,从而将开发精力集中在核心求值逻辑上。这种方法提供了一种熟悉的用户体验,具有一致的键映射和行为。

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 工作 | 提交 登录 调试适配器协议是一个伪装的 REPL 协议 (zignar.net) 22 分,由 Malp 7 小时前发布 | 隐藏 | 过去的 | 收藏 | 1 条评论 o11c 4 小时前 [–] 嗯,这里的 `type` 是静态类型还是动态类型,对于同时适用两种类型的语言来说?回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Table of content


A couple months back I created nluarepl. It’s a REPL for the Neovim Lua interpreter with a little twist: It’s using the Debug Adapter Protocol. And before that, I worked on hprofdap. Also a kind of a REPL using DAP that lets you inspect Java heap dumps (.hprof files) using OQL.

As the name might imply, a REPL isn’t the main use case for the Debug Adapter Protocol (DAP). From the DAP page:

The idea behind the Debug Adapter Protocol (DAP) is to abstract the way how the debugging support of development tools communicates with debuggers or runtimes into a protocol.

But it works surprisingly well for a REPL interface to a language interpreter too.

Essentials

The typical REPL shows you a prompt after which you can enter an expression. You then hit Enter to submit the expression, it gets evaluated and you’re presented with the result or an error.

The Debug Adapter protocol defines a evaluate command which - as the name implies - evaluates expressions.

The definition for the payload the client needs to send looks like this:

nluarepl Demo

To get the data - or expand an option as shown in the demo above, the client must call the variables command with the variablesReference as payload. The response has an array of variables, where a variable looks like this:

completions command for that. Click on the link if you’re interested - I won’t go into detail about that here.

Another untypical feature for a REPL that the Debug Adapter Protocol provides is finding the locations of a variable definition. That’s also implemented in nluarepl, although it only works for functions.

The Boilerplate

You might be wondering if there is anything in the Debug Adapter Protocol one must implement that’s useless baggage if all you want is a REPL frontend or backend.

Yes, there are are a few things:

  • There’s the RPC mechanism, which is close to JSON-RPC, but not quite.
  • Breakpoint handling. You can send back a response that rejects all. (nluarepl implements log points - which is basically dynamic log statements you can create at runtime)
  • Session initialization. Here you can send back the capabilities.
  • launch/attach pseudo handling.
  • Disconnect/terminate handling. Not much needed here - you can use these to clean up any state.

The typical flow is that a client starts a debug session with a initialize command. Then the debug adapter replies with its capabilities and a normal client follows up sending breakpoints. After that it typically follows up with a launch command, which in a normal scenario would launch the application you want to debug.

To give you an impression of what this entails, here’s a snippet of the nluarepl code to implement the “dummy” actions:

Final question: Why would you do that?

Partly because of lazyness. From a development perspective I didn’t want to have to implement another REPL UI. Going the DAP route let me focus on the evaluation parts. And from a user perspective - I also wanted to re-use UI elements from nvim-dap. I’m used to that interface and have keymaps setup. I didn’t want to have another slightly different interface with different keymaps or behavior.

联系我们 contact @ memedata.com