package app import ( "encoding/json" "os" "path/filepath" "github.com/egoist/mygo/ui" "github.com/egoist/mygo/ui/editor" ) // settings 是跨工作区的编辑器偏好,存在用户配置目录里。 type settings struct { FontSize float32 `json:"fontSize"` LineHeight float32 `json:"lineHeight"` TabSize int `json:"tabSize"` Spaces bool `json:"spaces"` SoftWrap bool `json:"softWrap"` FormatOnSave bool `json:"formatOnSave"` // OrganizeImports 在保存 Go 文件时顺手整理 import:补缺失的、删没用的。 OrganizeImports bool `json:"organizeImports"` Light bool `json:"light"` // History 开关本地历史(保存后留点、历史面板)。 History bool `json:"history"` // AutoSnapshot 在编辑停手后自动留点(只写历史,不动原文件)。 AutoSnapshot bool `json:"autoSnapshot"` // InlayHints 控制参数名/类型这类内联提示。 // 老配置文件里没有这一项,读的时候要按「默认开」处理(见 loadSettings)。 InlayHints bool `json:"inlayHints"` // TranslateDocs 开关补全文档的中文翻译(需配好下面的 AI 接口)。 TranslateDocs bool `json:"translateDocs"` // AI 接口三件套:OpenAI 兼容的地址、Key 与模型名, // 如 https://api.deepseek.com/v1 + sk-xxx + deepseek-chat。 AIBaseURL string `json:"aiBaseUrl"` AIAPIKey string `json:"aiApiKey"` AIModel string `json:"aiModel"` } // defaultSettings 是没配置过时的取值,和改动前的硬编码保持一致。 func defaultSettings() settings { return settings{ FontSize: 13, LineHeight: 1.4, TabSize: 4, Spaces: false, // Go 用 tab SoftWrap: false, FormatOnSave: true, // gopls 的整理是确定性改动(补缺/删无用),默认开。 OrganizeImports: true, History: true, AutoSnapshot: true, InlayHints: true, TranslateDocs: true, } } // inlaysOn 返回内联提示是否开启。 func (a *App) inlaysOn() bool { return a.set.InlayHints } // settingsPath 返回设置文件路径。 func settingsPath() string { dir := sessionDir() if dir == "" { return "" } return filepath.Join(dir, "settings.json") } // loadSettings 读设置,读不到就用默认值。 func loadSettings() settings { st := defaultSettings() p := settingsPath() if p == "" { return st } b, err := os.ReadFile(p) if err != nil { return st } // 缺字段时保留默认值,避免旧文件把新字段清零。 if err := json.Unmarshal(b, &st); err != nil { logf("读取设置失败: %v", err) return defaultSettings() } return st } // saveSettings 写设置文件。 func saveSettings(st settings) { p := settingsPath() if p == "" { return } if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { logf("保存设置失败: %v", err) return } b, err := json.MarshalIndent(st, "", " ") if err != nil { logf("保存设置失败: %v", err) return } if err := os.WriteFile(p, b, 0o644); err != nil { logf("保存设置失败: %v", err) } } // OpenSettings 打开设置弹窗。 func (a *App) OpenSettings() { a.setDlg = a.set a.setOpen = true a.win.Update(func() {}) } // settingsDialog 画设置弹窗。改动即时预览,点「确定」才落盘。 func (a *App) settingsDialog(c *ui.Context) { if !a.setOpen { return } t := c.Theme() ui.Modal(c, &a.setOpen, func() { ui.Column(c).Width(520).Gap(12).Children(func() { ui.Text(c, "设置").FontSize(14).Bold().TextColor(t.Text) ui.Text(c, "改动即时生效,点「确定」保存").FontSize(10).TextColor(t.TextMuted).SingleLine() row := func(label string, body func()) { ui.Row(c).FillWidth().Gap(12).AlignItems(ui.Center).Children(func() { ui.Text(c, label).FontSize(12).TextColor(t.Text).Width(96) body() }) } fontIdx := indexOfFloat(fontSizes, a.setDlg.FontSize) row("字号", func() { if ui.Segmented(c, &fontIdx, "12", "13", "14", "15", "16").Changed() { a.setDlg.FontSize = fontSizes[fontIdx] } }) lhIdx := indexOfFloat(lineHeights, a.setDlg.LineHeight) row("行高", func() { if ui.Segmented(c, &lhIdx, "1.3", "1.4", "1.5", "1.7").Changed() { a.setDlg.LineHeight = lineHeights[lhIdx] } }) tabIdx := 0 switch a.setDlg.TabSize { case 2: tabIdx = 0 case 4: tabIdx = 1 case 8: tabIdx = 2 } row("缩进宽度", func() { if ui.Segmented(c, &tabIdx, "2", "4", "8").Changed() { a.setDlg.TabSize = tabSizes[tabIdx] } }) row("缩进方式", func() { ui.Checkbox(c, &a.setDlg.Spaces, "用空格缩进(Go 一般不勾)") }) row("自动换行", func() { ui.Checkbox(c, &a.setDlg.SoftWrap, "超宽自动折行") }) row("保存时", func() { ui.Checkbox(c, &a.setDlg.FormatOnSave, "保存时自动格式化 Go 文件") ui.Checkbox(c, &a.setDlg.OrganizeImports, "保存时整理 import(补缺失 / 删无用)") ui.Checkbox(c, &a.setDlg.InlayHints, "显示类型 / 参数名内联提示") ui.Checkbox(c, &a.setDlg.History, "保留本地历史(保存后留快照)") ui.Checkbox(c, &a.setDlg.AutoSnapshot, "停手后自动留快照(不自动改磁盘)") }) themeIdx := 0 if a.setDlg.Light { themeIdx = 1 } row("主题", func() { if ui.Segmented(c, &themeIdx, "深色", "浅色").Changed() { a.setDlg.Light = themeIdx == 1 a.SetLight(a.setDlg.Light) } }) // AI 文档翻译:补全列表底部的英文 godoc 译成中文。 row("文档翻译", func() { ui.Checkbox(c, &a.setDlg.TranslateDocs, "补全文档翻译成中文(OpenAI 兼容接口)") }) if a.setDlg.TranslateDocs { row("接口地址", func() { ui.TextInput(c, &a.setDlg.AIBaseURL).FillWidth().FontSize(12) }) row("API Key", func() { ui.TextInput(c, &a.setDlg.AIAPIKey).FillWidth().FontSize(12) }) row("模型", func() { ui.TextInput(c, &a.setDlg.AIModel).FillWidth().FontSize(12) }) ui.Text(c, "示例:https://api.deepseek.com/v1 + deepseek-chat,或本地 Ollama http://localhost:11434/v1。"). FontSize(10).TextColor(t.TextMuted).SingleLine() } ui.Row(c).Gap(8).Justify(ui.End).Children(func() { if ui.Button(c, "恢复默认").Clicked() { a.setDlg = defaultSettings() a.applySettings(a.setDlg) } if ui.Button(c, "取消").Clicked() { // 取消要把预览时的改动退回去。 a.applySettings(a.set) a.setOpen = false } if ui.Button(c, "确定").Clicked() { a.applySettings(a.setDlg) a.set = a.setDlg saveSettings(a.set) a.setOpen = false } }) }) }) // 即时预览:滑块一动就套到已打开的编辑器上。 a.applySettings(a.setDlg) if !a.setOpen { a.focusEditorSoon() } } var ( fontSizes = []float32{12, 13, 14, 15, 16} lineHeights = []float32{1.3, 1.4, 1.5, 1.7} tabSizes = []int{2, 4, 8} ) func indexOfFloat(list []float32, v float32) int { for i, x := range list { if x == v { return i } } return 1 } // applySettings 把设置套到所有已打开的编辑器上。 // // 弹窗里每帧都会调它做即时预览,所以先跟「已应用的值」比一次: // 没变就直接返回,避免每帧都请求重绘。 func (a *App) applySettings(st settings) { if a.applied == st { return } prev := a.applied a.applied = st indent := editor.IndentAuto if st.Spaces { indent = editor.IndentSpaces } for _, d := range a.docs { if d.st == nil { continue } d.st.Font = ui.Font{Size: st.FontSize} d.st.LineHeight = st.LineHeight d.st.TabSize = st.TabSize d.st.Indent = indent d.st.SoftWrap = st.SoftWrap } // 内联提示开关变了就重来一遍:关掉要清空,打开要重新问 gopls。 if prev.InlayHints != st.InlayHints { for _, d := range a.docs { a.scheduleInlays(d) } } if a.win != nil { a.win.Update(func() {}) } } // editorSettings 返回新文档该用的排版参数。 func (a *App) editorSettings() (ui.Font, float32, int, editor.IndentStyle, bool) { st := a.applied indent := editor.IndentAuto if st.Spaces { indent = editor.IndentSpaces } return ui.Font{Size: st.FontSize}, st.LineHeight, st.TabSize, indent, st.SoftWrap }