/
commands.go
260 lines (237 loc) · 8.32 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package codev
import (
"fmt"
"strings"
"cogentcore.org/cogent/code/code"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
"cogentcore.org/core/fileinfo"
"cogentcore.org/core/filetree"
"cogentcore.org/core/paint"
"cogentcore.org/core/styles"
"cogentcore.org/core/texteditor"
"cogentcore.org/core/views"
)
// RecycleCmdBuf creates the buffer for command output, or returns
// existing. If clear is true, then any existing buffer is cleared.
// Returns true if new buffer created.
func (ge *CodeView) RecycleCmdBuf(cmdNm string, clear bool) (*texteditor.Buffer, bool) {
if ge.CmdBufs == nil {
ge.CmdBufs = make(map[string]*texteditor.Buffer, 20)
}
if buf, has := ge.CmdBufs[cmdNm]; has {
if clear {
buf.NewBuffer(0)
}
return buf, false
}
buf := texteditor.NewBuffer()
buf.NewBuffer(0)
ge.CmdBufs[cmdNm] = buf
buf.Autosave = false
// buf.Info.Known = fileinfo.Bash
// buf.Info.Mime = fileinfo.MimeString(fileinfo.Bash)
// buf.Hi.Lang = "Bash"
return buf, true
}
// RecycleCmdTab creates the tab to show command output, including making a
// buffer object to save output from the command. returns true if a new buffer
// was created, false if one already existed. if sel, select tab. if clearBuf, then any
// existing buffer is cleared. Also returns index of tab.
func (ge *CodeView) RecycleCmdTab(cmdNm string, sel bool, clearBuf bool) (*texteditor.Buffer, *texteditor.Editor, bool) {
buf, nw := ge.RecycleCmdBuf(cmdNm, clearBuf)
ctv := ge.RecycleTabTextEditor(cmdNm, sel)
if ctv == nil {
return nil, nil, false
}
ctv.SetReadOnly(true)
ctv.SetBuffer(buf)
ctv.LinkHandler = func(tl *paint.TextLink) {
ge.OpenFileURL(tl.URL, ctv)
}
return buf, ctv, nw
}
// TabDeleted is called when a main tab is deleted -- we cancel any running commmands
func (ge *CodeView) TabDeleted(tabnm string) {
ge.RunningCmds.KillByName(tabnm)
}
// ExecCmdName executes command of given name -- this is the final common
// pathway for all command invokation except on a node. if sel, select tab.
// if clearBuf, clear the buffer prior to command
func (ge *CodeView) ExecCmdName(cmdNm code.CmdName, sel bool, clearBuf bool) {
cmd, _, ok := code.AvailableCommands.CmdByName(cmdNm, true)
if !ok {
return
}
ge.SetArgVarVals()
cbuf, _, _ := ge.RecycleCmdTab(cmd.Name, sel, clearBuf)
cmd.Run(ge, cbuf)
}
// ExecCmdNameFileNode executes command of given name on given node
func (ge *CodeView) ExecCmdNameFileNode(fn *filetree.Node, cmdNm code.CmdName, sel bool, clearBuf bool) {
cmd, _, ok := code.AvailableCommands.CmdByName(cmdNm, true)
if !ok || fn == nil || fn.This() == nil {
return
}
ge.ArgVals.Set(string(fn.FPath), &ge.Settings, nil)
cbuf, _, _ := ge.RecycleCmdTab(cmd.Name, sel, clearBuf)
cmd.Run(ge, cbuf)
}
// ExecCmdNameFilename executes command of given name on given file name
func (ge *CodeView) ExecCmdNameFilename(fn string, cmdNm code.CmdName, sel bool, clearBuf bool) {
cmd, _, ok := code.AvailableCommands.CmdByName(cmdNm, true)
if !ok {
return
}
ge.ArgVals.Set(fn, &ge.Settings, nil)
cbuf, _, _ := ge.RecycleCmdTab(cmd.Name, sel, clearBuf)
cmd.Run(ge, cbuf)
}
// ExecCmds gets list of available commands for current active file
func ExecCmds(ge *CodeView) [][]string {
tv := ge.ActiveTextEditor()
if tv == nil {
return nil
}
var cmds [][]string
vc := ge.VersionControl()
if ge.ActiveLang == fileinfo.Unknown {
cmds = code.AvailableCommands.FilterCmdNames(ge.Settings.MainLang, vc)
} else {
cmds = code.AvailableCommands.FilterCmdNames(ge.ActiveLang, vc)
}
return cmds
}
// ExecCmdNameActive calls given command on current active texteditor
func (ge *CodeView) ExecCmdNameActive(cmdNm string) { //types:add
tv := ge.ActiveTextEditor()
if tv == nil {
return
}
ge.SaveAllCheck(true, func() { // true = cancel option
ge.ExecCmdName(code.CmdName(cmdNm), true, true)
})
}
// CommandFromMenu pops up a menu of commands for given language, with given last command
// selected by default, and runs selected command.
func (ge *CodeView) CommandFromMenu(fn *filetree.Node) {
tv := ge.ActiveTextEditor()
core.NewMenu(code.CommandMenu(fn), tv, tv.ContextMenuPos(nil)).Run()
}
// ExecCmd pops up a menu to select a command appropriate for the current
// active text view, and shows output in Tab with name of command
func (ge *CodeView) ExecCmd() { //types:add
fn := ge.ActiveFileNode()
if fn == nil {
fmt.Printf("no Active File for ExecCmd\n")
return
}
ge.CommandFromMenu(fn)
}
// ExecCmdFileNode pops up a menu to select a command appropriate for the given node,
// and shows output in Tab with name of command
func (ge *CodeView) ExecCmdFileNode(fn *filetree.Node) {
ge.CommandFromMenu(fn)
}
// SetArgVarVals sets the ArgVar values for commands, from CodeView values
func (ge *CodeView) SetArgVarVals() {
tv := ge.ActiveTextEditor()
tve := texteditor.AsEditor(tv)
if tv == nil || tv.Buffer == nil {
ge.ArgVals.Set("", &ge.Settings, tve)
} else {
ge.ArgVals.Set(string(tv.Buffer.Filename), &ge.Settings, tve)
}
}
// ExecCmds executes a sequence of commands, sel = select tab, clearBuf = clear buffer
func (ge *CodeView) ExecCmds(cmdNms code.CmdNames, sel bool, clearBuf bool) {
for _, cmdNm := range cmdNms {
ge.ExecCmdName(cmdNm, sel, clearBuf)
}
}
// ExecCmdsFileNode executes a sequence of commands on file node, sel = select tab, clearBuf = clear buffer
func (ge *CodeView) ExecCmdsFileNode(fn *filetree.Node, cmdNms code.CmdNames, sel bool, clearBuf bool) {
for _, cmdNm := range cmdNms {
ge.ExecCmdNameFileNode(fn, cmdNm, sel, clearBuf)
}
}
// Build runs the BuildCmds set for this project
func (ge *CodeView) Build() { //types:add
if len(ge.Settings.BuildCmds) == 0 {
core.MessageDialog(ge, "You need to set the BuildCmds in the Project Settings", "No BuildCmds Set")
return
}
ge.SaveAllCheck(true, func() { // true = cancel option
ge.ExecCmds(ge.Settings.BuildCmds, true, true)
})
}
// Run runs the RunCmds set for this project
func (ge *CodeView) Run() { //types:add
if len(ge.Settings.RunCmds) == 0 {
core.MessageDialog(ge, "You need to set the RunCmds in the Project Settings", "No RunCmds Set")
return
}
if ge.Settings.RunCmds[0] == "Run Project" && !ge.Settings.RunExecIsExec() {
views.CallFunc(ge, ge.ChooseRunExec)
return
}
ge.ExecCmds(ge.Settings.RunCmds, true, true)
}
// Commit commits the current changes using relevant VCS tool.
// Checks for VCS setting and for unsaved files.
func (ge *CodeView) Commit() { //types:add
vc := ge.VersionControl()
if vc == "" {
core.MessageDialog(ge, "No version control system detected in file system, or defined in project prefs -- define in project prefs if viewing a sub-directory within a larger repository", "No Version Control System Found")
return
}
ge.SaveAllCheck(true, func() { // true = cancel option
ge.CommitNoChecks()
})
}
// CommitNoChecks does the commit without any further checks for VCS, and unsaved files
func (ge *CodeView) CommitNoChecks() {
vc := ge.VersionControl()
cmds := code.AvailableCommands.FilterCmdNames(ge.ActiveLang, vc)
cmdnm := ""
for _, ct := range cmds {
if len(ct) < 2 {
continue
}
if !filetree.IsVersionControlSystem(ct[0]) {
continue
}
for _, cm := range ct {
if strings.Contains(cm, "Commit") {
cmdnm = code.CommandName(ct[0], cm)
break
}
}
}
if cmdnm == "" {
core.MessageDialog(ge, "Could not find Commit command in list of avail commands -- this is usually a programmer error -- check settings settings etc", "No Commit command found")
return
}
ge.SetArgVarVals() // need to set before setting prompt string below..
d := core.NewBody().AddTitle("Commit message").
AddText("Please enter your commit message here. Remember that this is essential documentation. Author information comes from the Cogent Core User Settings.")
tf := core.NewTextField(d)
tf.Style(func(s *styles.Style) {
s.Min.X.Ch(80)
})
d.AddBottomBar(func(parent core.Widget) {
d.AddCancel(parent)
d.AddOK(parent).SetText("Commit").OnClick(func(e events.Event) {
val := tf.Text()
ge.ArgVals["{PromptString1}"] = val
code.CmdNoUserPrompt = true // don't re-prompt!
ge.ExecCmdName(code.CmdName(cmdnm), true, true) // must be wait
ge.SaveProjectIfExists(true) // saveall
ge.UpdateFiles()
})
})
d.NewDialog(ge).Run()
}