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"
|
|
|
|
"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"
|
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-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
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-07-11 19:05:55 -06:00
|
|
|
|
|
|
|
file, err := f.GetAST(ctx, ParseFull)
|
2019-05-20 13:23:02 -06:00
|
|
|
if file == nil {
|
2019-07-11 19:05:55 -06:00
|
|
|
return nil, err
|
2019-05-20 13:23:02 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
pkg, err := f.GetPackage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-02 17:54:15 -06:00
|
|
|
if hasListErrors(pkg.GetErrors()) || hasParseErrors(pkg, f.URI()) {
|
2019-07-26 16:17:04 -06:00
|
|
|
// Even if this package has list or parse errors, this file may not
|
|
|
|
// have any parse errors and can still be formatted. Using format.Node
|
|
|
|
// on an ast with errors may result in code being added or removed.
|
|
|
|
// Attempt to format the source of this file instead.
|
|
|
|
formatted, err := formatSource(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return computeTextEdits(ctx, f, string(formatted)), nil
|
2019-04-05 16:05:31 -06:00
|
|
|
}
|
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 {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("no exact AST node matching the specified range")
|
2018-11-14 18:42:30 -07:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-26 16:17:04 -06:00
|
|
|
func formatSource(ctx context.Context, file File) ([]byte, error) {
|
|
|
|
ctx, done := trace.StartSpan(ctx, "source.formatSource")
|
|
|
|
defer done()
|
|
|
|
data, _, err := file.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return format.Source(data)
|
|
|
|
}
|
|
|
|
|
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-07-09 15:52:23 -06:00
|
|
|
pkg, err := f.GetPackage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-28 17:59:25 -06:00
|
|
|
}
|
2019-06-28 14:37:54 -06:00
|
|
|
if hasListErrors(pkg.GetErrors()) {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("%s has list errors, not running goimports", f.URI())
|
2019-05-20 13:23:02 -06:00
|
|
|
}
|
2019-07-03 13:23:05 -06:00
|
|
|
|
2019-06-28 14:21:07 -06:00
|
|
|
options := &imports.Options{
|
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
}
|
2019-07-12 16:54:06 -06:00
|
|
|
var formatted []byte
|
|
|
|
importFn := func(opts *imports.Options) error {
|
|
|
|
formatted, err = imports.Process(f.URI().Filename(), data, opts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = view.RunProcessEnvFunc(ctx, importFn, 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-07-30 12:00:02 -06:00
|
|
|
type ImportFix struct {
|
|
|
|
Fix *imports.ImportFix
|
|
|
|
Edits []TextEdit
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllImportsFixes formats f for each possible fix to the imports.
|
|
|
|
// In addition to returning the result of applying all edits,
|
|
|
|
// it returns a list of fixes that could be applied to the file, with the
|
|
|
|
// corresponding TextEdits that would be needed to apply that fix.
|
|
|
|
func AllImportsFixes(ctx context.Context, view View, f GoFile, rng span.Range) (edits []TextEdit, editsPerFix []*ImportFix, err error) {
|
|
|
|
ctx, done := trace.StartSpan(ctx, "source.AllImportsFixes")
|
|
|
|
defer done()
|
|
|
|
data, _, err := f.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
pkg, err := f.GetPackage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
|
|
|
if hasListErrors(pkg.GetErrors()) {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, nil, errors.Errorf("%s has list errors, not running goimports", f.URI())
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
|
|
|
options := &imports.Options{
|
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
}
|
|
|
|
importFn := func(opts *imports.Options) error {
|
|
|
|
fixes, err := imports.FixImports(f.URI().Filename(), data, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Apply all of the import fixes to the file.
|
|
|
|
formatted, err := imports.ApplyFixes(fixes, f.URI().Filename(), data, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
edits = computeTextEdits(ctx, f, string(formatted))
|
|
|
|
// Add the edits for each fix to the result.
|
|
|
|
editsPerFix = make([]*ImportFix, len(fixes))
|
|
|
|
for i, fix := range fixes {
|
|
|
|
formatted, err := imports.ApplyFixes([]*imports.ImportFix{fix}, f.URI().Filename(), data, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
editsPerFix[i] = &ImportFix{
|
|
|
|
Fix: fix,
|
|
|
|
Edits: computeTextEdits(ctx, f, string(formatted)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = view.RunProcessEnvFunc(ctx, importFn, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return edits, editsPerFix, nil
|
|
|
|
}
|
|
|
|
|
2019-08-14 15:25:47 -06:00
|
|
|
// AllImportsFixes formats f for each possible fix to the imports.
|
|
|
|
// In addition to returning the result of applying all edits,
|
|
|
|
// it returns a list of fixes that could be applied to the file, with the
|
|
|
|
// corresponding TextEdits that would be needed to apply that fix.
|
|
|
|
func CandidateImports(ctx context.Context, view View, filename string) (pkgs []imports.ImportFix, err error) {
|
|
|
|
ctx, done := trace.StartSpan(ctx, "source.CandidateImports")
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
options := &imports.Options{
|
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
}
|
|
|
|
importFn := func(opts *imports.Options) error {
|
|
|
|
pkgs, err = imports.GetAllCandidates(filename, opts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = view.RunProcessEnvFunc(ctx, importFn, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkgs, nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:54:15 -06:00
|
|
|
// hasParseErrors returns true if the given file has parse errors.
|
|
|
|
func hasParseErrors(pkg Package, uri span.URI) bool {
|
|
|
|
for _, err := range pkg.GetErrors() {
|
|
|
|
spn := packagesErrorSpan(err)
|
|
|
|
if spn.URI() == uri && err.Kind == packages.ParseError {
|
2019-06-28 14:37:54 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasListErrors(errors []packages.Error) bool {
|
|
|
|
for _, err := range errors {
|
|
|
|
if err.Kind == packages.ListError {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "Cannot compute text edits", 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
|
|
|
}
|