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

tools/cmd/gopls: modify gopls to use automatcally generated types

Gopls presently uses hand-coded data types (in internal/lsp/protocol)
for communicating with LSP clients. Instead, modify it to use the
automatically generated file (internal/lsp/protocol/tsprotocol.go).

Replaced files have been put (temporarily) in a directory 'preserve'
so readers can compare the old data types with the new ones.

Change-Id: Idfa53a5783e2d6a47e03b20641dd76fbc2c32677
Reviewed-on: https://go-review.googlesource.com/c/tools/+/166757
Run-TryBot: Peter Weinberger <pjw@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Peter Weinberger 2019-03-11 12:17:05 -04:00
parent c0c382bb4e
commit 6a988fd03a
18 changed files with 3852 additions and 40 deletions

View File

@ -15,7 +15,7 @@ import (
) )
func toProtocolCompletionItems(candidates []source.CompletionItem, prefix string, pos protocol.Position, snippetsSupported, signatureHelpEnabled bool) []protocol.CompletionItem { func toProtocolCompletionItems(candidates []source.CompletionItem, prefix string, pos protocol.Position, snippetsSupported, signatureHelpEnabled bool) []protocol.CompletionItem {
insertTextFormat := protocol.PlainTextFormat insertTextFormat := protocol.PlainTextTextFormat
if snippetsSupported { if snippetsSupported {
insertTextFormat = protocol.SnippetTextFormat insertTextFormat = protocol.SnippetTextFormat
} }
@ -35,8 +35,8 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, prefix string
item := protocol.CompletionItem{ item := protocol.CompletionItem{
Label: candidate.Label, Label: candidate.Label,
Detail: candidate.Detail, Detail: candidate.Detail,
Kind: float64(toProtocolCompletionItemKind(candidate.Kind)), Kind: toProtocolCompletionItemKind(candidate.Kind),
InsertTextFormat: insertTextFormat, InsertTextFormat: protocol.PlainTextTextFormat,
TextEdit: &protocol.TextEdit{ TextEdit: &protocol.TextEdit{
NewText: insertText, NewText: insertText,
Range: protocol.Range{ Range: protocol.Range{
@ -105,7 +105,7 @@ func labelToProtocolSnippets(label string, kind source.CompletionItemKind, inser
return label, true return label, true
} }
// Don't add parameters or parens for the plaintext insert format. // Don't add parameters or parens for the plaintext insert format.
if insertTextFormat == protocol.PlainTextFormat { if insertTextFormat == protocol.PlainTextTextFormat {
return trimmed, true return trimmed, true
} }
// If we do have signature help enabled, the user can see parameters as // If we do have signature help enabled, the user can see parameters as

View File

@ -12,7 +12,7 @@ import (
"golang.org/x/tools/internal/lsp/source" "golang.org/x/tools/internal/lsp/source"
) )
func (s *server) cacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI, content string) { func (s *server) cacheAndDiagnose(ctx context.Context, uri string, content string) {
sourceURI, err := fromProtocolURI(uri) sourceURI, err := fromProtocolURI(uri)
if err != nil { if err != nil {
return // handle error? return // handle error?
@ -31,7 +31,7 @@ func (s *server) cacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI,
} }
for filename, diagnostics := range reports { for filename, diagnostics := range reports {
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{ s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
URI: protocol.DocumentURI(source.ToURI(filename)), URI: string(source.ToURI(filename)),
Diagnostics: toProtocolDiagnostics(ctx, s.view, diagnostics), Diagnostics: toProtocolDiagnostics(ctx, s.view, diagnostics),
}) })
} }

View File

@ -8,7 +8,7 @@ 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 string, rng *protocol.Range) ([]protocol.TextEdit, error) {
sourceURI, err := fromProtocolURI(uri) sourceURI, err := fromProtocolURI(uri)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -11,7 +11,7 @@ import (
"golang.org/x/tools/internal/lsp/source" "golang.org/x/tools/internal/lsp/source"
) )
func organizeImports(ctx context.Context, v source.View, uri protocol.DocumentURI) ([]protocol.TextEdit, error) { func organizeImports(ctx context.Context, v source.View, uri string) ([]protocol.TextEdit, error) {
sourceURI, err := fromProtocolURI(uri) sourceURI, err := fromProtocolURI(uri)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -248,7 +248,7 @@ func (c completions) test(t *testing.T, exported *packagestest.Exported, s *serv
list, err := s.Completion(context.Background(), &protocol.CompletionParams{ list, err := s.Completion(context.Background(), &protocol.CompletionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{ TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{ TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentURI(source.ToURI(src.Filename)), URI: string(source.ToURI(src.Filename)),
}, },
Position: protocol.Position{ Position: protocol.Position{
Line: float64(src.Line - 1), Line: float64(src.Line - 1),
@ -278,7 +278,7 @@ func (c completions) test(t *testing.T, exported *packagestest.Exported, s *serv
func isBuiltin(item protocol.CompletionItem) bool { func isBuiltin(item protocol.CompletionItem) bool {
// If a type has no detail, it is a builtin type. // If a type has no detail, it is a builtin type.
if item.Detail == "" && item.Kind == float64(protocol.TypeParameterCompletion) { if item.Detail == "" && item.Kind == protocol.TypeParameterCompletion {
return true return true
} }
// Remaining builtin constants, variables, interfaces, and functions. // Remaining builtin constants, variables, interfaces, and functions.
@ -324,7 +324,7 @@ func (i completionItems) collect(pos token.Pos, label, detail, kind string) {
i[pos] = &protocol.CompletionItem{ i[pos] = &protocol.CompletionItem{
Label: label, Label: label,
Detail: detail, Detail: detail,
Kind: float64(k), Kind: k,
} }
} }
@ -364,7 +364,7 @@ func (f formats) test(t *testing.T, s *server) {
for filename, gofmted := range f { for filename, gofmted := range f {
edits, err := s.Formatting(context.Background(), &protocol.DocumentFormattingParams{ edits, err := s.Formatting(context.Background(), &protocol.DocumentFormattingParams{
TextDocument: protocol.TextDocumentIdentifier{ TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentURI(source.ToURI(filename)), URI: string(source.ToURI(filename)),
}, },
}) })
if err != nil { if err != nil {

View File

@ -14,9 +14,9 @@ import (
"golang.org/x/tools/internal/lsp/source" "golang.org/x/tools/internal/lsp/source"
) )
// fromProtocolURI converts a protocol.DocumentURI to a source.URI. // fromProtocolURI converts a string 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, error) { func fromProtocolURI(uri string) (source.URI, error) {
unescaped, err := url.PathUnescape(string(uri)) unescaped, err := url.PathUnescape(string(uri))
if err != nil { if err != nil {
return "", err return "", err
@ -45,7 +45,7 @@ func toProtocolLocation(fset *token.FileSet, r source.Range) protocol.Location {
tok := fset.File(r.Start) tok := fset.File(r.Start)
uri := source.ToURI(tok.Name()) uri := source.ToURI(tok.Name())
return protocol.Location{ return protocol.Location{
URI: protocol.DocumentURI(uri), URI: string(uri),
Range: toProtocolRange(tok, r), Range: toProtocolRange(tok, r),
} }
} }

View File

@ -0,0 +1 @@
These are the hand-generated protocol files, for reference while debugging.

View File

@ -48,7 +48,7 @@ type Server interface {
RangeFormatting(context.Context, *DocumentRangeFormattingParams) ([]TextEdit, error) RangeFormatting(context.Context, *DocumentRangeFormattingParams) ([]TextEdit, error)
OnTypeFormatting(context.Context, *DocumentOnTypeFormattingParams) ([]TextEdit, error) OnTypeFormatting(context.Context, *DocumentOnTypeFormattingParams) ([]TextEdit, error)
Rename(context.Context, *RenameParams) ([]WorkspaceEdit, error) Rename(context.Context, *RenameParams) ([]WorkspaceEdit, error)
FoldingRanges(context.Context, *FoldingRangeRequestParam) ([]FoldingRange, error) FoldingRanges(context.Context, *FoldingRangeParams) ([]FoldingRange, error)
} }
func serverHandler(server Server) jsonrpc2.Handler { func serverHandler(server Server) jsonrpc2.Handler {
@ -374,7 +374,7 @@ func serverHandler(server Server) jsonrpc2.Handler {
unhandledError(conn.Reply(ctx, r, resp, err)) unhandledError(conn.Reply(ctx, r, resp, err))
case "textDocument/foldingRange": case "textDocument/foldingRange":
var params FoldingRangeRequestParam var params FoldingRangeParams
if err := json.Unmarshal(*r.Params, &params); err != nil { if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, conn, r, err) sendParseError(ctx, conn, r, err)
return return
@ -637,10 +637,17 @@ func (s *serverDispatcher) Rename(ctx context.Context, params *RenameParams) ([]
return result, nil return result, nil
} }
func (s *serverDispatcher) FoldingRanges(ctx context.Context, params *FoldingRangeRequestParam) ([]FoldingRange, error) { func (s *serverDispatcher) FoldingRanges(ctx context.Context, params *FoldingRangeParams) ([]FoldingRange, error) {
var result []FoldingRange var result []FoldingRange
if err := s.Conn.Call(ctx, "textDocument/foldingRanges", params, &result); err != nil { if err := s.Conn.Call(ctx, "textDocument/foldingRanges", params, &result); err != nil {
return nil, err return nil, err
} }
return result, nil return result, nil
} }
type CancelParams struct {
/**
* The request id to cancel.
*/
ID jsonrpc2.ID `json:"id"`
}

File diff suppressed because it is too large Load Diff

View File

@ -84,12 +84,15 @@ func (s *server) Initialize(ctx context.Context, params *protocol.InitializePara
s.initialized = true // mark server as initialized now s.initialized = true // mark server as initialized now
// Check if the client supports snippets in completion items. // Check if the client supports snippets in completion items.
s.snippetsSupported = params.Capabilities.TextDocument.Completion.CompletionItem.SnippetSupport capText := params.Capabilities.InnerClientCapabilities.TextDocument
if capText != nil && capText.Completion != nil && capText.Completion.CompletionItem != nil {
s.snippetsSupported = capText.Completion.CompletionItem.SnippetSupport
}
s.signatureHelpEnabled = true s.signatureHelpEnabled = true
var rootURI protocol.DocumentURI var rootURI string
if params.RootURI != nil { if params.RootURI != "" {
rootURI = *params.RootURI rootURI = params.RootURI
} }
sourceURI, err := fromProtocolURI(rootURI) sourceURI, err := fromProtocolURI(rootURI)
if err != nil { if err != nil {
@ -118,22 +121,26 @@ func (s *server) Initialize(ctx context.Context, params *protocol.InitializePara
return &protocol.InitializeResult{ return &protocol.InitializeResult{
Capabilities: protocol.ServerCapabilities{ Capabilities: protocol.ServerCapabilities{
CodeActionProvider: true, InnerServerCapabilities: protocol.InnerServerCapabilities{
CompletionProvider: protocol.CompletionOptions{ CodeActionProvider: true,
TriggerCharacters: []string{"."}, CompletionProvider: &protocol.CompletionOptions{
TriggerCharacters: []string{"."},
},
DefinitionProvider: true,
DocumentFormattingProvider: true,
DocumentRangeFormattingProvider: true,
HoverProvider: true,
SignatureHelpProvider: &protocol.SignatureHelpOptions{
TriggerCharacters: []string{"(", ","},
},
TextDocumentSync: &protocol.TextDocumentSyncOptions{
Change: s.textDocumentSyncKind,
OpenClose: true,
},
}, },
DefinitionProvider: true, TypeDefinitionServerCapabilities: protocol.TypeDefinitionServerCapabilities{
DocumentFormattingProvider: true, TypeDefinitionProvider: true,
DocumentRangeFormattingProvider: true,
HoverProvider: true,
SignatureHelpProvider: protocol.SignatureHelpOptions{
TriggerCharacters: []string{"(", ","},
}, },
TextDocumentSync: protocol.TextDocumentSyncOptions{
Change: float64(s.textDocumentSyncKind),
OpenClose: true,
},
TypeDefinitionProvider: true,
}, },
}, nil }, nil
} }
@ -345,12 +352,13 @@ func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositio
return nil, err return nil, err
} }
markdown := "```go\n" + content + "\n```" markdown := "```go\n" + content + "\n```"
x := toProtocolRange(tok, ident.Range)
return &protocol.Hover{ return &protocol.Hover{
Contents: protocol.MarkupContent{ Contents: protocol.MarkupContent{
Kind: protocol.Markdown, Kind: protocol.Markdown,
Value: markdown, Value: markdown,
}, },
Range: toProtocolRange(tok, ident.Range), Range: &x,
}, nil }, nil
} }
@ -433,8 +441,8 @@ func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionPara
{ {
Title: "Organize Imports", Title: "Organize Imports",
Kind: protocol.SourceOrganizeImports, Kind: protocol.SourceOrganizeImports,
Edit: protocol.WorkspaceEdit{ Edit: &protocol.WorkspaceEdit{
Changes: map[protocol.DocumentURI][]protocol.TextEdit{ Changes: &map[string][]protocol.TextEdit{
params.TextDocument.URI: edits, params.TextDocument.URI: edits,
}, },
}, },
@ -482,7 +490,7 @@ func (s *server) Rename(context.Context, *protocol.RenameParams) ([]protocol.Wor
return nil, notImplemented("Rename") return nil, notImplemented("Rename")
} }
func (s *server) FoldingRanges(context.Context, *protocol.FoldingRangeRequestParam) ([]protocol.FoldingRange, error) { func (s *server) FoldingRanges(context.Context, *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) {
return nil, notImplemented("FoldingRanges") return nil, notImplemented("FoldingRanges")
} }