-
Notifications
You must be signed in to change notification settings - Fork 207
/
fmt.go
81 lines (72 loc) · 1.88 KB
/
fmt.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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"go/format"
"net/http"
"path"
"golang.org/x/mod/modfile"
"golang.org/x/tools/imports"
)
type fmtResponse struct {
Body string
Error string
}
func (s *server) handleFmt(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
// This is likely a pre-flight CORS request.
return
}
w.Header().Set("Content-Type", "application/json")
fs, err := splitFiles([]byte(r.FormValue("body")))
if err != nil {
json.NewEncoder(w).Encode(fmtResponse{Error: err.Error()})
return
}
fixImports := r.FormValue("imports") != ""
for _, f := range fs.files {
switch {
case path.Ext(f) == ".go":
var out []byte
var err error
in := fs.Data(f)
if fixImports {
// TODO: pass options to imports.Process so it
// can find symbols in sibling files.
out, err = imports.Process(f, in, nil)
} else {
out, err = format.Source(in)
}
if err != nil {
errMsg := err.Error()
if !fixImports {
// Unlike imports.Process, format.Source does not prefix
// the error with the file path. So, do it ourselves here.
errMsg = fmt.Sprintf("%v:%v", f, errMsg)
}
json.NewEncoder(w).Encode(fmtResponse{Error: errMsg})
return
}
fs.AddFile(f, out)
case path.Base(f) == "go.mod":
out, err := formatGoMod(f, fs.Data(f))
if err != nil {
json.NewEncoder(w).Encode(fmtResponse{Error: err.Error()})
return
}
fs.AddFile(f, out)
}
}
s.writeJSONResponse(w, fmtResponse{Body: string(fs.Format())}, http.StatusOK)
}
func formatGoMod(file string, data []byte) ([]byte, error) {
f, err := modfile.Parse(file, data, nil)
if err != nil {
return nil, err
}
return f.Format()
}