MoonBit Agent IDE#

概览#

moon ide 是一套专门用于导航、理解和探索 MoonBit 代码库的 CLI 工具。它基于编译器对项目结构的理解,提供具备语义感知能力的代码发现工具。

这些命令使用语义分析而不是文本匹配,因此在代码导航任务中比基于 grep 的搜索更精确。

可用命令#

  • moon ide peek-def:在上下文中定位符号定义

  • moon ide find-references:定位符号在整个项目中的所有用法

  • moon ide outline:获取文件或包的结构概览

  • moon ide doc:结合文档发现并探索 API

符号语法#

所有 moon ide 子命令都接受 <symbol> 参数,支持以下模式:

基础符号:

  • [@pkg.]symbol - 包中的符号(例如 parse_intArray

  • 如果省略 @pkg.,则搜索当前包和 prelude

类型成员:

  • [@pkg.]Type::member - 类型方法、结构体字段、枚举变体、trait 方法

  • 示例:Array::length@http.Request::newOption::None

对于当前包和标准库中的符号,包前缀 @pkg. 是可选的。

moon ide peek-def - 查看符号定义#

快速定位符号定义及其周边上下文。相比用 grep 查找声明,这种方式更快也更准确。

用法#

moon ide peek-def <symbol> [-loc filename:line[:col]]

两种模式:

  1. 全局搜索(不带 -loc):使用上文描述的符号语法在整个项目中搜索

  2. 上下文搜索(提供 -loc):在指定位置将 <symbol> 作为子串进行匹配

    • 行号必须精确;列号可选(作为提示使用)

    • <symbol> 参数按纯文本匹配,不会按符号语法解析

    • 当符号名有歧义或出现在多个上下文中时非常有用

示例#

$ moon ide peek-def Agent
Found 1 symbols matching 'Agent':

`pub (all) struct Agent` in package moonbitlang/maria/agent at ./agent/agent.mbt:17-47

17 | ///|
   | /// Represents an AI agent that interacts with language models and executes tools.
...|
   | pub(all) struct Agent {
...|
...| }
$ moon ide peek-def Agent -loc ./maria.mbt:7
Definition found at file ./maria/agent/agent.mbt
   | ///
   | /// The `Agent` struct encapsulates the complete state and behavior of an AI agent,
...|
24 | pub(all) struct Agent {
   |                 ^^^^^
   |   /// UUID generator for creating unique identifiers.
...|

moon ide find-references - 追踪所有用法#

了解一个符号在整个代码库中的使用位置和方式。

用法#

moon ide find-references <symbol>

注意:目前还不支持 -loc 参数。始终执行全局搜索。

示例#

$ moon ide find-references Agent
`pub (all) struct Agent` in package moonbitlang/maria/agent at ./agent/agent.mbt:17-47
17 | ///|
   | /// Represents an AI agent that interacts with language models and executes tools.
...|
   | pub(all) struct Agent {
...|
...| }
Found 98 references of this symbol:
./agent/agent_tool.mbt:41:8-41:13:
   | /// * `agent` : The agent instance whose tools will be enabled or disabled.
   | /// * `tool_names` : A set containing the names of tools that should be enabled.
   | ///   Tools not in this set will be disabled.
41 | pub fn Agent::set_enabled_tools(
   |        ^^^^^
   |   agent : Agent,
   |   tool_names : Set[String],
...

moon ide outline - 获取结构概览#

快速扫描文件或包的结构,理解其组织方式。

用法#

moon ide outline <path/to/file_or_directory>

两种模式:

  • moon ide outline path/to/file.mbt - 查看单个文件的结构

  • moon ide outline path/to/directory - 查看包内所有 .mbt 文件的结构

示例#

包结构 - 查看所有文件及其顶层符号:

moon ide outline .
maria.mbt:
 L05 | pub struct Maria {
       ...
 L71 | pub fn Maria::close(self : Maria) -> Unit {
       ...
 L84 | pub async fn Maria::start(self : Maria, prompt? : String) -> Unit {

文件结构 - 查看单个文件的详细结构:

$ moon ide outline ./internal/readline/readline.mbt
 L0002 | priv struct Edit {
         ...
 L0008 | pub enum KeyName {
         ...
 L0045 | pub struct Key {
         ...
 L1369 | pub async fn Interface::start(self : Interface) -> Unit {
         ...
 L1383 | pub async fn Interface::read_line(self : Interface) -> String {
         ...
 L1390 | pub async fn Interface::read_key(self : Interface) -> Key {
         ...

moon ide doc - 发现与探索 API#

查询文档并发现可用的符号、包和 API。

用法#

moon ide doc '<query>'

查询语法#

查询语法专门用于符号和包的发现:

空查询 - 列出可用包或符号:

  • moon ide doc ''

    • 在模块内:显示所有可用包(包括依赖和 moonbitlang/core

    • 在包内:显示当前包中的所有符号

    • 在包外:显示所有可用包

按名称查找:

  • moon ide doc "[@pkg.]function_name" - 查找函数或值

  • moon ide doc "[@pkg.]TypeName" - 查找类型(内建类型不需要前缀)

  • moon ide doc "[@pkg.]Type::member" - 查找类型方法或字段

  • moon ide doc "[@pkg.]Type::member" - 查找类型方法或字段

包探索:

  • moon ide doc "@pkg" - 列出一个包中所有导出符号

  • moon ide doc "@encoding/utf8" - 支持嵌套包

  • 示例:moon ide doc "@json"moon ide doc "@buffer"

通配匹配(Globbing) - 使用 * 通配符进行部分匹配:

  • moon ide doc "String::*rev*" - 查找名称中包含 “rev” 的所有 String 方法

  • moon ide doc "*parse*" - 查找名称中包含 “parse” 的所有符号

示例#

搜索类型方法:

# Search for String methods in standard library:
$ moon ide doc "String"

type String

  pub fn String::add(String, String) -> String
  # ... more methods omitted ...

探索一个包:

$ moon ide doc "@buffer"
moonbitlang/core/buffer

fn from_array(ArrayView[Byte]) -> Buffer
# ... omitted ...

查询特定函数:

$ moon ide doc "@buffer.new"
package "moonbitlang/core/buffer"

pub fn new(size_hint? : Int) -> Buffer
  Creates ... omitted ...

使用通配匹配查找相关函数:

$ moon ide doc "String::*rev*"
package "moonbitlang/core/string"

pub fn String::rev(String) -> String
  Returns ... omitted ...
  # ... more

pub fn String::rev_find(String, StringView) -> Int?
  Returns ... omitted ...