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"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
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-05-20 12:56:01 -06:00
|
|
|
filename, err := f.URI().Filename()
|
2019-05-17 11:45:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
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-06-03 23:04:18 -06:00
|
|
|
m := protocol.NewColumnMapper(f.URI(), filename, f.FileSet(), f.GetToken(ctx), 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-05-29 12:37:11 -06:00
|
|
|
return nil, nil, fmt.Errorf("not a Go file %v", f.URI())
|
2019-05-03 22:04:18 -06:00
|
|
|
}
|
|
|
|
return gof, m, nil
|
|
|
|
}
|