1
0
mirror of https://github.com/golang/go synced 2024-11-05 11:46:12 -07:00

internal/lsp: reorganize the generated Go code for the lsp protocol

Code generation has been unified, so that tsprotocol.go and tsserver.go
are produced by the same program. tsprotocol.go is about 900 lines shorter,
partly from removing boilerplate comments that golint no longer requires.
(And partly by generating fewer unneeded types.)

The choice made for a union type is commented with the set of types. There
is no Go equivalent for union types, but making themn all interface{}
would replace type checking at unmarshalling with checking runtime
conversions.

Intersection types (A&B) are sometimes embedded (struct{A;B;}, and
sometimes expanded, as they have to be if A and B have fields with the
same names.

There are fewer embedded structs, which had been verbose and confusing to
initialize. They have been replaced by types whose names end in Gn.

Essentially all the generated *structs have been removed. This makes
no difference in what the client sends, and the server may send a {}
where it previously might have sent nothing. The benefit is that some
nil tests can be removed. Thus 'omitempty' in json tags is just
documentation that the element is optional in the protocol.

The files that generate this code will be submitted later, but soon.

Change-Id: I52b997d9c58de3d733fc8c6ce061e47ce2bdb100
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207598
Run-TryBot: Peter Weinberger <pjw@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
pjw 2019-11-17 14:29:15 -05:00 committed by Peter Weinberger
parent 5a76f03bc7
commit 5091d647ee
18 changed files with 1396 additions and 2274 deletions

View File

@ -204,10 +204,10 @@ func (app *Application) connect(ctx context.Context) (*connection, error) {
}
func (c *connection) initialize(ctx context.Context) error {
params := &protocol.ParamInitia{}
params := &protocol.ParamInitialize{}
params.RootURI = string(span.FileURI(c.Client.app.wd))
params.Capabilities.Workspace.Configuration = true
params.Capabilities.TextDocument.Hover = &protocol.HoverClientCapabilities{
params.Capabilities.TextDocument.Hover = protocol.HoverClientCapabilities{
ContentFormat: []protocol.MarkupKind{protocol.PlainText},
}
if _, err := c.Server.Initialize(ctx, params); err != nil {
@ -295,7 +295,7 @@ func (c *cmdClient) WorkspaceFolders(ctx context.Context) ([]protocol.WorkspaceF
return nil, nil
}
func (c *cmdClient) Configuration(ctx context.Context, p *protocol.ParamConfig) ([]interface{}, error) {
func (c *cmdClient) Configuration(ctx context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) {
results := make([]interface{}, len(p.Items))
for i, item := range p.Items {
if item.Section != "gopls" {

View File

@ -69,7 +69,11 @@ func (t *imports) Run(ctx context.Context, args ...string) error {
return errors.Errorf("%v: %v", from, err)
}
var edits []protocol.TextEdit
for _, a := range actions {
v, ok := actions.([]protocol.CodeAction)
if !ok {
return errors.Errorf("expected CodeAction, got %T", actions)
}
for _, a := range v {
if a.Title != "Organize Imports" {
continue
}

View File

@ -87,7 +87,11 @@ func (s *suggestedfix) Run(ctx context.Context, args ...string) error {
return errors.Errorf("%v: %v", from, err)
}
var edits []protocol.TextEdit
for _, a := range actions {
v, ok := actions.([]protocol.CodeAction)
if !ok {
return errors.Errorf("expected CodeAction, got %T", actions)
}
for _, a := range v {
if !a.IsPreferred && !s.All {
continue
}

View File

@ -52,7 +52,6 @@ func (r *symbols) Run(ctx context.Context, args ...string) error {
if err != nil {
return err
}
for _, s := range symbols {
fmt.Println(symbolToString(s))
// Sort children for consistency

View File

@ -63,7 +63,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
codeActions = append(codeActions, protocol.CodeAction{
Title: "Tidy",
Kind: protocol.SourceOrganizeImports,
Command: &protocol.Command{
Command: protocol.Command{
Title: "Tidy",
Command: "tidy",
Arguments: []interface{}{
@ -94,7 +94,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
codeActions = append(codeActions, protocol.CodeAction{
Title: importFixTitle(importFix.Fix),
Kind: protocol.QuickFix,
Edit: &protocol.WorkspaceEdit{
Edit: protocol.WorkspaceEdit{
DocumentChanges: documentChanges(fh, importFix.Edits),
},
Diagnostics: fixDiagnostics,
@ -107,7 +107,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
codeActions = append(codeActions, protocol.CodeAction{
Title: "Organize Imports",
Kind: protocol.SourceOrganizeImports,
Edit: &protocol.WorkspaceEdit{
Edit: protocol.WorkspaceEdit{
DocumentChanges: documentChanges(fh, edits),
},
})
@ -226,7 +226,7 @@ func quickFixes(ctx context.Context, s source.Snapshot, f source.File, diagnosti
Title: fix.Title,
Kind: protocol.QuickFix,
Diagnostics: []protocol.Diagnostic{diag},
Edit: &protocol.WorkspaceEdit{},
Edit: protocol.WorkspaceEdit{},
}
for uri, edits := range fix.Edits {
f, err := s.View().GetFile(ctx, uri)

View File

@ -104,7 +104,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
Label: candidate.Label,
Detail: candidate.Detail,
Kind: candidate.Kind,
TextEdit: &protocol.TextEdit{
TextEdit: protocol.TextEdit{
NewText: insertText,
Range: rng,
},
@ -123,7 +123,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
// since we show return types as well.
switch item.Kind {
case protocol.FunctionCompletion, protocol.MethodCompletion:
item.Command = &protocol.Command{
item.Command = protocol.Command{
Command: "editor.action.triggerParameterHints",
}
}

View File

@ -20,7 +20,7 @@ import (
errors "golang.org/x/xerrors"
)
func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitia) (*protocol.InitializeResult, error) {
func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
s.stateMu.Lock()
state := s.state
s.stateMu.Unlock()
@ -53,8 +53,7 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitia) (
}
var codeActionProvider interface{}
if ca := params.Capabilities.TextDocument.CodeAction; ca != nil && ca.CodeActionLiteralSupport != nil &&
len(ca.CodeActionLiteralSupport.CodeActionKind.ValueSet) > 0 {
if ca := params.Capabilities.TextDocument.CodeAction; len(ca.CodeActionLiteralSupport.CodeActionKind.ValueSet) > 0 {
// If the client has specified CodeActionLiteralSupport,
// send the code actions we support.
//
@ -65,18 +64,16 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitia) (
} else {
codeActionProvider = true
}
var renameOpts interface{}
if r := params.Capabilities.TextDocument.Rename; r != nil {
renameOpts = &protocol.RenameOptions{
PrepareProvider: r.PrepareSupport,
}
} else {
renameOpts = true
// This used to be interface{}, when r could be nil
var renameOpts protocol.RenameOptions
r := params.Capabilities.TextDocument.Rename
renameOpts = protocol.RenameOptions{
PrepareProvider: r.PrepareSupport,
}
return &protocol.InitializeResult{
Capabilities: protocol.ServerCapabilities{
CodeActionProvider: codeActionProvider,
CompletionProvider: &protocol.CompletionOptions{
CompletionProvider: protocol.CompletionOptions{
TriggerCharacters: []string{"."},
},
DefinitionProvider: true,
@ -84,35 +81,27 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitia) (
ImplementationProvider: true,
DocumentFormattingProvider: true,
DocumentSymbolProvider: true,
ExecuteCommandProvider: &protocol.ExecuteCommandOptions{
ExecuteCommandProvider: protocol.ExecuteCommandOptions{
Commands: options.SupportedCommands,
},
FoldingRangeProvider: true,
HoverProvider: true,
DocumentHighlightProvider: true,
DocumentLinkProvider: &protocol.DocumentLinkOptions{},
DocumentLinkProvider: protocol.DocumentLinkOptions{},
ReferencesProvider: true,
RenameProvider: renameOpts,
SignatureHelpProvider: &protocol.SignatureHelpOptions{
SignatureHelpProvider: protocol.SignatureHelpOptions{
TriggerCharacters: []string{"(", ","},
},
TextDocumentSync: &protocol.TextDocumentSyncOptions{
Change: options.TextDocumentSyncKind,
OpenClose: true,
Save: &protocol.SaveOptions{
Save: protocol.SaveOptions{
IncludeText: false,
},
},
Workspace: &struct {
WorkspaceFolders *struct {
Supported bool "json:\"supported,omitempty\""
ChangeNotifications string "json:\"changeNotifications,omitempty\""
} "json:\"workspaceFolders,omitempty\""
}{
WorkspaceFolders: &struct {
Supported bool "json:\"supported,omitempty\""
ChangeNotifications string "json:\"changeNotifications,omitempty\""
}{
Workspace: protocol.WorkspaceGn{
protocol.WorkspaceFoldersGn{
Supported: true,
ChangeNotifications: "workspace/didChangeWorkspaceFolders",
},
@ -192,7 +181,7 @@ func (s *Server) fetchConfig(ctx context.Context, name string, folder span.URI,
if !s.session.Options().ConfigurationSupported {
return nil
}
v := protocol.ParamConfig{
v := protocol.ParamConfiguration{
ConfigurationParams: protocol.ConfigurationParams{
Items: []protocol.ConfigurationItem{{
ScopeURI: protocol.NewURI(folder),

View File

@ -32,7 +32,7 @@ func toProtocolHighlight(rngs []protocol.Range) []protocol.DocumentHighlight {
kind := protocol.Text
for _, rng := range rngs {
result = append(result, protocol.DocumentHighlight{
Kind: &kind,
Kind: kind,
Range: rng,
})
}

View File

@ -40,7 +40,7 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
contents := s.toProtocolHoverContents(ctx, hover, view.Options())
return &protocol.Hover{
Contents: contents,
Range: &rng,
Range: rng,
}, nil
}

View File

@ -314,8 +314,9 @@ func (r *runner) Import(t *testing.T, spn span.Span) {
t.Fatal(err)
}
got := string(m.Content)
if len(actions) > 0 {
res, err := applyWorkspaceEdits(r, actions[0].Edit)
xact := actions.([]protocol.CodeAction)
if len(xact) > 0 {
res, err := applyWorkspaceEdits(r, xact[0].Edit)
if err != nil {
t.Fatal(err)
}
@ -354,10 +355,11 @@ func (r *runner) SuggestedFix(t *testing.T, spn span.Span) {
t.Fatal(err)
}
// TODO: This test should probably be able to handle multiple code actions.
if len(actions) > 1 {
xact := actions.([]protocol.CodeAction)
if len(xact) > 1 {
t.Fatal("expected only 1 code action")
}
res, err := applyWorkspaceEdits(r, actions[0].Edit)
res, err := applyWorkspaceEdits(r, xact[0].Edit)
if err != nil {
t.Fatal(err)
}
@ -575,7 +577,7 @@ func (r *runner) Rename(t *testing.T, spn span.Span, newText string) {
}
return
}
res, err := applyWorkspaceEdits(r, wedit)
res, err := applyWorkspaceEdits(r, *wedit)
if err != nil {
t.Fatal(err)
}
@ -628,18 +630,23 @@ func (r *runner) PrepareRename(t *testing.T, src span.Span, want *source.Prepare
t.Errorf("prepare rename failed for %v: got error: %v", src, err)
return
}
if got == nil {
// we all love typed nils
if got == nil || got.(*protocol.Range) == nil {
if want.Text != "" { // expected an ident.
t.Errorf("prepare rename failed for %v: got nil", src)
}
return
}
if protocol.CompareRange(*got, want.Range) != 0 {
t.Errorf("prepare rename failed: incorrect range got %v want %v", *got, want.Range)
xx, ok := got.(*protocol.Range)
if !ok {
t.Fatalf("got %T, wanted Range", got)
}
if protocol.CompareRange(*xx, want.Range) != 0 {
t.Errorf("prepare rename failed: incorrect range got %v want %v", *xx, want.Range)
}
}
func applyWorkspaceEdits(r *runner, wedit *protocol.WorkspaceEdit) (map[span.URI]string, error) {
func applyWorkspaceEdits(r *runner, wedit protocol.WorkspaceEdit) (map[span.URI]string, error) {
res := map[span.URI]string{}
for _, docEdits := range wedit.DocumentChanges {
uri := span.URI(docEdits.TextDocument.URI)
@ -682,7 +689,6 @@ func (r *runner) Symbols(t *testing.T, uri span.URI, expectedSymbols []protocol.
if err != nil {
t.Fatal(err)
}
if len(symbols) != len(expectedSymbols) {
t.Errorf("want %d top-level symbols in %v, got %d", len(expectedSymbols), uri, len(symbols))
return

View File

@ -7,6 +7,7 @@ package protocol
import (
"context"
"encoding/json"
"fmt"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/telemetry/log"
@ -39,7 +40,16 @@ func (canceller) Request(ctx context.Context, conn *jsonrpc2.Conn, direction jso
if err := json.Unmarshal(*r.Params, &params); err != nil {
log.Error(ctx, "", err)
} else {
conn.Cancel(params.ID)
v := jsonrpc2.ID{}
if n, ok := params.ID.(float64); ok {
v.Number = int64(n)
} else if s, ok := params.ID.(string); ok {
v.Name = s
} else {
log.Error(ctx, fmt.Sprintf("Request ID %v malformed", params.ID), nil)
return ctx
}
conn.Cancel(v)
}
}
return ctx

View File

@ -1,5 +1,10 @@
package protocol
// Package protocol contains data types and code for LSP jsonrpcs
// generated automatically from vscode-languageserver-node
// commit: 635ab1fe6f8c57ce9402e573d007f24d6d290fd3
// last fetched Sun Oct 13 2019 10:14:32 GMT-0400 (Eastern Daylight Time)
// Code generated (see typescript/README.md) DO NOT EDIT.
import (
@ -16,11 +21,11 @@ type Client interface {
LogMessage(context.Context, *LogMessageParams) error
Event(context.Context, *interface{}) error
PublishDiagnostics(context.Context, *PublishDiagnosticsParams) error
WorkspaceFolders(context.Context) ([]WorkspaceFolder, error)
Configuration(context.Context, *ParamConfig) ([]interface{}, error)
WorkspaceFolders(context.Context) ([]WorkspaceFolder /*WorkspaceFolder[] | null*/, error)
Configuration(context.Context, *ParamConfiguration) ([]interface{}, error)
RegisterCapability(context.Context, *RegistrationParams) error
UnregisterCapability(context.Context, *UnregistrationParams) error
ShowMessageRequest(context.Context, *ShowMessageRequestParams) (*MessageActionItem, error)
ShowMessageRequest(context.Context, *ShowMessageRequestParams) (*MessageActionItem /*MessageActionItem | null*/, error)
ApplyEdit(context.Context, *ApplyWorkspaceEditParams) (*ApplyWorkspaceEditResponse, error)
}
@ -85,7 +90,7 @@ func (h clientHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, deliver
}
return true
case "workspace/configuration": // req
var params ParamConfig
var params ParamConfiguration
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, r, err)
return true
@ -164,15 +169,15 @@ func (s *clientDispatcher) Event(ctx context.Context, params *interface{}) error
func (s *clientDispatcher) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) error {
return s.Conn.Notify(ctx, "textDocument/publishDiagnostics", params)
}
func (s *clientDispatcher) WorkspaceFolders(ctx context.Context) ([]WorkspaceFolder, error) {
var result []WorkspaceFolder
func (s *clientDispatcher) WorkspaceFolders(ctx context.Context) ([]WorkspaceFolder /*WorkspaceFolder[] | null*/, error) {
var result []WorkspaceFolder /*WorkspaceFolder[] | null*/
if err := s.Conn.Call(ctx, "workspace/workspaceFolders", nil, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *clientDispatcher) Configuration(ctx context.Context, params *ParamConfig) ([]interface{}, error) {
func (s *clientDispatcher) Configuration(ctx context.Context, params *ParamConfiguration) ([]interface{}, error) {
var result []interface{}
if err := s.Conn.Call(ctx, "workspace/configuration", params, &result); err != nil {
return nil, err
@ -188,8 +193,8 @@ func (s *clientDispatcher) UnregisterCapability(ctx context.Context, params *Unr
return s.Conn.Call(ctx, "client/unregisterCapability", params, nil) // Call, not Notify
}
func (s *clientDispatcher) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (*MessageActionItem, error) {
var result MessageActionItem
func (s *clientDispatcher) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (*MessageActionItem /*MessageActionItem | null*/, error) {
var result MessageActionItem /*MessageActionItem | null*/
if err := s.Conn.Call(ctx, "window/showMessageRequest", params, &result); err != nil {
return nil, err
}
@ -203,9 +208,3 @@ func (s *clientDispatcher) ApplyEdit(ctx context.Context, params *ApplyWorkspace
}
return &result, nil
}
// Types constructed to avoid structs as formal argument types
type ParamConfig struct {
ConfigurationParams
PartialResultParams
}

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,10 @@
package protocol
// Package protocol contains data types and code for LSP jsonrpcs
// generated automatically from vscode-languageserver-node
// commit: 635ab1fe6f8c57ce9402e573d007f24d6d290fd3
// last fetched Sun Oct 13 2019 10:14:32 GMT-0400 (Eastern Daylight Time)
// Code generated (see typescript/README.md) DO NOT EDIT.
import (
@ -22,39 +27,40 @@ type Server interface {
DidSave(context.Context, *DidSaveTextDocumentParams) error
WillSave(context.Context, *WillSaveTextDocumentParams) error
DidChangeWatchedFiles(context.Context, *DidChangeWatchedFilesParams) error
CancelRequest(context.Context, *CancelParams) error
Progress(context.Context, *ProgressParams) error
SetTraceNotification(context.Context, *SetTraceParams) error
LogTraceNotification(context.Context, *LogTraceParams) error
Implementation(context.Context, *ImplementationParams) ([]Location, error)
TypeDefinition(context.Context, *TypeDefinitionParams) ([]Location, error)
Implementation(context.Context, *ImplementationParams) (Definition /*Definition | DefinitionLink[] | null*/, error)
TypeDefinition(context.Context, *TypeDefinitionParams) (Definition /*Definition | DefinitionLink[] | null*/, error)
DocumentColor(context.Context, *DocumentColorParams) ([]ColorInformation, error)
ColorPresentation(context.Context, *ColorPresentationParams) ([]ColorPresentation, error)
FoldingRange(context.Context, *FoldingRangeParams) ([]FoldingRange, error)
Declaration(context.Context, *DeclarationParams) ([]DeclarationLink, error)
SelectionRange(context.Context, *SelectionRangeParams) ([]SelectionRange, error)
Initialize(context.Context, *ParamInitia) (*InitializeResult, error)
FoldingRange(context.Context, *FoldingRangeParams) ([]FoldingRange /*FoldingRange[] | null*/, error)
Declaration(context.Context, *DeclarationParams) (Declaration /*Declaration | DeclarationLink[] | null*/, error)
SelectionRange(context.Context, *SelectionRangeParams) ([]SelectionRange /*SelectionRange[] | null*/, error)
Initialize(context.Context, *ParamInitialize) (*InitializeResult, error)
Shutdown(context.Context) error
WillSaveWaitUntil(context.Context, *WillSaveTextDocumentParams) ([]TextEdit, error)
Completion(context.Context, *CompletionParams) (*CompletionList, error)
WillSaveWaitUntil(context.Context, *WillSaveTextDocumentParams) ([]TextEdit /*TextEdit[] | null*/, error)
Completion(context.Context, *CompletionParams) (*CompletionList /*CompletionItem[] | CompletionList | null*/, error)
Resolve(context.Context, *CompletionItem) (*CompletionItem, error)
Hover(context.Context, *HoverParams) (*Hover, error)
SignatureHelp(context.Context, *SignatureHelpParams) (*SignatureHelp, error)
Definition(context.Context, *DefinitionParams) ([]Location, error)
References(context.Context, *ReferenceParams) ([]Location, error)
DocumentHighlight(context.Context, *DocumentHighlightParams) ([]DocumentHighlight, error)
DocumentSymbol(context.Context, *DocumentSymbolParams) ([]DocumentSymbol, error)
CodeAction(context.Context, *CodeActionParams) ([]CodeAction, error)
Symbol(context.Context, *WorkspaceSymbolParams) ([]SymbolInformation, error)
CodeLens(context.Context, *CodeLensParams) ([]CodeLens, error)
Hover(context.Context, *HoverParams) (*Hover /*Hover | null*/, error)
SignatureHelp(context.Context, *SignatureHelpParams) (*SignatureHelp /*SignatureHelp | null*/, error)
Definition(context.Context, *DefinitionParams) (Definition /*Definition | DefinitionLink[] | null*/, error)
References(context.Context, *ReferenceParams) ([]Location /*Location[] | null*/, error)
DocumentHighlight(context.Context, *DocumentHighlightParams) ([]DocumentHighlight /*DocumentHighlight[] | null*/, error)
DocumentSymbol(context.Context, *DocumentSymbolParams) ([]DocumentSymbol /*SymbolInformation[] | DocumentSymbol[] | null*/, error)
CodeAction(context.Context, *CodeActionParams) (interface{} /*Command | CodeAction*/ /*(Command | CodeAction)[] | null*/, error)
Symbol(context.Context, *WorkspaceSymbolParams) ([]SymbolInformation /*SymbolInformation[] | null*/, error)
CodeLens(context.Context, *CodeLensParams) ([]CodeLens /*CodeLens[] | null*/, error)
ResolveCodeLens(context.Context, *CodeLens) (*CodeLens, error)
DocumentLink(context.Context, *DocumentLinkParams) ([]DocumentLink, error)
DocumentLink(context.Context, *DocumentLinkParams) ([]DocumentLink /*DocumentLink[] | null*/, error)
ResolveDocumentLink(context.Context, *DocumentLink) (*DocumentLink, error)
Formatting(context.Context, *DocumentFormattingParams) ([]TextEdit, error)
RangeFormatting(context.Context, *DocumentRangeFormattingParams) ([]TextEdit, error)
OnTypeFormatting(context.Context, *DocumentOnTypeFormattingParams) ([]TextEdit, error)
Rename(context.Context, *RenameParams) (*WorkspaceEdit, error)
PrepareRename(context.Context, *PrepareRenameParams) (*Range, error)
ExecuteCommand(context.Context, *ExecuteCommandParams) (interface{}, error)
Formatting(context.Context, *DocumentFormattingParams) ([]TextEdit /*TextEdit[] | null*/, error)
RangeFormatting(context.Context, *DocumentRangeFormattingParams) ([]TextEdit /*TextEdit[] | null*/, error)
OnTypeFormatting(context.Context, *DocumentOnTypeFormattingParams) ([]TextEdit /*TextEdit[] | null*/, error)
Rename(context.Context, *RenameParams) (*WorkspaceEdit /*WorkspaceEdit | null*/, error)
PrepareRename(context.Context, *PrepareRenameParams) (interface{} /* Range | struct{; Range Range`json:"range"`; Placeholder string`json:"placeholder"`; } | nil*/, error)
ExecuteCommand(context.Context, *ExecuteCommandParams) (interface{} /*any | null*/, error)
}
func (h serverHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, delivered bool) bool {
@ -162,6 +168,16 @@ func (h serverHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, deliver
log.Error(ctx, "", err)
}
return true
case "$/cancelRequest": // notif
var params CancelParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, r, err)
return true
}
if err := h.server.CancelRequest(ctx, &params); err != nil {
log.Error(ctx, "", err)
}
return true
case "$/progress": // notif
var params ProgressParams
if err := json.Unmarshal(*r.Params, &params); err != nil {
@ -270,7 +286,7 @@ func (h serverHandler) Deliver(ctx context.Context, r *jsonrpc2.Request, deliver
}
return true
case "initialize": // req
var params ParamInitia
var params ParamInitialize
if err := json.Unmarshal(*r.Params, &params); err != nil {
sendParseError(ctx, r, err)
return true
@ -571,6 +587,10 @@ func (s *serverDispatcher) DidChangeWatchedFiles(ctx context.Context, params *Di
return s.Conn.Notify(ctx, "workspace/didChangeWatchedFiles", params)
}
func (s *serverDispatcher) CancelRequest(ctx context.Context, params *CancelParams) error {
return s.Conn.Notify(ctx, "$/cancelRequest", params)
}
func (s *serverDispatcher) Progress(ctx context.Context, params *ProgressParams) error {
return s.Conn.Notify(ctx, "$/progress", params)
}
@ -582,16 +602,16 @@ func (s *serverDispatcher) SetTraceNotification(ctx context.Context, params *Set
func (s *serverDispatcher) LogTraceNotification(ctx context.Context, params *LogTraceParams) error {
return s.Conn.Notify(ctx, "$/logTraceNotification", params)
}
func (s *serverDispatcher) Implementation(ctx context.Context, params *ImplementationParams) ([]Location, error) {
var result []Location
func (s *serverDispatcher) Implementation(ctx context.Context, params *ImplementationParams) (Definition /*Definition | DefinitionLink[] | null*/, error) {
var result Definition /*Definition | DefinitionLink[] | null*/
if err := s.Conn.Call(ctx, "textDocument/implementation", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) TypeDefinition(ctx context.Context, params *TypeDefinitionParams) ([]Location, error) {
var result []Location
func (s *serverDispatcher) TypeDefinition(ctx context.Context, params *TypeDefinitionParams) (Definition /*Definition | DefinitionLink[] | null*/, error) {
var result Definition /*Definition | DefinitionLink[] | null*/
if err := s.Conn.Call(ctx, "textDocument/typeDefinition", params, &result); err != nil {
return nil, err
}
@ -614,31 +634,31 @@ func (s *serverDispatcher) ColorPresentation(ctx context.Context, params *ColorP
return result, nil
}
func (s *serverDispatcher) FoldingRange(ctx context.Context, params *FoldingRangeParams) ([]FoldingRange, error) {
var result []FoldingRange
func (s *serverDispatcher) FoldingRange(ctx context.Context, params *FoldingRangeParams) ([]FoldingRange /*FoldingRange[] | null*/, error) {
var result []FoldingRange /*FoldingRange[] | null*/
if err := s.Conn.Call(ctx, "textDocument/foldingRange", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) Declaration(ctx context.Context, params *DeclarationParams) ([]DeclarationLink, error) {
var result []DeclarationLink
func (s *serverDispatcher) Declaration(ctx context.Context, params *DeclarationParams) (Declaration /*Declaration | DeclarationLink[] | null*/, error) {
var result Declaration /*Declaration | DeclarationLink[] | null*/
if err := s.Conn.Call(ctx, "textDocument/declaration", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) SelectionRange(ctx context.Context, params *SelectionRangeParams) ([]SelectionRange, error) {
var result []SelectionRange
func (s *serverDispatcher) SelectionRange(ctx context.Context, params *SelectionRangeParams) ([]SelectionRange /*SelectionRange[] | null*/, error) {
var result []SelectionRange /*SelectionRange[] | null*/
if err := s.Conn.Call(ctx, "textDocument/selectionRange", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) Initialize(ctx context.Context, params *ParamInitia) (*InitializeResult, error) {
func (s *serverDispatcher) Initialize(ctx context.Context, params *ParamInitialize) (*InitializeResult, error) {
var result InitializeResult
if err := s.Conn.Call(ctx, "initialize", params, &result); err != nil {
return nil, err
@ -650,16 +670,16 @@ func (s *serverDispatcher) Shutdown(ctx context.Context) error {
return s.Conn.Call(ctx, "shutdown", nil, nil)
}
func (s *serverDispatcher) WillSaveWaitUntil(ctx context.Context, params *WillSaveTextDocumentParams) ([]TextEdit, error) {
var result []TextEdit
func (s *serverDispatcher) WillSaveWaitUntil(ctx context.Context, params *WillSaveTextDocumentParams) ([]TextEdit /*TextEdit[] | null*/, error) {
var result []TextEdit /*TextEdit[] | null*/
if err := s.Conn.Call(ctx, "textDocument/willSaveWaitUntil", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) Completion(ctx context.Context, params *CompletionParams) (*CompletionList, error) {
var result CompletionList
func (s *serverDispatcher) Completion(ctx context.Context, params *CompletionParams) (*CompletionList /*CompletionItem[] | CompletionList | null*/, error) {
var result CompletionList /*CompletionItem[] | CompletionList | null*/
if err := s.Conn.Call(ctx, "textDocument/completion", params, &result); err != nil {
return nil, err
}
@ -674,72 +694,72 @@ func (s *serverDispatcher) Resolve(ctx context.Context, params *CompletionItem)
return &result, nil
}
func (s *serverDispatcher) Hover(ctx context.Context, params *HoverParams) (*Hover, error) {
var result Hover
func (s *serverDispatcher) Hover(ctx context.Context, params *HoverParams) (*Hover /*Hover | null*/, error) {
var result Hover /*Hover | null*/
if err := s.Conn.Call(ctx, "textDocument/hover", params, &result); err != nil {
return nil, err
}
return &result, nil
}
func (s *serverDispatcher) SignatureHelp(ctx context.Context, params *SignatureHelpParams) (*SignatureHelp, error) {
var result SignatureHelp
func (s *serverDispatcher) SignatureHelp(ctx context.Context, params *SignatureHelpParams) (*SignatureHelp /*SignatureHelp | null*/, error) {
var result SignatureHelp /*SignatureHelp | null*/
if err := s.Conn.Call(ctx, "textDocument/signatureHelp", params, &result); err != nil {
return nil, err
}
return &result, nil
}
func (s *serverDispatcher) Definition(ctx context.Context, params *DefinitionParams) ([]Location, error) {
var result []Location
func (s *serverDispatcher) Definition(ctx context.Context, params *DefinitionParams) (Definition /*Definition | DefinitionLink[] | null*/, error) {
var result Definition /*Definition | DefinitionLink[] | null*/
if err := s.Conn.Call(ctx, "textDocument/definition", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) References(ctx context.Context, params *ReferenceParams) ([]Location, error) {
var result []Location
func (s *serverDispatcher) References(ctx context.Context, params *ReferenceParams) ([]Location /*Location[] | null*/, error) {
var result []Location /*Location[] | null*/
if err := s.Conn.Call(ctx, "textDocument/references", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) DocumentHighlight(ctx context.Context, params *DocumentHighlightParams) ([]DocumentHighlight, error) {
var result []DocumentHighlight
func (s *serverDispatcher) DocumentHighlight(ctx context.Context, params *DocumentHighlightParams) ([]DocumentHighlight /*DocumentHighlight[] | null*/, error) {
var result []DocumentHighlight /*DocumentHighlight[] | null*/
if err := s.Conn.Call(ctx, "textDocument/documentHighlight", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) DocumentSymbol(ctx context.Context, params *DocumentSymbolParams) ([]DocumentSymbol, error) {
var result []DocumentSymbol
func (s *serverDispatcher) DocumentSymbol(ctx context.Context, params *DocumentSymbolParams) ([]DocumentSymbol /*SymbolInformation[] | DocumentSymbol[] | null*/, error) {
var result []DocumentSymbol /*SymbolInformation[] | DocumentSymbol[] | null*/
if err := s.Conn.Call(ctx, "textDocument/documentSymbol", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) CodeAction(ctx context.Context, params *CodeActionParams) ([]CodeAction, error) {
var result []CodeAction
func (s *serverDispatcher) CodeAction(ctx context.Context, params *CodeActionParams) (interface{} /*Command | CodeAction*/ /*(Command | CodeAction)[] | null*/, error) {
var result interface{} /*Command | CodeAction*/ /*(Command | CodeAction)[] | null*/
if err := s.Conn.Call(ctx, "textDocument/codeAction", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) Symbol(ctx context.Context, params *WorkspaceSymbolParams) ([]SymbolInformation, error) {
var result []SymbolInformation
func (s *serverDispatcher) Symbol(ctx context.Context, params *WorkspaceSymbolParams) ([]SymbolInformation /*SymbolInformation[] | null*/, error) {
var result []SymbolInformation /*SymbolInformation[] | null*/
if err := s.Conn.Call(ctx, "workspace/symbol", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) CodeLens(ctx context.Context, params *CodeLensParams) ([]CodeLens, error) {
var result []CodeLens
func (s *serverDispatcher) CodeLens(ctx context.Context, params *CodeLensParams) ([]CodeLens /*CodeLens[] | null*/, error) {
var result []CodeLens /*CodeLens[] | null*/
if err := s.Conn.Call(ctx, "textDocument/codeLens", params, &result); err != nil {
return nil, err
}
@ -754,8 +774,8 @@ func (s *serverDispatcher) ResolveCodeLens(ctx context.Context, params *CodeLens
return &result, nil
}
func (s *serverDispatcher) DocumentLink(ctx context.Context, params *DocumentLinkParams) ([]DocumentLink, error) {
var result []DocumentLink
func (s *serverDispatcher) DocumentLink(ctx context.Context, params *DocumentLinkParams) ([]DocumentLink /*DocumentLink[] | null*/, error) {
var result []DocumentLink /*DocumentLink[] | null*/
if err := s.Conn.Call(ctx, "textDocument/documentLink", params, &result); err != nil {
return nil, err
}
@ -770,63 +790,50 @@ func (s *serverDispatcher) ResolveDocumentLink(ctx context.Context, params *Docu
return &result, nil
}
func (s *serverDispatcher) Formatting(ctx context.Context, params *DocumentFormattingParams) ([]TextEdit, error) {
var result []TextEdit
func (s *serverDispatcher) Formatting(ctx context.Context, params *DocumentFormattingParams) ([]TextEdit /*TextEdit[] | null*/, error) {
var result []TextEdit /*TextEdit[] | null*/
if err := s.Conn.Call(ctx, "textDocument/formatting", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) RangeFormatting(ctx context.Context, params *DocumentRangeFormattingParams) ([]TextEdit, error) {
var result []TextEdit
func (s *serverDispatcher) RangeFormatting(ctx context.Context, params *DocumentRangeFormattingParams) ([]TextEdit /*TextEdit[] | null*/, error) {
var result []TextEdit /*TextEdit[] | null*/
if err := s.Conn.Call(ctx, "textDocument/rangeFormatting", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) OnTypeFormatting(ctx context.Context, params *DocumentOnTypeFormattingParams) ([]TextEdit, error) {
var result []TextEdit
func (s *serverDispatcher) OnTypeFormatting(ctx context.Context, params *DocumentOnTypeFormattingParams) ([]TextEdit /*TextEdit[] | null*/, error) {
var result []TextEdit /*TextEdit[] | null*/
if err := s.Conn.Call(ctx, "textDocument/onTypeFormatting", params, &result); err != nil {
return nil, err
}
return result, nil
}
func (s *serverDispatcher) Rename(ctx context.Context, params *RenameParams) (*WorkspaceEdit, error) {
var result WorkspaceEdit
func (s *serverDispatcher) Rename(ctx context.Context, params *RenameParams) (*WorkspaceEdit /*WorkspaceEdit | null*/, error) {
var result WorkspaceEdit /*WorkspaceEdit | null*/
if err := s.Conn.Call(ctx, "textDocument/rename", params, &result); err != nil {
return nil, err
}
return &result, nil
}
func (s *serverDispatcher) PrepareRename(ctx context.Context, params *PrepareRenameParams) (*Range, error) {
var result Range
func (s *serverDispatcher) PrepareRename(ctx context.Context, params *PrepareRenameParams) (interface{} /* Range | struct{; Range Range`json:"range"`; Placeholder string`json:"placeholder"`; } | nil*/, error) {
var result interface{} /* Range | struct{; Range Range`json:"range"`; Placeholder string`json:"placeholder"`; } | nil*/
if err := s.Conn.Call(ctx, "textDocument/prepareRename", params, &result); err != nil {
return nil, err
}
return &result, nil
}
func (s *serverDispatcher) ExecuteCommand(ctx context.Context, params *ExecuteCommandParams) (interface{}, error) {
var result interface{}
if err := s.Conn.Call(ctx, "workspace/executeCommand", params, &result); err != nil {
return nil, err
}
return result, nil
}
type CancelParams struct {
/**
* The request id to cancel.
*/
ID jsonrpc2.ID `json:"id"`
}
// Types constructed to avoid structs as formal argument types
type ParamInitia struct {
InitializeParams
WorkDoneProgressParams
func (s *serverDispatcher) ExecuteCommand(ctx context.Context, params *ExecuteCommandParams) (interface{} /*any | null*/, error) {
var result interface{} /*any | null*/
if err := s.Conn.Call(ctx, "workspace/executeCommand", params, &result); err != nil {
return nil, err
}
return result, nil
}

View File

@ -91,7 +91,7 @@ type Server struct {
// General
func (s *Server) Initialize(ctx context.Context, params *protocol.ParamInitia) (*protocol.InitializeResult, error) {
func (s *Server) Initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
return s.initialize(ctx, params)
}
@ -107,6 +107,10 @@ func (s *Server) Exit(ctx context.Context) error {
return s.exit(ctx)
}
func (s *Server) CancelRequest(ctx context.Context, params *protocol.CancelParams) error {
return s.CancelRequest(ctx, params)
}
// Workspace
func (s *Server) DidChangeWorkspaceFolders(ctx context.Context, params *protocol.DidChangeWorkspaceFoldersParams) error {
@ -173,15 +177,15 @@ func (s *Server) SignatureHelp(ctx context.Context, params *protocol.SignatureHe
return s.signatureHelp(ctx, params)
}
func (s *Server) Definition(ctx context.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
func (s *Server) Definition(ctx context.Context, params *protocol.DefinitionParams) (protocol.Definition, error) {
return s.definition(ctx, params)
}
func (s *Server) TypeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) {
func (s *Server) TypeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) (protocol.Definition, error) {
return s.typeDefinition(ctx, params)
}
func (s *Server) Implementation(ctx context.Context, params *protocol.ImplementationParams) ([]protocol.Location, error) {
func (s *Server) Implementation(ctx context.Context, params *protocol.ImplementationParams) (protocol.Definition, error) {
return s.implementation(ctx, params)
}
@ -197,7 +201,7 @@ func (s *Server) DocumentSymbol(ctx context.Context, params *protocol.DocumentSy
return s.documentSymbol(ctx, params)
}
func (s *Server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
func (s *Server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) (interface{}, error) {
return s.codeAction(ctx, params)
}
@ -241,7 +245,7 @@ func (s *Server) Rename(ctx context.Context, params *protocol.RenameParams) (*pr
return s.rename(ctx, params)
}
func (s *Server) Declaration(context.Context, *protocol.DeclarationParams) ([]protocol.DeclarationLink, error) {
func (s *Server) Declaration(context.Context, *protocol.DeclarationParams) (protocol.Declaration, error) {
return nil, notImplemented("Declaration")
}
@ -253,7 +257,7 @@ func (s *Server) LogTraceNotification(context.Context, *protocol.LogTraceParams)
return notImplemented("LogtraceNotification")
}
func (s *Server) PrepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (*protocol.Range, error) {
func (s *Server) PrepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (interface{}, error) {
// TODO(suzmue): support sending placeholder text.
return s.prepareRename(ctx, params)
}

View File

@ -183,7 +183,7 @@ func SetOptions(options *Options, opts interface{}) OptionResults {
func (o *Options) ForClientCapabilities(caps protocol.ClientCapabilities) {
// Check if the client supports snippets in completion items.
if c := caps.TextDocument.Completion; c != nil && c.CompletionItem != nil && c.CompletionItem.SnippetSupport {
if c := caps.TextDocument.Completion; c.CompletionItem.SnippetSupport {
o.InsertTextFormat = protocol.SnippetTextFormat
}
// Check if the client supports configuration messages.
@ -192,13 +192,12 @@ func (o *Options) ForClientCapabilities(caps protocol.ClientCapabilities) {
o.DynamicWatchedFilesSupported = caps.Workspace.DidChangeWatchedFiles.DynamicRegistration
// Check which types of content format are supported by this client.
if hover := caps.TextDocument.Hover; hover != nil && len(hover.ContentFormat) > 0 {
if hover := caps.TextDocument.Hover; len(hover.ContentFormat) > 0 {
o.PreferredContentFormat = hover.ContentFormat[0]
}
// Check if the client supports only line folding.
if fr := caps.TextDocument.FoldingRange; fr != nil {
o.LineFoldingOnly = fr.LineFoldingOnly
}
fr := caps.TextDocument.FoldingRange
o.LineFoldingOnly = fr.LineFoldingOnly
}
func (o *Options) set(name string, value interface{}) OptionResult {

View File

@ -26,7 +26,7 @@ func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionIte
Detail: item.Detail,
Documentation: item.Documentation,
InsertText: item.InsertText,
TextEdit: &protocol.TextEdit{
TextEdit: protocol.TextEdit{
NewText: item.Snippet(),
},
// Negate score so best score has lowest sort text like real API.

View File

@ -96,7 +96,7 @@ func fullChange(changes []protocol.TextDocumentContentChangeEvent) (string, bool
return "", false
}
// The length of the changes must be 1 at this point.
if changes[0].Range == nil && changes[0].RangeLength == 0 {
if changes[0].RangeLength == 0 { // used to check changes[0].Range == nil
return changes[0].Text, true
}
return "", false
@ -116,7 +116,7 @@ func (s *Server) applyChanges(ctx context.Context, uri span.URI, changes []proto
Content: content,
}
spn, err := m.RangeSpan(*change.Range)
spn, err := m.RangeSpan(change.Range)
if err != nil {
return "", err
}