- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
139 lines
4.3 KiB
Go
139 lines
4.3 KiB
Go
package app
|
|
|
|
import (
|
|
_ "embed"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
)
|
|
|
|
// gopherPNG 是 Go 语言吉祥物的位图,用作 .go 文件的类型图标。
|
|
//
|
|
// 不用应用自己的 logo:那是「MyGoland 的标识」,而文件树上的图标说的是
|
|
// 「这是什么语言写的」,Go 源文件就该用语言本身的形象。图由
|
|
// docs 里那张带 GOLANG 字样的图裁出并去掉白底,64×64 透明 PNG。
|
|
//
|
|
//go:embed assets/gopher.png
|
|
var gopherPNG []byte
|
|
|
|
// gopherIcon 是解码后的位图。视图每帧重建,解码只能做一次。
|
|
var gopherIcon = func() ui.ImageSource {
|
|
b, err := ui.DecodeBitmap(gopherPNG)
|
|
if err != nil {
|
|
// 图标缺失不该影响文件树,退化成回落到矢量代码图标。
|
|
logf("gopher 图标解码失败: %v", err)
|
|
return nil
|
|
}
|
|
return b
|
|
}()
|
|
|
|
// 文件树的图标。mygo 的 TreeItem 只接受一段文字,挂不了图标,所以树行是
|
|
// 自绘的(见 view.go 的 treeRow),这里只负责「给一个名字,返回画什么」。
|
|
//
|
|
// 三个单色矢量图标都用 currentColor 填充,由 ui.Icon 的 TextColor 上色,
|
|
// 深浅主题自动跟随;只有 Go 文件用 gopher 位图——那个是彩色的吉祥物,
|
|
// 换成单色矢量反而认不出来。
|
|
var (
|
|
iconFolder = mustSVG(`<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill="currentColor" d="M1.6 3.4c0-.5.4-.9.9-.9h3.2l1.4 1.6h6.4c.5 0 .9.4.9.9v7.2c0 .5-.4.9-.9.9H2.5c-.5 0-.9-.4-.9-.9z"/></svg>`)
|
|
iconFile = mustSVG(`<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill="currentColor" d="M3.6 1.6h5.2l3.6 3.6v9.2H3.6zm5.4.9v3h3z"/></svg>`)
|
|
iconCode = mustSVG(`<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill="currentColor" d="M6.1 3.9 1.9 8.1l4.2 4.2 1.1-1.1L4.1 8.1l3.1-3.1zm3.8 0-1.1 1.1 3.1 3.1-3.1 3.1 1.1 1.1 4.2-4.2z"/></svg>`)
|
|
)
|
|
|
|
func mustSVG(s string) *ui.SVG {
|
|
v, err := ui.ParseSVG([]byte(s))
|
|
if err != nil {
|
|
logf("解析图标失败: %v", err)
|
|
return nil
|
|
}
|
|
return v
|
|
}
|
|
|
|
// fileIcon 在文件树的一行前面画上类型图标。
|
|
func (a *App) fileIcon(c *ui.Context, name string, isDir bool) {
|
|
t := c.Theme()
|
|
lower := strings.ToLower(name)
|
|
|
|
// Go 源码用语言吉祥物本身做图标。
|
|
if !isDir && strings.HasSuffix(lower, ".go") {
|
|
if gopherIcon != nil {
|
|
ui.Image(c, gopherIcon).Size(14, 14)
|
|
return
|
|
}
|
|
}
|
|
|
|
var svg *ui.SVG
|
|
var col ui.Color
|
|
switch {
|
|
case isDir:
|
|
svg, col = iconFolder, t.Accent
|
|
case lower == "go.mod", lower == "go.sum", strings.HasSuffix(lower, ".mod"),
|
|
strings.HasSuffix(lower, ".sum"):
|
|
svg, col = iconCode, ui.Hex(a.codeColor())
|
|
case isCodeFile(lower):
|
|
svg, col = iconCode, ui.Hex(a.codeColor())
|
|
case isConfigFile(lower):
|
|
svg, col = iconFile, t.Warning
|
|
case isImageFile(lower):
|
|
svg, col = iconFile, t.Success
|
|
case isDocFile(lower):
|
|
svg, col = iconFile, t.TextMuted
|
|
default:
|
|
svg, col = iconFile, t.TextMuted
|
|
}
|
|
|
|
if svg == nil {
|
|
ui.Box(c).Size(14, 14)
|
|
return
|
|
}
|
|
ui.Icon(c, svg).Size(14, 14).TextColor(col)
|
|
}
|
|
|
|
// codeColor 是源码类文件的图标色,深浅主题各一档,保证在两种底色上都跳得出来。
|
|
func (a *App) codeColor() string {
|
|
if a.light {
|
|
return "#0969DA"
|
|
}
|
|
return "#61AFEF"
|
|
}
|
|
|
|
func isCodeFile(l string) bool {
|
|
for _, ext := range []string{".php", ".js", ".mjs", ".cjs", ".ts", ".jsx", ".tsx",
|
|
".vue", ".html", ".htm", ".css", ".scss", ".less", ".sql", ".sh", ".bat", ".ps1",
|
|
".c", ".h", ".cpp", ".hpp", ".cs", ".java", ".kt", ".py", ".rb", ".rs"} {
|
|
if strings.HasSuffix(l, ext) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isConfigFile(l string) bool {
|
|
for _, ext := range []string{".json", ".yaml", ".yml", ".toml", ".ini", ".conf",
|
|
".xml", ".properties", ".env"} {
|
|
if strings.HasSuffix(l, ext) {
|
|
return true
|
|
}
|
|
}
|
|
return l == ".gitignore" || l == ".gitattributes" || l == "dockerfile" || l == "makefile"
|
|
}
|
|
|
|
func isImageFile(l string) bool {
|
|
for _, ext := range []string{".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp",
|
|
".ico", ".svg"} {
|
|
if strings.HasSuffix(l, ext) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isDocFile(l string) bool {
|
|
for _, ext := range []string{".md", ".markdown", ".txt", ".rst", ".pdf", ".doc",
|
|
".docx", ".xls", ".xlsx", ".ppt", ".pptx"} {
|
|
if strings.HasSuffix(l, ext) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|