1
0
mirror of https://github.com/golang/go synced 2024-10-01 07:18:32 -06:00
go/internal/lsp/util.go
Ian Cottrell e88138204b internal/span: change URI.Filename so it just returns the filename
We panic if the uri was not a valid file uri instead
They always are a valid file URI, and we would fail miserably to cope if they were
not anyway, and there are lots of places where we need to be able to get the filename
and don't want to cope with an error that cannot occur.
If we ever have not file uri's, you will have to check if it is a file before calling
.Filename, which seems reasonable anyway.

Change-Id: Ifb26a165bd43c2d310378314550b5749b09e2ebd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181017
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-10 21:39:43 +00:00

41 lines
1.0 KiB
Go

// 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"
)
func getSourceFile(ctx context.Context, v source.View, uri span.URI) (source.File, *protocol.ColumnMapper, error) {
f, err := v.GetFile(ctx, uri)
if err != nil {
return nil, nil, err
}
data, _, err := f.Handle(ctx).Read(ctx)
if err != nil {
return nil, nil, err
}
m := protocol.NewColumnMapper(f.URI(), f.URI().Filename(), f.FileSet(), f.GetToken(ctx), data)
return f, m, nil
}
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 {
return nil, nil, fmt.Errorf("not a Go file %v", f.URI())
}
return gof, m, nil
}