2019-04-17 13:37:20 -06:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-04-17 13:37:20 -06:00
|
|
|
)
|
|
|
|
|
2019-05-03 22:04:18 -06:00
|
|
|
func getSourceFile(ctx context.Context, v source.View, uri span.URI) (source.File, *protocol.ColumnMapper, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
f, err := v.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
data, _, err := f.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
2019-07-11 19:05:55 -06:00
|
|
|
tok, err := f.GetToken(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
m := protocol.NewColumnMapper(f.URI(), f.URI().Filename(), f.FileSet(), tok, data)
|
2019-05-17 11:45:50 -06:00
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
return f, m, nil
|
|
|
|
}
|
2019-05-03 22:04:18 -06:00
|
|
|
|
|
|
|
func getGoFile(ctx context.Context, v source.View, uri span.URI) (source.GoFile, *protocol.ColumnMapper, error) {
|
|
|
|
f, m, err := getSourceFile(ctx, v, uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
gof, ok := f.(source.GoFile)
|
|
|
|
if !ok {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, nil, errors.Errorf("not a Go file %v", f.URI())
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
|
|
|
return gof, m, nil
|
|
|
|
}
|