MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
This commit is contained in:
commit
a80c0f886a
220 files changed
+20784
No files matched your search
@@ -0,0 +1,270 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user