1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:34:51 -07:00

internal/lsp: handle escaping URIs for files in the module cache

This change makes sure to handle escaped characters, for instance, files
in the module cache contain "@" in the path. Also, return errors when a
package is not found for a file.

Updates golang/go#30027

Change-Id: I6c4f6d0f30dde55701308b89785f02b671e81cc6
Reviewed-on: https://go-review.googlesource.com/c/161077
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Yasuhiro MATSUMOTO <mattn.jp@gmail.com>
This commit is contained in:
Rebecca Stambler 2019-02-04 17:44:35 -05:00
parent 069601f371
commit 90c8b4f75b
6 changed files with 73 additions and 14 deletions

View File

@ -104,7 +104,7 @@ func (v *View) parse(uri source.URI) error {
} }
return err return err
} }
var foundPkg bool // true if we found a package for uri
for _, pkg := range pkgs { for _, pkg := range pkgs {
if len(pkg.Syntax) == 0 { if len(pkg.Syntax) == 0 {
return fmt.Errorf("no syntax trees for %s", pkg.PkgPath) return fmt.Errorf("no syntax trees for %s", pkg.PkgPath)
@ -118,7 +118,13 @@ func (v *View) parse(uri source.URI) error {
f.token = fToken f.token = fToken
f.ast = fAST f.ast = fAST
f.pkg = pkg f.pkg = pkg
if fURI == uri {
foundPkg = true
}
} }
} }
if !foundPkg {
return fmt.Errorf("no package found for %v", uri)
}
return nil return nil
} }

View File

