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"
|
2020-07-16 18:00:10 -06:00
|
|
|
"fmt"
|
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"
|
2020-05-24 15:13:47 -06:00
|
|
|
"strings"
|
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"
|
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-07-21 13:15:06 -06:00
|
|
|
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
|
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.
|
2020-07-21 13:15:06 -06:00
|
|
|
if pgf.ParseErr != 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
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
return computeTextEdits(ctx, snapshot, pgf, string(formatted))
|
2018-11-14 18:42:30 -07:00
|
|
|
}
|
2019-06-28 14:21:07 -06:00
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
fset := snapshot.FileSet()
|
2019-06-28 14:21:07 -06:00
|
|
|
|
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.
|
2020-07-11 21:09:29 -06:00
|
|
|
buf := &bytes.Buffer{}
|
2020-07-21 13:15:06 -06:00
|
|
|
if err := format.Node(buf, fset, pgf.File); err != nil {
|
2018-11-07 10:58:55 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-11 21:09:29 -06:00
|
|
|
formatted := buf.String()
|
|
|
|
|
|
|
|
// Apply additional formatting, if any is supported. Currently, the only
|
|
|
|
// supported additional formatter is gofumpt.
|
|
|
|
if format := snapshot.View().Options().Hooks.GofumptFormat; snapshot.View().Options().Gofumpt && format != nil {
|
|
|
|
b, err := format(ctx, buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
formatted = string(b)
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
return computeTextEdits(ctx, snapshot, pgf, formatted)
|
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-06-11 22:24:37 -06:00
|
|
|
_, done := event.Start(ctx, "source.formatSource")
|
2019-07-26 16:17:04 -06:00
|
|
|
defer done()
|
2019-09-27 11:17:59 -06:00
|
|
|
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
data, err := fh.Read()
|
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-07-21 13:15:06 -06:00
|
|
|
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-02-26 15:07:02 -07:00
|
|
|
if err := snapshot.View().RunProcessEnvFunc(ctx, func(opts *imports.Options) error {
|
2020-07-28 15:00:10 -06:00
|
|
|
allFixEdits, editsPerFix, err = computeImportEdits(ctx, snapshot, pgf, opts)
|
2019-07-12 16:54:06 -06:00
|
|
|
return err
|
2020-02-26 15:07:02 -07:00
|
|
|
}); err != nil {
|
2020-07-16 18:00:10 -06:00
|
|
|
return nil, nil, fmt.Errorf("AllImportsFixes: %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.
|
2020-07-28 15:00:10 -06:00
|
|
|
func computeImportEdits(ctx context.Context, snapshot Snapshot, pgf *ParsedGoFile, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*ImportFix, err error) {
|
2020-07-21 13:15:06 -06:00
|
|
|
filename := pgf.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.
|
2020-07-21 13:15:06 -06:00
|
|
|
allFixes, err := imports.FixImports(filename, pgf.Src, options)
|
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
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
allFixEdits, err = computeFixEdits(snapshot, pgf, options, 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-07-28 15:00:10 -06:00
|
|
|
edits, err := computeFixEdits(snapshot, pgf, options, []*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
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
func computeOneImportFixEdits(ctx context.Context, snapshot Snapshot, pgf *ParsedGoFile, fix *imports.ImportFix) ([]protocol.TextEdit, error) {
|
2019-11-12 14:58:00 -07:00
|
|
|
options := &imports.Options{
|
2020-07-28 15:00:10 -06:00
|
|
|
LocalPrefix: snapshot.View().Options().LocalPrefix,
|
2019-11-12 14:58:00 -07:00
|
|
|
// Defaults.
|
|
|
|
AllErrors: true,
|
|
|
|
Comments: true,
|
|
|
|
Fragment: true,
|
|
|
|
FormatOnly: false,
|
|
|
|
TabIndent: true,
|
|
|
|
TabWidth: 8,
|
|
|
|
}
|
2020-07-28 15:00:10 -06:00
|
|
|
return computeFixEdits(snapshot, pgf, options, []*imports.ImportFix{fix})
|
2019-11-12 14:58:00 -07:00
|
|
|
}
|
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
func computeFixEdits(snapshot Snapshot, pgf *ParsedGoFile, options *imports.Options, fixes []*imports.ImportFix) ([]protocol.TextEdit, error) {
|
2020-05-24 15:13:47 -06:00
|
|
|
// trim the original data to match fixedData
|
2020-07-21 13:15:06 -06:00
|
|
|
left := importPrefix(pgf.Src)
|
2020-05-28 18:38:19 -06:00
|
|
|
extra := !strings.Contains(left, "\n") // one line may have more than imports
|
2020-05-24 15:13:47 -06:00
|
|
|
if extra {
|
2020-07-21 13:15:06 -06:00
|
|
|
left = string(pgf.Src)
|
2020-05-24 15:13:47 -06:00
|
|
|
}
|
|
|
|
if len(left) > 0 && left[len(left)-1] != '\n' {
|
|
|
|
left += "\n"
|
|
|
|
}
|
2019-11-12 14:58:00 -07:00
|
|
|
// Apply the fixes and re-parse the file so that we can locate the
|
|
|
|
// new imports.
|
2020-05-24 15:13:47 -06:00
|
|
|
flags := parser.ImportsOnly
|
|
|
|
if extra {
|
|
|
|
// used all of origData above, use all of it here too
|
|
|
|
flags = 0
|
|
|
|
}
|
2020-07-21 13:15:06 -06:00
|
|
|
fixedData, err := imports.ApplyFixes(fixes, "", pgf.Src, options, flags)
|
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-07-28 15:00:10 -06:00
|
|
|
edits := snapshot.View().Options().ComputeEdits(pgf.URI, left, string(fixedData))
|
2020-07-21 13:15:06 -06:00
|
|
|
return ToProtocolEdits(pgf.Mapper, edits)
|
2019-11-12 14:58:00 -07:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:14:33 -06:00
|
|
|
// importPrefix returns the prefix of the given file content through the final
|
|
|
|
// import statement. If there are no imports, the prefix is the package
|
|
|
|
// statement and any comment groups below it.
|
2020-05-19 13:51:07 -06:00
|
|
|
func importPrefix(src []byte) string {
|
2020-05-09 06:54:23 -06:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
// do as little parsing as possible
|
2020-05-19 13:51:07 -06:00
|
|
|
f, err := parser.ParseFile(fset, "", src, parser.ImportsOnly|parser.ParseComments)
|
2020-05-09 06:54:23 -06:00
|
|
|
if err != nil { // This can happen if 'package' is misspelled
|
|
|
|
return ""
|
|
|
|
}
|
2020-07-26 18:14:33 -06:00
|
|
|
tok := fset.File(f.Pos())
|
2020-05-09 06:54:23 -06:00
|
|
|
var importEnd int
|
|
|
|
for _, d := range f.Decls {
|
|
|
|
if x, ok := d.(*ast.GenDecl); ok && x.Tok == token.IMPORT {
|
2020-07-26 18:14:33 -06:00
|
|
|
if e := tok.Offset(d.End()); e > importEnd {
|
2020-05-09 06:54:23 -06:00
|
|
|
importEnd = e
|
2019-11-05 15:33:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 18:14:33 -06:00
|
|
|
|
|
|
|
maybeAdjustToLineEnd := func(pos token.Pos, isCommentNode bool) int {
|
|
|
|
offset := tok.Offset(pos)
|
|
|
|
|
|
|
|
// Don't go past the end of the file.
|
|
|
|
if offset > len(src) {
|
|
|
|
offset = len(src)
|
|
|
|
}
|
|
|
|
// The go/ast package does not account for different line endings, and
|
|
|
|
// specifically, in the text of a comment, it will strip out \r\n line
|
|
|
|
// endings in favor of \n. To account for these differences, we try to
|
|
|
|
// return a position on the next line whenever possible.
|
|
|
|
switch line := tok.Line(tok.Pos(offset)); {
|
|
|
|
case line < tok.LineCount():
|
|
|
|
nextLineOffset := tok.Offset(tok.LineStart(line + 1))
|
|
|
|
// If we found a position that is at the end of a line, move the
|
|
|
|
// offset to the start of the next line.
|
|
|
|
if offset+1 == nextLineOffset {
|
|
|
|
offset = nextLineOffset
|
|
|
|
}
|
|
|
|
case isCommentNode, offset+1 == tok.Size():
|
|
|
|
// If the last line of the file is a comment, or we are at the end
|
|
|
|
// of the file, the prefix is the entire file.
|
|
|
|
offset = len(src)
|
2020-07-14 12:15:32 -06:00
|
|
|
}
|
2020-07-26 18:14:33 -06:00
|
|
|
return offset
|
|
|
|
}
|
|
|
|
if importEnd == 0 {
|
|
|
|
pkgEnd := f.Name.End()
|
|
|
|
importEnd = maybeAdjustToLineEnd(pkgEnd, false)
|
2019-11-14 11:10:28 -07:00
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
for _, c := range f.Comments {
|
2020-07-26 18:14:33 -06:00
|
|
|
if end := tok.Offset(c.End()); end > importEnd {
|
|
|
|
importEnd = maybeAdjustToLineEnd(c.End(), true)
|
2019-11-14 11:10:28 -07:00
|
|
|
}
|
2019-11-05 15:33:19 -07:00
|
|
|
}
|
2020-07-26 18:14:33 -06:00
|
|
|
if importEnd > len(src) {
|
|
|
|
importEnd = len(src)
|
|
|
|
}
|
2020-05-19 13:51:07 -06:00
|
|
|
return string(src[:importEnd])
|
2019-07-30 12:00:02 -06:00
|
|
|
}
|
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
func computeTextEdits(ctx context.Context, snapshot Snapshot, pgf *ParsedGoFile, formatted string) ([]protocol.TextEdit, error) {
|
2020-06-11 22:24:37 -06:00
|
|
|
_, done := event.Start(ctx, "source.computeTextEdits")
|
2019-06-26 20:46:12 -06:00
|
|
|
defer done()
|
2019-09-05 14:58:50 -06:00
|
|
|
|
2020-07-28 15:00:10 -06:00
|
|
|
edits := snapshot.View().Options().ComputeEdits(pgf.URI, string(pgf.Src), formatted)
|
2020-07-21 13:15:06 -06:00
|
|
|
return ToProtocolEdits(pgf.Mapper, 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
|
|
|
|
}
|