Files
MonocoEditor-With-Lsp-Backend/backend
2026-02-15 15:55:49 +08:00
..
all
2026-02-15 15:55:49 +08:00
all
2026-02-15 15:55:49 +08:00
all
2026-02-15 15:55:49 +08:00
all
2026-02-15 15:55:49 +08:00
all
2026-02-15 15:55:49 +08:00
all
2026-02-15 15:55:49 +08:00

Monaco Go Completion Backend

Gin + gopls(JSON-RPC/LSP over stdio) 实现的 Go 代码补全后端。

运行

go mod tidy
go run ./cmd/server

默认监听 http://localhost:8080

环境变量

  • PORTHTTP 端口,默认 8080
  • GOPLS_PATHgopls 可执行文件路径,默认 gopls
  • WORKSPACE_DIRgopls 工作目录,默认当前目录
  • CORS_ALLOW_ORIGINCORS 允许来源,默认 *

API

健康检查

  • GET /health

Go 补全

  • POST /api/v1/completions/go
  • Content-Type: application/json

Go 补全WebSocket

  • GET /ws/completions/go
  • 客户端发送:
{
  "id": "1",
  "uri": "file:///main.go",
  "text": "package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Pri\n}",
  "line": 5,
  "character": 11
}
  • 服务端返回:
{
  "id": "1",
  "items": [{ "label": "Println" }],
  "isIncomplete": false
}

请求体:

{
  "uri": "file:///C:/Users/meowr/Desktop/bishe/monica_editor_with_code_completion/backend/playground.go",
  "text": "package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Pri\n}",
  "line": 5,
  "character": 11
}

说明:

  • uri 建议使用绝对 file:// URIWORKSPACE_DIR 在同一工作区最稳定)。
  • 也支持 file:///main.go 这类相对根路径 URI后端会自动映射到 WORKSPACE_DIR 下。
  • line / character 为 0-based。
  • 每次请求都要传前端当前全文 text

响应体:

{
  "items": [
    {
      "label": "Println",
      "kind": 3,
      "detail": "func(a ...any) (n int, err error)",
      "documentation": "Println formats using the default formats..."
    }
  ],
  "isIncomplete": false
}