@ -13,7 +13,10 @@ import (
) )
func (s *server) cacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI, content string) { func (s *server) cacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI, content string) {
sourceURI := fromProtocolURI(uri) sourceURI, err := fromProtocolURI(uri)
if err != nil {
return // handle error?
}
if err := s.setContent(ctx, sourceURI, []byte(content)); err != nil { if err := s.setContent(ctx, sourceURI, []byte(content)); err != nil {
return // handle error? return // handle error?
} }

View File

@ -10,7 +10,11 @@ import (
// formatRange formats a document with a given range. // formatRange formats a document with a given range.
func formatRange(ctx context.Context, v source.View, uri protocol.DocumentURI, rng *protocol.Range) ([]protocol.TextEdit, error) { func formatRange(ctx context.Context, v source.View, uri protocol.DocumentURI, rng *protocol.Range) ([]protocol.TextEdit, error) {
f, err := v.GetFile(ctx, fromProtocolURI(uri)) sourceURI, err := fromProtocolURI(uri)
if err != nil {
return nil, err
}
f, err := v.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -12,7 +12,11 @@ import (
) )
func organizeImports(ctx context.Context, v source.View, uri protocol.DocumentURI) ([]protocol.TextEdit, error) { func organizeImports(ctx context.Context, v source.View, uri protocol.DocumentURI) ([]protocol.TextEdit, error) {
f, err := v.GetFile(ctx, fromProtocolURI(uri)) sourceURI, err := fromProtocolURI(uri)
if err != nil {
return nil, err
}
f, err := v.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -7,6 +7,7 @@ package lsp
import ( import (
"context" "context"
"go/token" "go/token"
"net/url"
"golang.org/x/tools/internal/lsp/cache" "golang.org/x/tools/internal/lsp/cache"
"golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/protocol"
@ -15,15 +16,23 @@ import (
// fromProtocolURI converts a protocol.DocumentURI to a source.URI. // fromProtocolURI converts a protocol.DocumentURI to a source.URI.
// TODO(rstambler): Add logic here to support Windows. // TODO(rstambler): Add logic here to support Windows.
func fromProtocolURI(uri protocol.DocumentURI) source.URI { func fromProtocolURI(uri protocol.DocumentURI) (source.URI, error) {
return source.URI(uri) unescaped, err := url.PathUnescape(string(uri))
if err != nil {
return "", err
}
return source.URI(unescaped), nil
} }
// fromProtocolLocation converts from a protocol location to a source range. // fromProtocolLocation converts from a protocol location to a source range.
// It will return an error if the file of the location was not valid. // It will return an error if the file of the location was not valid.
// It uses fromProtocolRange to convert the start and end positions. // It uses fromProtocolRange to convert the start and end positions.
func fromProtocolLocation(ctx context.Context, v *cache.View, loc protocol.Location) (source.Range, error) { func fromProtocolLocation(ctx context.Context, v *cache.View, loc protocol.Location) (source.Range, error) {
f, err := v.GetFile(ctx, fromProtocolURI(loc.URI)) sourceURI, err := fromProtocolURI(loc.URI)
if err != nil {
return source.Range{}, err
}
f, err := v.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return source.Range{}, err return source.Range{}, err
} }

View File

@ -75,10 +75,19 @@ func (s *server) Initialize(ctx context.Context, params *protocol.InitializePara
s.snippetsSupported = params.Capabilities.TextDocument.Completion.CompletionItem.SnippetSupport s.snippetsSupported = params.Capabilities.TextDocument.Completion.CompletionItem.SnippetSupport
s.signatureHelpEnabled = true s.signatureHelpEnabled = true
rootPath, err := fromProtocolURI(*params.RootURI).Filename() var rootURI protocol.DocumentURI
if params.RootURI != nil {
rootURI = *params.RootURI
}
sourceURI, err := fromProtocolURI(rootURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
rootPath, err := sourceURI.Filename()
if err != nil {
return nil, err
}
s.view = cache.NewView(&packages.Config{ s.view = cache.NewView(&packages.Config{
Dir: rootPath, Dir: rootPath,
Mode: packages.LoadSyntax, Mode: packages.LoadSyntax,
@ -180,12 +189,20 @@ func (s *server) DidSave(context.Context, *protocol.DidSaveTextDocumentParams) e
} }
func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error { func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
s.setContent(ctx, fromProtocolURI(params.TextDocument.URI), nil) sourceURI, err := fromProtocolURI(params.TextDocument.URI)
if err != nil {
return err
}
s.setContent(ctx, sourceURI, nil)
return nil return nil
} }
func (s *server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) { func (s *server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
f, err := s.view.GetFile(ctx, fromProtocolURI(params.TextDocument.URI)) sourceURI, err := fromProtocolURI(params.TextDocument.URI)
if err != nil {
return nil, err
}
f, err := s.view.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -209,7 +226,11 @@ func (s *server) CompletionResolve(context.Context, *protocol.CompletionItem) (*
} }
func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) { func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
f, err := s.view.GetFile(ctx, fromProtocolURI(params.TextDocument.URI)) sourceURI, err := fromProtocolURI(params.TextDocument.URI)
if err != nil {
return nil, err
}
f, err := s.view.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -237,7 +258,11 @@ func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositio
} }
func (s *server) SignatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) { func (s *server) SignatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) {
f, err := s.view.GetFile(ctx, fromProtocolURI(params.TextDocument.URI)) sourceURI, err := fromProtocolURI(params.TextDocument.URI)
if err != nil {
return nil, err
}
f, err := s.view.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -254,7 +279,11 @@ func (s *server) SignatureHelp(ctx context.Context, params *protocol.TextDocumen
} }
func (s *server) Definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) { func (s *server) Definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
f, err := s.view.GetFile(ctx, fromProtocolURI(params.TextDocument.URI)) sourceURI, err := fromProtocolURI(params.TextDocument.URI)
if err != nil {
return nil, err
}
f, err := s.view.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -271,7 +300,11 @@ func (s *server) Definition(ctx context.Context, params *protocol.TextDocumentPo
} }
func (s *server) TypeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) { func (s *server) TypeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
f, err := s.view.GetFile(ctx, fromProtocolURI(params.TextDocument.URI)) sourceURI, err := fromProtocolURI(params.TextDocument.URI)
if err != nil {
return nil, err
}
f, err := s.view.GetFile(ctx, sourceURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }