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
|
|
|
|
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
|
|
|
pgh := snapshot.View().Session().Cache().ParseGoHandle(ctx, 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
|
|
|
|
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{}
|
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
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
return computeTextEdits(ctx, snapshot.View(), pgh.File(), m, 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
|
|
|
|
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
|
|
|
pgh := snapshot.View().Session().Cache().ParseGoHandle(ctx, 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 {
|
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.
|
|
|
|
func computeImportEdits(ctx context.Context, view View, ph ParseGoHandle, options *imports.Options) (allFixEdits []protocol.TextEdit, editsPerFix []*ImportFix, err error) {
|
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
|
|
|
filename := ph.File().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.
|
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
|
|
|
origData, err := ph.File().Read()
|
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) {
|
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
|
|
|
origData, err := ph.File().Read()
|
2019-11-12 14:58:00 -07:00
|
|
|
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{
|
2020-07-06 13:18:50 -06:00
|
|
|
LocalPrefix: 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-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) {
|
2020-05-24 15:13:47 -06:00
|
|
|
// trim the original data to match fixedData
|
|
|
|
left := importPrefix(origData)
|
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 {
|
|
|
|
left = string(origData)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
fixedData, err := imports.ApplyFixes(fixes, "", origData, 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
|
|
|
}
|
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
|
|
|
uri := ph.File().URI()
|
2020-05-19 13:51:07 -06:00
|
|
|
edits := view.Options().ComputeEdits(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)
|
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 ""
|
|
|
|
}
|
|
|
|
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-19 13:51:07 -06:00
|
|
|
if importEnd == 0 {
|
|
|
|
importEnd = pkgEnd
|
2020-07-14 12:15:32 -06:00
|
|
|
if importEnd > len(src) {
|
|
|
|
importEnd-- // pkgEnd is off by 1 because Pos is 1-based
|
|
|
|
}
|
2019-11-14 11:10:28 -07:00
|
|
|
}
|
2020-05-09 06:54:23 -06:00
|
|
|
for _, c := range f.Comments {
|
2020-05-19 13:51:07 -06:00
|
|
|
if int(c.End()) > importEnd {
|
|
|
|
importEnd = int(c.End())
|
2019-11-14 11:10:28 -07:00
|
|
|
}
|
2019-11-05 15:33:19 -07:00
|
|
|
}
|
2020-05-19 13:51:07 -06:00
|
|
|
return string(src[:importEnd])
|
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-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
|
|
|
|
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-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
|
|
|
}
|
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
|
|
|
edits := view.Options().ComputeEdits(fh.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
|
|
|
|
}
|