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"
|
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-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/log"
|
2019-06-21 14:28:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/trace"
|
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-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-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-07-03 13:23:05 -06:00
|
|
|
|
2019-07-14 11:59:24 -06:00
|
|
|
if resolver, ok := view.ProcessEnv(ctx).GetResolver().(*imports.ModuleResolver); ok && resolver.Initialized {
|
2019-07-03 13:23:05 -06:00
|
|
|
// TODO(suzmue): only reset this state when necessary (eg when the go.mod files of this
|
|
|
|
// module or modules with replace directive changes).
|
|
|
|
resolver.Initialized = false
|
|
|
|
resolver.Main = nil
|
|
|
|
resolver.ModsByModPath = nil
|
|
|
|
resolver.ModsByDir = nil
|
2019-07-16 15:37:05 -06:00
|
|
|
resolver.ModCachePkgs = nil
|
2019-07-03 13:23:05 -06:00
|
|
|
}
|
2019-06-28 14:21:07 -06:00
|
|
|
options := &imports.Options{
|
2019-07-14 11:59:24 -06:00
|
|
|
Env: view.ProcessEnv(ctx),
|
2019-06-28 14:21:07 -06:00
|
|
|
// 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-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
|
|
|
}
|