Files
MyGolandRelease/internal/app/wscheck.go
T
zerorigin a80c0f886a MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
2026-11-04 16:37:05 +08:00

97 lines
2.4 KiB
Go

package app
import (
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/egoist/mygo/ui/editor"
)
// wsCheckState 是全工作区检查的状态。
type wsCheckState struct {
on bool
items []Problem
at int64
err string
}
// RunWorkspaceCheck 在工作区根目录跑 go vet,把结果并进「问题」面板。
//
// 为什么用 go vet 而不是问语言服务器:LSP 没有「把整个项目的诊断给我」这种请求,
// 它只推已打开的文件。vet 恰好能一次覆盖整个模块,代价是编译期检查而非编辑器实时分析。
func (a *App) RunWorkspaceCheck() {
if a.root == "" || a.wsState.on {
return
}
root := a.root
a.wsState.on = true
a.wsState.err = ""
a.panel = true
a.panelTab = 1
a.win.Update(func() {})
go func() {
cmd := exec.Command("go", "vet", "./...")
cmd.Dir = root
out, err := cmd.CombinedOutput()
items := parseVetOutput(root, string(out))
a.win.Update(func() {
a.wsState.on = false
a.wsState.items = items
if err != nil && len(items) == 0 {
// 没有解析出问题但命令失败:多半是环境问题(没装 go、模块坏了),
// 直接把输出甩到运行面板,别让它悄无声息地失败。
a.wsState.err = strings.TrimSpace(string(out))
a.panelTab = 0
a.appendOut("go vet 失败:" + a.wsState.err)
}
a.win.Update(func() {})
})
}()
}
// parseVetOutput 解析 `path:line:col: message` 形式的行。
func parseVetOutput(root, out string) []Problem {
var ps []Problem
for _, line := range strings.Split(out, "\n") {
line = strings.TrimRight(line, "\r")
i := strings.Index(line, ".go:")
if i < 0 {
continue
}
path := line[:i+3]
rest := line[i+4:]
parts := strings.SplitN(rest, ":", 3)
if len(parts) < 2 {
continue
}
lineNo, err := strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil {
continue
}
col, msg := 0, ""
if c, err := strconv.Atoi(strings.TrimSpace(parts[1])); err == nil {
col = c
if len(parts) == 3 {
msg = parts[2]
}
} else {
msg = strings.Join(parts[1:], ":")
}
if !filepath.IsAbs(path) {
path = filepath.Join(root, filepath.FromSlash(strings.TrimPrefix(path, "./")))
}
ps = append(ps, Problem{
Path: filepath.Clean(path),
Line: lineNo,
Col: col + 1,
Text: strings.TrimSpace(msg),
Severity: editor.SeverityError,
Source: "vet",
})
}
return ps
}