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"
|
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"
|
2018-12-14 15:00:24 -07:00
|
|
|
"golang.org/x/tools/imports"
|
2019-01-17 09:59:05 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
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-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)
|
|
|
|
if hasParseErrors(pkg.GetErrors()) {
|
|
|
|
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]
|
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.
|
2019-05-17 08:51:19 -06:00
|
|
|
fset := f.FileSet()
|
2018-11-07 10:58:55 -07:00
|
|
|
buf := &bytes.Buffer{}
|
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
|
|
|
}
|
|
|
|
|
2019-04-05 16:05:31 -06:00
|
|
|
func hasParseErrors(errors []packages.Error) bool {
|
|
|
|
for _, err := range errors {
|
|
|
|
if err.Kind == packages.ParseError {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-14 15:00:24 -07:00
|
|
|
// Imports formats a file using the goimports tool.
|
2019-05-03 22:04:18 -06:00
|
|
|
func Imports(ctx context.Context, f GoFile, rng span.Range) ([]TextEdit, error) {
|
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-05-20 13:23:02 -06:00
|
|
|
tok := f.GetToken(ctx)
|
|
|
|
if tok == nil {
|
|
|
|
return nil, fmt.Errorf("no token file for %s", f.URI())
|
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
formatted, err := imports.Process(tok.Name(), data, nil)
|
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-03-05 15:30:44 -07:00
|
|
|
func computeTextEdits(ctx context.Context, file File, formatted string) (edits []TextEdit) {
|
2019-06-03 23:04:18 -06:00
|
|
|
data, _, err := file.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
file.View().Session().Logger().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
|
|
|
}
|