go-mini 的 LSP / 查询能力建立在 AST 蓝图与语义上下文之上,和运行时执行链各自独立:
- LSP 使用
core/ast与core/lspserv - 执行使用 compiled artifact / prepared program / bytecode
IDE 能力基于源码和 AST,执行链基于 compiled artifact / prepared program / bytecode。
如果你在 Web IDE 或服务端集成,不必启动 cmd/lsp-server。推荐直接封装 core/lspserv:
executor := engine.NewMiniExecutor()
executor.InjectStandardLibraries()
lsp := lspserv.NewLSPServer(executor)典型请求流:
UpdateSession(uri, code)GetCompletions(uri, line, char)GetHover(uri, line, char)GetDefinition/GetReferences
每次请求都上传当前源码:
diags, _ := lsp.UpdateSession(uri, code)
items := lsp.GetCompletions(uri, line, char)适合:
- HTTP API
- Serverless
- 简单在线编辑器
特点:
- 后端不需要维护复杂会话
- 每次重新解析当前源码
- 更容易水平扩展
如果是 WebSocket 或长连接 IDE,可以复用同一个 LSPServer:
diags, _ := lsp.UpdateSession(msg.URI, msg.Code)
items := lsp.GetCompletions(msg.URI, msg.Line, msg.Char)
hover := lsp.GetHover(msg.URI, msg.Line, msg.Char)适合:
- 云 IDE
- 多文件大项目
- 需要持续缓存 session 的场景
UpdateSession 支持按 URI 维度逐个更新文件。只要这些文件属于同一包,LSP 会在语义层做包级合并分析。
for filename, code := range req.Files {
_, _ = lsp.UpdateSession("virtual://project/"+filename, code)
}
items := lsp.GetCompletions("virtual://project/"+req.CurrentFile, req.Line, req.Char)当前诊断能力包括:
- 语法错误
- 语义错误
- 跨文件符号错误
- 精确 range 输出
返回结构遵循 LSP 风格:
[
{
"range": {
"start": { "line": 4, "character": 2 },
"end": { "line": 4, "character": 14 }
},
"severity": 1,
"source": "go-mini",
"message": "类型不匹配: 无法将 String 赋值给 a (Int64)"
}
]需要明确区分:
- LSP / IDE 查询:基于 AST 蓝图
- 运行 / 反汇编 / 持久化:基于 bytecode / prepared program
如果你的系统既要编辑又要运行,推荐双链设计:
- 编辑时走
lspserv - 运行时走
CompileGoCode/NewRuntimeByCompiled/NewRuntimeByBytecodeJSON
不要把 LSP 会话缓存当作执行入口。