2018-11-07 10:58:55 -07:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2018-12-05 15:00:36 -07:00
|
|
|
// Package source provides core features for use by Go editors and tools.
|
2018-11-07 10:58:55 -07:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2018-11-14 18:42:30 -07:00
|
|
|
"fmt"
|
2018-11-07 10:58:55 -07:00
|
|
|
"go/format"
|
2019-06-28 14:21:07 -06:00
|
|
|
"strings"
|
2018-11-14 18:42:30 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2019-04-05 16:05:31 -06:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-06-28 14:21:07 -06:00
|
|
|
"golang.org/x/tools/internal/imports"
|
2019-01-17 09:59:05 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
2019-06-21 14:28:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
2019-07-10 18:47:54 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/xlog"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-11-07 10:58:55 -07:00
|
|
|
)
|
|
|
|
|
2018-12-14 15:00:24 -07:00
|
|
|
// Format formats a file with a given range.
|
2019-05-03 22:04:18 -06:00
|
|
|
func Format(ctx context.Context, f GoFile, rng span.Range) ([]TextEdit, error) {
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.Format")
|
|
|
|
defer done()
|
2019-05-20 13:23:02 -06:00
|
|
|
file := f.GetAST(ctx)
|
|
|
|
if file == nil {
|
|
|
|
return nil, fmt.Errorf("no AST for %s", f.URI())
|
|
|
|
}
|
2019-04-05 16:05:31 -06:00
|
|
|
pkg := f.GetPackage(ctx)
|
2019-06-28 14:37:54 -06:00
|
|
|
if hasListErrors(pkg.GetErrors()) || hasParseErrors(pkg.GetErrors()) {
|
2019-04-05 16:05:31 -06:00
|
|
|
return nil, fmt.Errorf("%s has parse errors, not formatting", f.URI())
|
|
|
|
}
|
2019-05-20 13:23:02 -06:00
|
|
|
path, exact := astutil.PathEnclosingInterval(file, rng.Start, rng.End)
|
2018-11-14 18:42:30 -07:00
|
|
|
if !exact || len(path) == 0 {
|
|
|
|
return nil, fmt.Errorf("no exact AST node matching the specified range")
|
|
|
|
}
|
|
|
|
node := path[0]
|
2019-06-28 14:21:07 -06:00
|
|
|
|
|
|
|
fset := f.FileSet()
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
2018-11-07 10:58:55 -07:00
|
|
|
// format.Node changes slightly from one release to another, so the version
|
|
|
|
// of Go used to build the LSP server will determine how it formats code.
|
|
|
|
// This should be acceptable for all users, who likely be prompted to rebuild
|
|
|
|
// the LSP server on each Go release.
|
2018-12-05 15:00:36 -07:00
|
|
|
if err := format.Node(buf, fset, node); err != nil {
|
2018-11-07 10:58:55 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
return computeTextEdits(ctx, f, buf.String()), nil
|
2018-12-14 15:00:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Imports formats a file using the goimports tool.
|
2019-06-28 14:21:07 -06:00
|
|
|
func Imports(ctx context.Context, view View, f GoFile, rng span.Range) ([]TextEdit, error) {
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.Imports")
|
|
|
|
defer done()
|
2019-06-03 23:04:18 -06:00
|
|
|
data, _, err := f.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
pkg := f.GetPackage(ctx)
|
2019-06-28 17:59:25 -06:00
|
|
|
if pkg == nil || pkg.IsIllTyped() {
|
|
|
|
return nil, fmt.Errorf("no package for file %s", f.URI())
|
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
if hasListErrors(pkg.GetErrors()) {
|
|
|
|
return nil, fmt.Errorf("%s has list errors, not running goimports", f.URI())
|
2019-05-20 13:23:02 -06:00
|
|
|
}
|
2019-06-28 14:21:07 -06:00
|
|
|
options := &imports.Options{
|
|
|
|
Env: buildProcessEnv(ctx, view),
|
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
}
|
|
|
|
formatted, err := imports.Process(f.URI().Filename(), data, options)
|
2018-12-14 15:00:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
return computeTextEdits(ctx, f, string(formatted)), nil
|
2018-12-14 15:00:24 -07:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:37:54 -06:00
|
|
|
func hasParseErrors(errors []packages.Error) bool {
|
|
|
|
for _, err := range errors {
|
|
|
|
if err.Kind == packages.ParseError {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasListErrors(errors []packages.Error) bool {
|
|
|
|
for _, err := range errors {
|
|
|
|
if err.Kind == packages.ListError {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:21:07 -06:00
|
|
|
func buildProcessEnv(ctx context.Context, view View) *imports.ProcessEnv {
|
|
|
|
cfg := view.Config()
|
|
|
|
env := &imports.ProcessEnv{
|
|
|
|
WorkingDir: cfg.Dir,
|
|
|
|
Logf: func(format string, v ...interface{}) {
|
2019-07-10 18:47:54 -06:00
|
|
|
xlog.Infof(ctx, format, v...)
|
2019-06-28 14:21:07 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, kv := range cfg.Env {
|
|
|
|
split := strings.Split(kv, "=")
|
|
|
|
if len(split) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch split[0] {
|
|
|
|
case "GOPATH":
|
|
|
|
env.GOPATH = split[1]
|
|
|
|
case "GOROOT":
|
|
|
|
env.GOROOT = split[1]
|
|
|
|
case "GO111MODULE":
|
|
|
|
env.GO111MODULE = split[1]
|
|
|
|
case "GOPROXY":
|
|
|
|
env.GOROOT = split[1]
|
|
|
|
case "GOFLAGS":
|
|
|
|
env.GOFLAGS = split[1]
|
|
|
|
case "GOSUMDB":
|
|
|
|
env.GOSUMDB = split[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:30:44 -07:00
|
|
|
func computeTextEdits(ctx context.Context, file File, formatted string) (edits []TextEdit) {
|
2019-06-26 20:46:12 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.computeTextEdits")
|
|
|
|
defer done()
|
2019-06-03 23:04:18 -06:00
|
|
|
data, _, err := file.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
2019-07-10 18:47:54 -06:00
|
|
|
xlog.Errorf(ctx, "Cannot compute text edits: %v", err)
|
2019-05-17 10:15:22 -06:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
u := diff.SplitLines(string(data))
|
2019-04-08 07:22:58 -06:00
|
|
|
f := diff.SplitLines(formatted)
|
|
|
|
return DiffToEdits(file.URI(), diff.Operations(u, f))
|
2018-11-07 10:58:55 -07:00
|
|
|
}
|