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"
|
2019-11-05 15:33:19 -07:00
|
|
|
"go/ast"
|
2018-11-07 10:58:55 -07:00
|
|
|
"go/format"
|
2019-11-05 15:33:19 -07:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2018-11-14 18:42:30 -07:00
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
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-08-16 15:05:40 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
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-12-17 16:57:54 -07:00
|
|
|
func Format(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.TextEdit, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.Format")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-07-11 19:05:55 -06:00
|
|
|
|
2020-01-12 16:14:55 -07:00
|
|
|
pgh := snapshot.View().Session().Cache().ParseGoHandle(fh, ParseFull)
|
2020-02-10 21:10:59 -07:00
|
|
|
file, _, m, parseErrors, err := pgh.Parse(ctx)
|
2019-09-17 09:19:11 -06:00
|
|
|
if err != nil {
|
2019-09-06 21:58:07 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-12 16:14:55 -07:00
|
|
|
// Even if this file has parse errors, it might still be possible to format it.
|
|
|
|
// Using format.Node on an AST with errors may result in code being modified.
|
|
|
|
// Attempt to format the source of this file instead.
|
|
|
|
if parseErrors != nil {
|
2020-03-27 10:52:40 -06:00
|
|
|
formatted, err := formatSource(ctx, fh)
|
2019-07-26 16:17:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
return computeTextEdits(ctx, snapshot.View(), pgh.File(), m, string(formatted))
|
2018-11-14 18:42:30 -07:00
|
|
|
}
|
2019-06-28 14:21:07 -06:00
|
|
|
|
2019-11-20 14:38:43 -07:00
|
|
|
fset := snapshot.View().Session().Cache().FileSet()
|
2019-06-28 14:21:07 -06:00
|
|
|
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.
|
2019-09-05 14:58:50 -06:00
|
|
|
if err := format.Node(buf, fset, file); err != nil {
|
2018-11-07 10:58:55 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
return computeTextEdits(ctx, snapshot.View(), pgh.File(), m, buf.String())
|
2018-12-14 15:00:24 -07:00
|
|
|
}
|
|
|
|
|
2020-03-27 10:52:40 -06:00
|
|
|
func formatSource(ctx context.Context, fh FileHandle) ([]byte, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.formatSource")
|
2019-07-26 16:17:04 -06:00
|
|
|
defer done()
|
2019-09-27 11:17:59 -06:00
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
data, _, err := fh.Read(ctx)
|
2019-07-26 16:17:04 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return format.Source(data)
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:33:19 -07:00
|
|
|
type ImportFix struct {
|
|
|
|
Fix *imports.ImportFix
|
|
|
|
Edits []protocol.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.
|
2019-12-17 16:57:54 -07:00
|
|
|
func AllImportsFixes(ctx context.Context, snapshot Snapshot, fh FileHandle) (allFixEdits []protocol.TextEdit, editsPerFix []*ImportFix, err error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.AllImportsFixes")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-09-09 17:26:26 -06:00
|
|
|
|
2020-03-10 15:21:51 -06:00
|
|
|
pgh := snapshot.View().Session().Cache().ParseGoHandle(fh, ParseFull)
|
2020-02-26 15:07:02 -07:00
|
|
|
if err := snapshot.View().RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
|
2019-11-29 23:17:57 -07:00
|
|
|
allFixEdits, editsPerFix, err = computeImportEdits(ctx, snapshot.View(), pgh, opts)
|
2019-07-12 16:54:06 -06:00
|
|
|
return err
|
2020-02-26 15:07:02 -07:00
|
|
|
}); err != nil {
|
2019-12-05 17:49:14 -07:00
|
|
|
return nil, nil, errors.Errorf("computing fix edits: %v", err)
|
2019-09-09 22:36:39 -06:00
|
|
|
}
|
2019-11-05 15:33:19 -07:00
|
|
|
return allFixEdits, editsPerFix, nil
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:33:19 -07:00
|
|
|
// computeImportEdits computes a set of edits that perform one or all of the
|
|
|
|
// necessary import fixes.
|
|
|
|
func computeImportEdits(ctx context.Context, view View, ph ParseGoHandle, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*ImportFix, err error) {
|
|
|
|
filename := ph.File().Identity().URI.Filename()
|
2019-09-05 14:58:50 -06:00
|
|
|
|
2019-11-05 15:33:19 -07:00
|
|
|
// Build up basic information about the original file.
|
|
|
|
origData, _, err := ph.File().Read(ctx)
|
2019-09-09 17:26:26 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
_, _, origMapper, _, err := ph.Parse(ctx)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
2019-11-05 15:33:19 -07:00
|
|
|
|
|
|
|
allFixes, err := imports.FixImports(filename, origData, options)
|
2019-07-30 12:00:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:54:23 -06:00
|
|
|
allFixEdits, err = computeFixEdits(view, ph, options, origData, origMapper, allFixes)
|
2019-11-05 15:33:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply all of the import fixes to the file.
|
|
|
|
// Add the edits for each fix to the result.
|
|
|
|
for _, fix := range allFixes {
|
2020-05-09 06:54:23 -06:00
|
|
|
edits, err := computeFixEdits(view, ph, options, origData, origMapper, []*imports.ImportFix{fix})
|
2019-11-05 15:33:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
editsPerFix = append(editsPerFix, &ImportFix{
|
|
|
|
Fix: fix,
|
|
|
|
Edits: edits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return allFixEdits, editsPerFix, nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 14:58:00 -07:00
|
|
|
func computeOneImportFixEdits(ctx context.Context, view View, ph ParseGoHandle, fix *imports.ImportFix) ([]protocol.TextEdit, error) {
|
|
|
|
origData, _, err := ph.File().Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
_, _, origMapper, _, err := ph.Parse(ctx) // ph.Parse returns values never used
|
2019-11-12 14:58:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
options := &imports.Options{
|
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
return computeFixEdits(view, ph, options, origData, origMapper, []*imports.ImportFix{fix})
|
2019-11-12 14:58:00 -07:00
|
|
|
}
|
|
|
|
|
2020-05-09 06:54:23 -06:00
|
|
|
func computeFixEdits(view View, ph ParseGoHandle, options *imports.Options, origData []byte, origMapper *protocol.ColumnMapper, fixes []*imports.ImportFix) ([]protocol.TextEdit, error) {
|
2019-11-12 14:58:00 -07:00
|
|
|
filename := ph.File().Identity().URI.Filename()
|
|
|
|
// Apply the fixes and re-parse the file so that we can locate the
|
|
|
|
// new imports.
|
2019-12-05 17:49:14 -07:00
|
|
|
fixedData, err := imports.ApplyFixes(fixes, filename, origData, options, parser.ImportsOnly)
|
2019-11-12 14:58:00 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
if fixedData == nil || fixedData[len(fixedData)-1] != '\n' {
|
|
|
|
fixedData = append(fixedData, '\n') // ApplyFixes may miss the newline, go figure.
|
2019-11-12 14:58:00 -07:00
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
// trim the original data to match fixedData
|
|
|
|
left := importPrefix(filename, origData)
|
|
|
|
if len(left) > 0 && left[len(left)-1] != '\n' {
|
|
|
|
left += "\n"
|
2019-11-12 14:58:00 -07:00
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
f := view.Options().ComputeEdits
|
|
|
|
edits := f(ph.File().Identity().URI, left, string(fixedData))
|
2019-11-12 14:58:00 -07:00
|
|
|
return ToProtocolEdits(origMapper, edits)
|
|
|
|
}
|
|
|
|
|
2020-05-09 06:54:23 -06:00
|
|
|
// return the prefix of the src through the last imports, or if there are
|
|
|
|
// no imports, through the package statement (and a subsequent comment group)
|
|
|
|
func importPrefix(filename string, src []byte) string {
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
// do as little parsing as possible
|
|
|
|
f, err := parser.ParseFile(fset, filename, src, parser.ImportsOnly|parser.ParseComments)
|
|
|
|
if err != nil { // This can happen if 'package' is misspelled
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
myStart := fset.File(f.Pos()).Base() // 1, but the generality costs little
|
|
|
|
pkgEnd := int(f.Name.NamePos) + len(f.Name.Name)
|
|
|
|
var importEnd int
|
|
|
|
for _, d := range f.Decls {
|
|
|
|
if x, ok := d.(*ast.GenDecl); ok && x.Tok == token.IMPORT {
|
|
|
|
e := int(d.End()) - myStart
|
|
|
|
if e > importEnd {
|
|
|
|
importEnd = e
|
2019-11-05 15:33:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
if importEnd > 0 {
|
|
|
|
return string(src[:importEnd])
|
2019-11-14 11:10:28 -07:00
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
// If there are no imports there may still be comments after the package
|
|
|
|
// name. There could be one group before the package statement, and one after.
|
|
|
|
for _, c := range f.Comments {
|
|
|
|
if int(c.End()) > pkgEnd {
|
|
|
|
pkgEnd = int(c.End())
|
2019-11-14 11:10:28 -07:00
|
|
|
}
|
2019-11-05 15:33:19 -07:00
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
return string(src[:pkgEnd])
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
|
|
|
|
2019-10-01 16:06:10 -06:00
|
|
|
func computeTextEdits(ctx context.Context, view View, fh FileHandle, m *protocol.ColumnMapper, formatted string) ([]protocol.TextEdit, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.computeTextEdits")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-09-05 14:58:50 -06:00
|
|
|
|
2019-09-09 22:36:39 -06:00
|
|
|
data, _, err := fh.Read(ctx)
|
2019-06-03 23:04:18 -06:00
|
|
|
if err != nil {
|
2019-09-05 14:58:50 -06:00
|
|
|
return nil, err
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
2019-10-01 16:06:10 -06:00
|
|
|
edits := view.Options().ComputeEdits(fh.Identity().URI, string(data), formatted)
|
2019-09-05 14:58:50 -06:00
|
|
|
return ToProtocolEdits(m, edits)
|
2018-11-07 10:58:55 -07:00
|
|
|
}
|
2019-08-16 15:05:40 -06:00
|
|
|
|
|
|
|
func ToProtocolEdits(m *protocol.ColumnMapper, edits []diff.TextEdit) ([]protocol.TextEdit, error) {
|
|
|
|
if edits == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
result := make([]protocol.TextEdit, len(edits))
|
|
|
|
for i, edit := range edits {
|
|
|
|
rng, err := m.Range(edit.Span)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[i] = protocol.TextEdit{
|
|
|
|
Range: rng,
|
|
|
|
NewText: edit.NewText,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2019-09-05 14:58:50 -06:00
|
|
|
|
|
|
|
func FromProtocolEdits(m *protocol.ColumnMapper, edits []protocol.TextEdit) ([]diff.TextEdit, error) {
|
|
|
|
if edits == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
result := make([]diff.TextEdit, len(edits))
|
|
|
|
for i, edit := range edits {
|
|
|
|
spn, err := m.RangeSpan(edit.Range)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[i] = diff.TextEdit{
|
|
|
|
Span: spn,
|
|
|
|
NewText: edit.NewText,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|