-
Notifications
You must be signed in to change notification settings - Fork 16
/
format.go
207 lines (184 loc) · 7.46 KB
/
format.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
package main
import (
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/xyproto/mode"
"github.com/xyproto/vt100"
)
// Map from formatting command to a list of file extensions
var format = map[*exec.Cmd][]string{
exec.Command("goimports", "-w", "--"): {".go"},
exec.Command("clang-format", "-fallback-style=WebKit", "-style=file", "-i", "--"): {".cpp", ".cc", ".cxx", ".h", ".hpp", ".c++", ".h++", ".c"},
exec.Command("zig", "fmt"): {".zig"},
exec.Command("v", "fmt"): {".v"},
exec.Command("rustfmt"): {".rs"},
exec.Command("brittany", "--write-mode=inplace"): {".hs"},
exec.Command("autopep8", "-i", "--max-line-length", "120"): {".py"},
exec.Command("ocamlformat"): {".ml"},
exec.Command("crystal", "tool", "format"): {".cr"},
exec.Command("ktlint", "-F"): {".kt", ".kts"},
exec.Command("google-java-format", "-a", "-i"): {".java"},
exec.Command("scalafmt"): {".scala"},
exec.Command("astyle", "--mode=cs"): {".cs"},
exec.Command("prettier", "--tab-width", "4", "-w"): {".js", ".ts"},
exec.Command("prettier", "--tab-width", "2", "-w"): {".css"},
exec.Command("shfmt", "-s", "-w", "-i", "2", "-bn", "-ci", "-sr", "-kp"): {".sh", ".bash", "PKGBUILD"},
exec.Command("lua-format", "-i", "--no-keep-simple-function-one-line", "--column-limit=120", "--indent-width=2", "--no-use-tab"): {".lua"},
exec.Command("tidy", "-w", "80", "-q", "-i", "-utf8", "--show-errors", "0", "--show-warnings", "no", "--tidy-mark", "no", "-xml", "-m"): {".xml"},
exec.Command("tidy", "-w", "120", "-q", "-i", "-utf8", "--show-errors", "0", "--show-warnings", "no", "--tidy-mark", "no", "--hide-endtags", "yes", "--force-output", "yes", "-ashtml", "-omit", "no", "-xml", "no", "-m", "-c"): {".html", ".htm"},
exec.Command("/usr/bin/vendor_perl/perltidy", "-se", "-b", "-i=2", "-ole=unix", "-bt=2", "-pt=2", "-sbt=2", "-ce"): {".pl"},
}
// Using exec.Cmd instead of *exec.Cmd is on purpose, to get a new cmd.stdout and cmd.stdin every time.
func (e *Editor) formatWithUtility(c *vt100.Canvas, tty *vt100.TTY, status *StatusBar, cmd exec.Cmd, extOrBaseFilename string) error {
if which(cmd.Path) == "" { // Does the formatting tool even exist?
return errors.New(cmd.Path + " is missing")
}
tempFirstName := "o"
if e.mode == mode.Kotlin {
tempFirstName = "O"
}
if f, err := os.CreateTemp(tempDir, tempFirstName+".*"+extOrBaseFilename); err == nil {
// no error, everything is fine
tempFilename := f.Name()
defer os.Remove(tempFilename)
defer f.Close()
// TODO: Implement e.SaveAs
oldFilename := e.filename
e.filename = tempFilename
err := e.Save(c, tty)
e.filename = oldFilename
if err == nil {
// Add the filename of the temporary file to the command
cmd.Args = append(cmd.Args, tempFilename)
// Save the command in a temporary file
saveCommand(&cmd)
// Format the temporary file
output, err := cmd.CombinedOutput()
// Ignore errors if the command is "tidy" and tidy exists
ignoreErrors := strings.HasSuffix(cmd.Path, "tidy") && which("tidy") != ""
// Perl may place executables in /usr/bin/vendor_perl
if e.mode == mode.Perl {
// Use perltidy from the PATH if /usr/bin/vendor_perl/perltidy does not exists
if cmd.Path == "/usr/bin/vendor_perl/perltidy" && !exists("/usr/bin/vendor_perl/perltidy") {
perltidyPath := which("perltidy")
if perltidyPath == "" {
return errors.New("perltidy is missing")
}
cmd.Path = perltidyPath
}
}
if err != nil && !ignoreErrors {
// Only grab the first error message
errorMessage := strings.TrimSpace(string(output))
if errorMessage == "" && err != nil {
errorMessage = err.Error()
}
if strings.Count(errorMessage, "\n") > 0 {
errorMessage = strings.TrimSpace(strings.SplitN(errorMessage, "\n", 2)[0])
}
var retErr error
if errorMessage == "" {
retErr = errors.New("failed to format code")
} else {
retErr = errors.New("failed to format code: " + errorMessage)
}
if strings.Count(errorMessage, ":") >= 3 {
fields := strings.Split(errorMessage, ":")
// Go To Y:X, if available
var foundY int
if y, err := strconv.Atoi(fields[1]); err == nil { // no error
foundY = y - 1
e.redraw, _ = e.GoTo(LineIndex(foundY), c, status)
foundX := -1
if x, err := strconv.Atoi(fields[2]); err == nil { // no error
foundX = x - 1
}
if foundX != -1 {
tabs := strings.Count(e.Line(LineIndex(foundY)), "\t")
e.pos.sx = foundX + (tabs * (e.indentation.PerTab - 1))
e.Center(c)
}
}
e.redrawCursor = true
}
return retErr
}
if _, err := e.Load(c, tty, FilenameOrData{tempFilename, []byte{}, 0}); err != nil {
return err
}
// Mark the data as changed, despite just having loaded a file
e.changed = true
e.redrawCursor = true
}
// Try to close the file. f.Close() checks if f is nil before closing.
e.redraw = true
e.redrawCursor = true
}
return nil
}
func (e *Editor) formatCode(c *vt100.Canvas, tty *vt100.TTY, status *StatusBar, jsonFormatToggle *bool) {
// Format JSON
if e.mode == mode.JSON {
var v interface{}
err := json.Unmarshal([]byte(e.String()), &v)
if err != nil {
status.ClearAll(c)
status.ShowErrorAfterRedraw(err)
return
}
// Format the JSON bytes, first without indentation and then
// with indentation.
var indentedJSON []byte
if *jsonFormatToggle {
indentedJSON, err = json.Marshal(v)
*jsonFormatToggle = false
} else {
indentationString := strings.Repeat(" ", e.indentation.PerTab)
indentedJSON, err = json.MarshalIndent(v, "", indentationString)
*jsonFormatToggle = true
}
if err != nil {
status.ClearAll(c)
status.ShowErrorAfterRedraw(err)
return
}
e.LoadBytes(indentedJSON)
e.redraw = true
return
}
baseFilename := filepath.Base(e.filename)
if baseFilename == "fstab" {
cmd := exec.Command("fstabfmt", "-i")
if which(cmd.Path) == "" { // Does the formatting tool even exist?
status.ClearAll(c)
status.SetErrorMessage(cmd.Path + " is missing")
status.Show(c, e)
return
}
if err := e.formatWithUtility(c, tty, status, *cmd, baseFilename); err != nil {
status.ClearAll(c)
status.SetMessage(err.Error())
status.Show(c, e)
}
return
}
// Not in git mode, format Go or C++ code with goimports or clang-format
OUT:
for cmd, extensions := range format {
for _, ext := range extensions {
if strings.HasSuffix(e.filename, ext) {
if err := e.formatWithUtility(c, tty, status, *cmd, ext); err != nil {
status.ClearAll(c)
status.SetMessage(err.Error())
status.Show(c, e)
}
break OUT
}
}
}
}