1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -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 {
insertTextFormat := protocol.PlainTextFormat
insertTextFormat := protocol.PlainTextTextFormat
if snippetsSupported {
insertTextFormat = protocol.SnippetTextFormat
}
@ -35,8 +35,8 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, prefix string
item := protocol.CompletionItem{
Label: candidate.Label,
Detail: candidate.Detail,
Kind: float64(toProtocolCompletionItemKind(candidate.Kind)),
InsertTextFormat: insertTextFormat,
Kind: toProtocolCompletionItemKind(candidate.Kind),
InsertTextFormat: protocol.PlainTextTextFormat,
TextEdit: &protocol.TextEdit{
NewText: insertText,
Range: protocol.Range{
@ -105,7 +105,7 @@ func labelToProtocolSnippets(label string, kind source.CompletionItemKind, inser
return label, true
}
// Don't add parameters or parens for the plaintext insert format.
if insertTextFormat == protocol.PlainTextFormat {
if insertTextFormat == protocol.PlainTextTextFormat {
return trimmed, true
}
// 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"
)
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)
if err != nil {
return // handle error?
@ -31,7 +31,7 @@ func (s *server) cacheAndDiagnose(ctx context.Context, uri protocol.DocumentURI,
}
for filename, diagnostics := range reports {
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
URI: protocol.DocumentURI(source.ToURI(filename)),
URI: string(source.ToURI(filename)),
Diagnostics: toProtocolDiagnostics(ctx, s.view, diagnostics),
})
}

View File

@ -8,7 +8,7 @@ import (
)
// 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)
if err != nil {
return nil, err

View File

@ -11,7 +11,7 @@ import (
"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)
if err != nil {
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{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentURI(source.ToURI(src.Filename)),
URI: string(source.ToURI(src.Filename)),
},
Position: protocol.Position{
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 {
// 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
}
// 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{
Label: label,
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 {
edits, err := s.Formatting(context.Background(), &protocol.DocumentFormattingParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentURI(source.ToURI(filename)),
URI: string(source.ToURI(filename)),
},
})
if err != nil {

View File

@ -14,9 +14,9 @@ import (
"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.
func fromProtocolURI(uri protocol.DocumentURI) (source.URI, error) {
func fromProtocolURI(uri string) (source.URI, error) {
unescaped, err := url.PathUnescape(string(uri))
if err != nil {
return "", err
@ -45,7 +45,7 @@ func toProtocolLocation(fset *token.FileSet, r source.Range) protocol.Location {
tok := fset.File(r.Start)
uri := source.ToURI(tok.Name())
return protocol.Location{
URI: protocol.DocumentURI(uri),
URI: string(uri),
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)
OnTypeFormatting(context.Context, *DocumentOnTypeFormattingParams) ([]TextEdit, 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 {
@ -374,7 +374,7 @@ func serverHandler(server Server) jsonrpc2.Handler {
unhandledError(conn.Reply(ctx, r, resp, err))
case "textDocument/foldingRange":
var params FoldingRangeRequestParam
var params FoldingRangeParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, conn, r, err)
return
@ -637,10 +637,17 @@ func (s *serverDispatcher) Rename(ctx context.Context, params *RenameParams) ([]
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
if err := s.Conn.Call(ctx, "textDocument/foldingRanges", params, &result); err != nil {
return nil, err
}
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
// 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
var rootURI protocol.DocumentURI
if params.RootURI != nil {
rootURI = *params.RootURI
var rootURI string
if params.RootURI != "" {
rootURI = params.RootURI
}
sourceURI, err := fromProtocolURI(rootURI)
if err != nil {
@ -118,22 +121,26 @@ func (s *server) Initialize(ctx context.Context, params *protocol.InitializePara
return &protocol.InitializeResult{
Capabilities: protocol.ServerCapabilities{
CodeActionProvider: true,
CompletionProvider: protocol.CompletionOptions{
TriggerCharacters: []string{"."},
InnerServerCapabilities: protocol.InnerServerCapabilities{
CodeActionProvider: true,
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,
DocumentFormattingProvider: true,
DocumentRangeFormattingProvider: true,
HoverProvider: true,
SignatureHelpProvider: protocol.SignatureHelpOptions{
TriggerCharacters: []string{"(", ","},
TypeDefinitionServerCapabilities: protocol.TypeDefinitionServerCapabilities{
TypeDefinitionProvider: true,
},
TextDocumentSync: protocol.TextDocumentSyncOptions{
Change: float64(s.textDocumentSyncKind),
OpenClose: true,
},
TypeDefinitionProvider: true,
},
}, nil
}
@ -345,12 +352,13 @@ func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositio
return nil, err
}
markdown := "```go\n" + content + "\n```"
x := toProtocolRange(tok, ident.Range)
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: markdown,
},
Range: toProtocolRange(tok, ident.Range),
Range: &x,
}, nil
}
@ -433,8 +441,8 @@ func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionPara
{
Title: "Organize Imports",
Kind: protocol.SourceOrganizeImports,
Edit: protocol.WorkspaceEdit{
Changes: map[protocol.DocumentURI][]protocol.TextEdit{
Edit: &protocol.WorkspaceEdit{
Changes: &map[string][]protocol.TextEdit{
params.TextDocument.URI: edits,
},
},
@ -482,7 +490,7 @@ func (s *server) Rename(context.Context, *protocol.RenameParams) ([]protocol.Wor
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")
}