GUI 程序(-H windowsgui)起 console 子进程时若未设 HideWindow, Windows 会为其分配新控制台。refreshGit 一次连跑 3 个 git 进程 = 3 个闪窗(主因),另有 taskkill 和 go vet 两处。 统一 procAttr() 模式(Windows 返回 HideWindow:true,其他平台 nil): - internal/git: 新建 procattr_windows/other.go,run/runOut 挂上 - internal/task: killTree(taskkill)挂上 - internal/app: 新建 procattr_windows/other.go,go vet 挂上
98 lines
2.4 KiB
Go
98 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
|
|
cmd.SysProcAttr = procAttr()
|
|
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
|
|
}
|